Browse Source

#1033: Convert ColorSelect to a templatized widget

Jeremy Stretch 8 years ago
parent
commit
d2bd4a213b
2 changed files with 5 additions and 14 deletions
  1. 4 14
      netbox/utilities/forms.py
  2. 1 0
      netbox/utilities/templates/colorselect_option.html

+ 4 - 14
netbox/utilities/forms.py

@@ -118,25 +118,15 @@ class SmallTextarea(forms.Textarea):
 
 
 class ColorSelect(forms.Select):
+    """
+    Extends the built-in Select widget to colorize each <option>.
+    """
+    option_template_name = 'colorselect_option.html'
 
     def __init__(self, *args, **kwargs):
         kwargs['choices'] = COLOR_CHOICES
         super(ColorSelect, self).__init__(*args, **kwargs)
 
-    def render_option(self, selected_choices, option_value, option_label):
-        if option_value is None:
-            option_value = ''
-        option_value = force_text(option_value)
-        if option_value in selected_choices:
-            selected_html = mark_safe(' selected')
-            if not self.allow_multiple_selected:
-                # Only allow for a single selection.
-                selected_choices.remove(option_value)
-        else:
-            selected_html = ''
-        return format_html('<option value="{}"{} style="background-color: #{}">{}</option>',
-                           option_value, selected_html, option_value, force_text(option_label))
-
 
 class SelectWithDisabled(forms.Select):
     """

+ 1 - 0
netbox/utilities/templates/colorselect_option.html

@@ -0,0 +1 @@
+<option value="{{ widget.value }}"{% include "django/forms/widgets/attrs.html" %} style="background-color: #{{ widget.value }}">{{ widget.label }}</option>