From be9b35c3a4bf23beaeebc3ff4a674a38a4fa3248 Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 24 Mar 2025 10:00:18 -0700 Subject: [PATCH] 18417 use TemplateColumn --- netbox/dcim/tables/racks.py | 34 +++++++++++------------------ netbox/dcim/tables/template_code.py | 5 +++++ 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/netbox/dcim/tables/racks.py b/netbox/dcim/tables/racks.py index ee0df9891..d9604dc66 100644 --- a/netbox/dcim/tables/racks.py +++ b/netbox/dcim/tables/racks.py @@ -1,12 +1,11 @@ from django.utils.translation import gettext_lazy as _ import django_tables2 as tables from django_tables2.utils import Accessor -from django.utils.safestring import mark_safe from dcim.models import Rack, RackReservation, RackRole, RackType from netbox.tables import NetBoxTable, columns from tenancy.tables import ContactsColumnMixin, TenancyColumnsMixin -from .template_code import WEIGHT +from .template_code import OUTER_UNIT, WEIGHT __all__ = ( 'RackTable', @@ -16,19 +15,6 @@ __all__ = ( ) -class OuterUnitColumn(tables.Column): - """ - Render a model's outer unit value with unit designation or placeholder if the value is None. - """ - DEFAULT_BG_COLOR = 'secondary' - - def render(self, record, value): - if value is None: - return mark_safe('') - - return mark_safe(f'{value} {record.outer_unit}') - - # # Rack roles # @@ -76,13 +62,16 @@ class RackTypeTable(NetBoxTable): template_code="{{ value }}U", verbose_name=_('Height') ) - outer_width = OuterUnitColumn( + outer_width = tables.TemplateColumn( + template_code=OUTER_UNIT, verbose_name=_('Outer Width') ) - outer_height = OuterUnitColumn( + outer_height = tables.TemplateColumn( + template_code=OUTER_UNIT, verbose_name=_('Outer Height') ) - outer_depth = OuterUnitColumn( + outer_depth = tables.TemplateColumn( + template_code=OUTER_UNIT, verbose_name=_('Outer Depth') ) weight = columns.TemplateColumn( @@ -174,13 +163,16 @@ class RackTable(TenancyColumnsMixin, ContactsColumnMixin, NetBoxTable): tags = columns.TagColumn( url_name='dcim:rack_list' ) - outer_width = OuterUnitColumn( + outer_width = tables.TemplateColumn( + template_code=OUTER_UNIT, verbose_name=_('Outer Width') ) - outer_height = OuterUnitColumn( + outer_height = tables.TemplateColumn( + template_code=OUTER_UNIT, verbose_name=_('Outer Height') ) - outer_depth = OuterUnitColumn( + outer_depth = tables.TemplateColumn( + template_code=OUTER_UNIT, verbose_name=_('Outer Depth') ) weight = columns.TemplateColumn( diff --git a/netbox/dcim/tables/template_code.py b/netbox/dcim/tables/template_code.py index 4b51cd06a..13f2589e3 100644 --- a/netbox/dcim/tables/template_code.py +++ b/netbox/dcim/tables/template_code.py @@ -109,6 +109,11 @@ LOCATION_BUTTONS = """ """ +OUTER_UNIT = """ +{% load helpers %} +{% if value %}{{ value }} {{ record.outer_unit }}{% else %}{{ ''|placeholder }}{% endif %} +""" + # # Device component templatebuttons #