Просмотр исходного кода

Improve ChoiceFieldColumn to not rely on model method to derive label color

jeremystretch 4 лет назад
Родитель
Сommit
71d3dc6e44

+ 6 - 0
docs/plugins/development/tables.md

@@ -57,6 +57,12 @@ The table column classes listed below are supported for use in plugins. These cl
     selection:
       members: false
 
+::: netbox.tables.ChoiceFieldColumn
+    rendering:
+      show_source: false
+    selection:
+      members: false
+
 ::: netbox.tables.ColorColumn
     rendering:
       show_source: false

+ 0 - 3
netbox/circuits/models/circuits.py

@@ -132,9 +132,6 @@ class Circuit(NetBoxModel):
     def get_absolute_url(self):
         return reverse('circuits:circuit', args=[self.pk])
 
-    def get_status_class(self):
-        return CircuitStatusChoices.colors.get(self.status, 'secondary')
-
 
 class CircuitTermination(WebhooksMixin, ChangeLoggedModel, LinkTermination):
     circuit = models.ForeignKey(

+ 0 - 3
netbox/dcim/models/cables.py

@@ -286,9 +286,6 @@ class Cable(NetBoxModel):
         # Update the private pk used in __str__ in case this is a new object (i.e. just got its pk)
         self._pk = self.pk
 
-    def get_status_class(self):
-        return LinkStatusChoices.colors.get(self.status)
-
     def get_compatible_types(self):
         """
         Return all termination types compatible with termination A.

+ 0 - 3
netbox/dcim/models/devices.py

@@ -1001,9 +1001,6 @@ class Device(NetBoxModel, ConfigContextModel):
         """
         return Device.objects.filter(parent_bay__device=self.pk)
 
-    def get_status_class(self):
-        return DeviceStatusChoices.colors.get(self.status, 'secondary')
-
 
 class Module(NetBoxModel, ConfigContextModel):
     """

+ 0 - 6
netbox/dcim/models/power.py

@@ -169,9 +169,3 @@ class PowerFeed(NetBoxModel, PathEndpoint, LinkTermination):
     @property
     def parent_object(self):
         return self.power_panel
-
-    def get_type_class(self):
-        return PowerFeedTypeChoices.colors.get(self.type)
-
-    def get_status_class(self):
-        return PowerFeedStatusChoices.colors.get(self.status, 'secondary')

+ 0 - 3
netbox/dcim/models/racks.py

@@ -247,9 +247,6 @@ class Rack(NetBoxModel):
         else:
             return reversed(range(1, self.u_height + 1))
 
-    def get_status_class(self):
-        return RackStatusChoices.colors.get(self.status, 'secondary')
-
     def get_rack_units(self, user=None, face=DeviceFaceChoices.FACE_FRONT, exclude=None, expand_devices=True):
         """
         Return a list of rack units as dictionaries. Example: {'device': None, 'face': 0, 'id': 48, 'name': 'U48'}

+ 0 - 3
netbox/dcim/models/sites.py

@@ -309,9 +309,6 @@ class Site(NetBoxModel):
     def get_absolute_url(self):
         return reverse('dcim:site', args=[self.pk])
 
-    def get_status_class(self):
-        return SiteStatusChoices.colors.get(self.status, 'secondary')
-
 
 #
 # Locations

+ 0 - 3
netbox/extras/models/change_logging.py

@@ -102,6 +102,3 @@ class ObjectChange(models.Model):
 
     def get_absolute_url(self):
         return reverse('extras:objectchange', args=[self.pk])
-
-    def get_action_class(self):
-        return ObjectChangeActionChoices.colors.get(self.action)

+ 0 - 3
netbox/extras/models/models.py

@@ -458,9 +458,6 @@ class JournalEntry(WebhooksMixin, ChangeLoggedModel):
     def get_absolute_url(self):
         return reverse('extras:journalentry', args=[self.pk])
 
-    def get_kind_class(self):
-        return JournalEntryKindChoices.colors.get(self.kind)
-
 
 class JobResult(models.Model):
     """

+ 0 - 12
netbox/ipam/models/ip.py

@@ -437,9 +437,6 @@ class Prefix(GetAvailablePrefixesMixin, NetBoxModel):
             self.prefix.prefixlen = value
     prefix_length = property(fset=_set_prefix_length)
 
-    def get_status_class(self):
-        return PrefixStatusChoices.colors.get(self.status, 'secondary')
-
     def get_parents(self, include_self=False):
         """
         Return all containing Prefixes in the hierarchy.
@@ -706,9 +703,6 @@ class IPRange(NetBoxModel):
         self.end_address.prefixlen = value
     prefix_length = property(fset=_set_prefix_length)
 
-    def get_status_class(self):
-        return IPRangeStatusChoices.colors.get(self.status, 'secondary')
-
     def get_child_ips(self):
         """
         Return all IPAddresses within this IPRange and VRF.
@@ -922,9 +916,3 @@ class IPAddress(NetBoxModel):
         if self.address is not None:
             self.address.prefixlen = value
     mask_length = property(fset=_set_mask_length)
-
-    def get_status_class(self):
-        return IPAddressStatusChoices.colors.get(self.status, 'secondary')
-
-    def get_role_class(self):
-        return IPAddressRoleChoices.colors.get(self.role)

+ 0 - 3
netbox/ipam/models/vlans.py

@@ -211,9 +211,6 @@ class VLAN(NetBoxModel):
                        f"{self.group}"
             })
 
-    def get_status_class(self):
-        return VLANStatusChoices.colors.get(self.status, 'secondary')
-
     def get_interfaces(self):
         # Return all device interfaces assigned to this VLAN
         return Interface.objects.filter(

+ 13 - 10
netbox/netbox/tables/columns.py

@@ -167,18 +167,21 @@ class ActionsColumn(tables.Column):
 
 class ChoiceFieldColumn(tables.Column):
     """
-    Render a ChoiceField value inside a <span> indicating a particular CSS class. This is useful for displaying colored
-    choices. The CSS class is derived by calling .get_FOO_class() on the row record.
+    Render a model's static ChoiceField with its value from `get_FOO_display()` as a colored badge. Colors are derived
+    from the ChoiceSet associated with the model field.
     """
     def render(self, record, bound_column, value):
-        if value:
-            name = bound_column.name
-            css_class = getattr(record, f'get_{name}_class')()
-            label = getattr(record, f'get_{name}_display')()
-            return mark_safe(
-                f'<span class="badge bg-{css_class}">{label}</span>'
-            )
-        return self.default
+        if value in self.empty_values:
+            return self.default
+
+        accessor = tables.A(bound_column.accessor)
+        field = accessor.get_field(record)
+        raw_value = accessor.resolve(record)  # `value` is from get_FOO_display()
+
+        # Determine the background color to use
+        bg_color = field.choices.colors.get(raw_value, 'secondary')
+
+        return mark_safe(f'<span class="badge bg-{bg_color}">{value}</span>')
 
     def value(self, value):
         return value

+ 0 - 3
netbox/virtualization/models.py

@@ -322,9 +322,6 @@ class VirtualMachine(NetBoxModel, ConfigContextModel):
                         field: f"The specified IP address ({ip}) is not assigned to this VM.",
                     })
 
-    def get_status_class(self):
-        return VirtualMachineStatusChoices.colors.get(self.status, 'secondary')
-
     @property
     def primary_ip(self):
         if get_config().PREFER_IPV4 and self.primary_ip4:

+ 0 - 3
netbox/wireless/models.py

@@ -177,9 +177,6 @@ class WirelessLink(WirelessAuthenticationBase, NetBoxModel):
     def get_absolute_url(self):
         return reverse('wireless:wirelesslink', args=[self.pk])
 
-    def get_status_class(self):
-        return LinkStatusChoices.colors.get(self.status)
-
     def clean(self):
 
         # Validate interface types