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

This commit is contained in:
jeremystretch 2022-02-10 16:29:19 -05:00
parent f111380674
commit 71d3dc6e44
14 changed files with 19 additions and 58 deletions

View File

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

View File

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

View File

@ -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) # Update the private pk used in __str__ in case this is a new object (i.e. just got its pk)
self._pk = self.pk self._pk = self.pk
def get_status_class(self):
return LinkStatusChoices.colors.get(self.status)
def get_compatible_types(self): def get_compatible_types(self):
""" """
Return all termination types compatible with termination A. Return all termination types compatible with termination A.

View File

@ -1001,9 +1001,6 @@ class Device(NetBoxModel, ConfigContextModel):
""" """
return Device.objects.filter(parent_bay__device=self.pk) 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): class Module(NetBoxModel, ConfigContextModel):
""" """

View File

@ -169,9 +169,3 @@ class PowerFeed(NetBoxModel, PathEndpoint, LinkTermination):
@property @property
def parent_object(self): def parent_object(self):
return self.power_panel 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')

View File

@ -247,9 +247,6 @@ class Rack(NetBoxModel):
else: else:
return reversed(range(1, self.u_height + 1)) 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): 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'} Return a list of rack units as dictionaries. Example: {'device': None, 'face': 0, 'id': 48, 'name': 'U48'}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -167,19 +167,22 @@ class ActionsColumn(tables.Column):
class ChoiceFieldColumn(tables.Column): class ChoiceFieldColumn(tables.Column):
""" """
Render a ChoiceField value inside a <span> indicating a particular CSS class. This is useful for displaying colored Render a model's static ChoiceField with its value from `get_FOO_display()` as a colored badge. Colors are derived
choices. The CSS class is derived by calling .get_FOO_class() on the row record. from the ChoiceSet associated with the model field.
""" """
def render(self, record, bound_column, value): def render(self, record, bound_column, value):
if value: if value in self.empty_values:
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 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): def value(self, value):
return value return value

View File

@ -322,9 +322,6 @@ class VirtualMachine(NetBoxModel, ConfigContextModel):
field: f"The specified IP address ({ip}) is not assigned to this VM.", 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 @property
def primary_ip(self): def primary_ip(self):
if get_config().PREFER_IPV4 and self.primary_ip4: if get_config().PREFER_IPV4 and self.primary_ip4:

View File

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