widgets.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from django import forms
  2. class ColorPickerWidget(forms.TextInput):
  3. input_type = 'color'
  4. class ClassifierSelectWidget(forms.CheckboxSelectMultiple):
  5. template_name = 'wagtailcrx/widgets/checkbox_classifiers.html'
  6. def optgroups(self, name, value, attrs=None):
  7. from wagtailcrx.models.snippet_models import Classifier
  8. classifiers = Classifier.objects.all().select_related()
  9. groups = []
  10. has_selected = False
  11. for index, classifier in enumerate(classifiers):
  12. subgroup = []
  13. group_name = classifier.name
  14. subindex = 0
  15. choices = []
  16. for term in classifier.terms.all():
  17. choices.append((term.pk, term.name))
  18. groups.append((group_name, subgroup, index))
  19. for subvalue, sublabel in choices:
  20. selected = (
  21. str(subvalue) in value and
  22. (not has_selected or self.allow_multiple_selected)
  23. )
  24. has_selected |= selected
  25. subgroup.append(self.create_option(
  26. name, subvalue, sublabel, selected, index,
  27. subindex=subindex, attrs=attrs,
  28. ))
  29. if subindex is not None:
  30. subindex += 1
  31. return groups