utils.py 802 B

12345678910111213141516171819202122232425262728
  1. from decimal import Decimal
  2. from django.utils.translation import gettext_lazy as _
  3. from .choices import WirelessChannelChoices
  4. __all__ = (
  5. 'get_channel_attr',
  6. )
  7. def get_channel_attr(channel, attr):
  8. """
  9. Return the specified attribute of a given WirelessChannelChoices value.
  10. """
  11. if channel not in WirelessChannelChoices.values():
  12. raise ValueError(_("Invalid channel value: {channel}").format(channel=channel))
  13. channel_values = channel.split('-')
  14. attrs = {
  15. 'band': channel_values[0],
  16. 'id': int(channel_values[1]),
  17. 'frequency': Decimal(channel_values[2]),
  18. 'width': Decimal(channel_values[3]),
  19. }
  20. if attr not in attrs:
  21. raise ValueError(_("Invalid channel attribute: {name}").format(name=attr))
  22. return attrs[attr]