1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

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:
members: false
::: netbox.tables.ChoiceFieldColumn
rendering:
show_source: false
selection:
members: false
::: netbox.tables.ColorColumn
rendering:
show_source: false

View File

@ -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(

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)
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.

View File

@ -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):
"""

View File

@ -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')

View File

@ -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'}

View File

@ -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

View File

@ -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)

View File

@ -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):
"""

View File

@ -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)

View File

@ -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(

View File

@ -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

View File

@ -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:

View File

@ -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