Selaa lähdekoodia

Adds selection custom field labels to UI

sleepinggenius2 2 vuotta sitten
vanhempi
commit
285187542d

+ 5 - 0
netbox/extras/models/customfields.py

@@ -232,6 +232,11 @@ class CustomField(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel):
             return self.choice_set.choices
         return []
 
+    def get_choice_label(self, value):
+        if not hasattr(self, '_choice_map'):
+            self._choice_map = dict(self.choices)
+        return self._choice_map.get(value, value)
+
     def populate_initial_data(self, content_types):
         """
         Populate initial custom field data upon either a) the creation of a new CustomField, or

+ 3 - 1
netbox/netbox/tables/columns.py

@@ -483,8 +483,10 @@ class CustomFieldColumn(tables.Column):
             return mark_safe('<i class="mdi mdi-close-thick text-danger"></i>')
         if self.customfield.type == CustomFieldTypeChoices.TYPE_URL:
             return mark_safe(f'<a href="{escape(value)}">{escape(value)}</a>')
+        if self.customfield.type == CustomFieldTypeChoices.TYPE_SELECT:
+            return self.customfield.get_choice_label(value)
         if self.customfield.type == CustomFieldTypeChoices.TYPE_MULTISELECT:
-            return ', '.join(v for v in value)
+            return ', '.join(self.customfield.get_choice_label(v) for v in value)
         if self.customfield.type == CustomFieldTypeChoices.TYPE_MULTIOBJECT:
             return mark_safe(', '.join(
                 self._linkify_item(obj) for obj in self.customfield.deserialize(value)

+ 6 - 0
netbox/utilities/templatetags/builtins/tags.py

@@ -1,6 +1,7 @@
 from django import template
 from django.http import QueryDict
 
+from extras.choices import CustomFieldTypeChoices
 from utilities.utils import dict_to_querydict
 
 __all__ = (
@@ -38,6 +39,11 @@ def customfield_value(customfield, value):
         customfield: A CustomField instance
         value: The custom field value applied to an object
     """
+    if value:
+        if customfield.type == CustomFieldTypeChoices.TYPE_SELECT:
+            value = customfield.get_choice_label(value)
+        elif customfield.type == CustomFieldTypeChoices.TYPE_MULTISELECT:
+            value = [customfield.get_choice_label(v) for v in value]
     return {
         'customfield': customfield,
         'value': value,