choices.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. class ChoiceSetMeta(type):
  2. """
  3. Metaclass for ChoiceSet
  4. """
  5. def __call__(cls, *args, **kwargs):
  6. # Django will check if a 'choices' value is callable, and if so assume that it returns an iterable
  7. return getattr(cls, 'CHOICES', ())
  8. def __iter__(cls):
  9. choices = getattr(cls, 'CHOICES', ())
  10. return iter(choices)
  11. class ChoiceSet(metaclass=ChoiceSetMeta):
  12. CHOICES = list()
  13. @classmethod
  14. def values(cls):
  15. return [c[0] for c in unpack_grouped_choices(cls.CHOICES)]
  16. @classmethod
  17. def as_dict(cls):
  18. # Unpack grouped choices before casting as a dict
  19. return dict(unpack_grouped_choices(cls.CHOICES))
  20. def unpack_grouped_choices(choices):
  21. """
  22. Unpack a grouped choices hierarchy into a flat list of two-tuples. For example:
  23. choices = (
  24. ('Foo', (
  25. (1, 'A'),
  26. (2, 'B')
  27. )),
  28. ('Bar', (
  29. (3, 'C'),
  30. (4, 'D')
  31. ))
  32. )
  33. becomes:
  34. choices = (
  35. (1, 'A'),
  36. (2, 'B'),
  37. (3, 'C'),
  38. (4, 'D')
  39. )
  40. """
  41. unpacked_choices = []
  42. for key, value in choices:
  43. if isinstance(value, (list, tuple)):
  44. # Entered an optgroup
  45. for optgroup_key, optgroup_value in value:
  46. unpacked_choices.append((optgroup_key, optgroup_value))
  47. else:
  48. unpacked_choices.append((key, value))
  49. return unpacked_choices
  50. #
  51. # Generic color choices
  52. #
  53. class ColorChoices(ChoiceSet):
  54. COLOR_DARK_RED = 'aa1409'
  55. COLOR_RED = 'f44336'
  56. COLOR_PINK = 'e91e63'
  57. COLOR_ROSE = 'ffe4e1'
  58. COLOR_FUCHSIA = 'ff66ff'
  59. COLOR_PURPLE = '9c27b0'
  60. COLOR_DARK_PURPLE = '673ab7'
  61. COLOR_INDIGO = '3f51b5'
  62. COLOR_BLUE = '2196f3'
  63. COLOR_LIGHT_BLUE = '03a9f4'
  64. COLOR_CYAN = '00bcd4'
  65. COLOR_TEAL = '009688'
  66. COLOR_AQUA = '00ffff'
  67. COLOR_DARK_GREEN = '2f6a31'
  68. COLOR_GREEN = '4caf50'
  69. COLOR_LIGHT_GREEN = '8bc34a'
  70. COLOR_LIME = 'cddc39'
  71. COLOR_YELLOW = 'ffeb3b'
  72. COLOR_AMBER = 'ffc107'
  73. COLOR_ORANGE = 'ff9800'
  74. COLOR_DARK_ORANGE = 'ff5722'
  75. COLOR_BROWN = '795548'
  76. COLOR_LIGHT_GREY = 'c0c0c0'
  77. COLOR_GREY = '9e9e9e'
  78. COLOR_DARK_GREY = '607d8b'
  79. COLOR_BLACK = '111111'
  80. COLOR_WHITE = 'ffffff'
  81. CHOICES = (
  82. (COLOR_DARK_RED, 'Dark Red'),
  83. (COLOR_RED, 'Red'),
  84. (COLOR_PINK, 'Pink'),
  85. (COLOR_ROSE, 'Rose'),
  86. (COLOR_FUCHSIA, 'Fuchsia'),
  87. (COLOR_PURPLE, 'Purple'),
  88. (COLOR_DARK_PURPLE, 'Dark Purple'),
  89. (COLOR_INDIGO, 'Indigo'),
  90. (COLOR_BLUE, 'Blue'),
  91. (COLOR_LIGHT_BLUE, 'Light Blue'),
  92. (COLOR_CYAN, 'Cyan'),
  93. (COLOR_TEAL, 'Teal'),
  94. (COLOR_AQUA, 'Aqua'),
  95. (COLOR_DARK_GREEN, 'Dark Green'),
  96. (COLOR_GREEN, 'Green'),
  97. (COLOR_LIGHT_GREEN, 'Light Green'),
  98. (COLOR_LIME, 'Lime'),
  99. (COLOR_YELLOW, 'Yellow'),
  100. (COLOR_AMBER, 'Amber'),
  101. (COLOR_ORANGE, 'Orange'),
  102. (COLOR_DARK_ORANGE, 'Dark Orange'),
  103. (COLOR_BROWN, 'Brown'),
  104. (COLOR_LIGHT_GREY, 'Light Grey'),
  105. (COLOR_GREY, 'Grey'),
  106. (COLOR_DARK_GREY, 'Dark Grey'),
  107. (COLOR_BLACK, 'Black'),
  108. (COLOR_WHITE, 'White'),
  109. )
  110. #
  111. # Button color choices
  112. #
  113. class ButtonColorChoices(ChoiceSet):
  114. """
  115. Map standard button color choices to Bootstrap 3 button classes
  116. """
  117. DEFAULT = 'outline-dark'
  118. BLUE = 'primary'
  119. CYAN = 'info'
  120. GREEN = 'success'
  121. RED = 'danger'
  122. YELLOW = 'warning'
  123. GREY = 'secondary'
  124. BLACK = 'dark'
  125. CHOICES = (
  126. (DEFAULT, 'Default'),
  127. (BLUE, 'Blue'),
  128. (CYAN, 'Cyan'),
  129. (GREEN, 'Green'),
  130. (RED, 'Red'),
  131. (YELLOW, 'Yellow'),
  132. (GREY, 'Grey'),
  133. (BLACK, 'Black')
  134. )