widgets.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from django import forms
  2. class ColorPickerWidget(forms.TextInput):
  3. input_type = "color"
  4. class ClassifierSelectWidget(forms.CheckboxSelectMultiple):
  5. template_name = "coderedcms/widgets/checkbox_classifiers.html"
  6. def optgroups(self, name, value, attrs=None):
  7. from coderedcms.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 = str(subvalue) in value and (
  21. not has_selected or self.allow_multiple_selected
  22. )
  23. has_selected |= selected
  24. subgroup.append(
  25. self.create_option(
  26. name,
  27. subvalue,
  28. sublabel,
  29. selected,
  30. index,
  31. subindex=subindex,
  32. attrs=attrs,
  33. )
  34. )
  35. if subindex is not None:
  36. subindex += 1
  37. return groups