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

Introduce ChoiceFieldColumn to replace template columns

This commit is contained in:
Jeremy Stretch
2020-09-25 10:52:14 -04:00
parent 1b55285167
commit 18a8a91d57
6 changed files with 44 additions and 64 deletions

View File

@@ -174,6 +174,22 @@ class ButtonsColumn(tables.TemplateColumn):
return ''
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.
"""
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="label label-{css_class}">{label}</span>'
)
return self.default
class ColorColumn(tables.Column):
"""
Display a color (#RRGGBB).