Skip to content

Commit 8835b7f

Browse files
authored
Merge pull request #322 from netboxlabs/284-tags-table
284 Add "tags" to table columns for Custom Objects
2 parents abe3edb + aae4ad2 commit 8835b7f

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

netbox_custom_objects/tables.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class CustomObjectTypeTable(NetBoxTable):
5656
verbose_name=_('Comments'),
5757
)
5858
tags = columns.TagColumn(
59-
url_name='circuits:provider_list'
59+
url_name='plugins:netbox_custom_objects:customobjecttype_list'
6060
)
6161
name = tables.Column(
6262
verbose_name=_('Name'),
@@ -87,6 +87,36 @@ class Meta(NetBoxTable.Meta):
8787
)
8888

8989

90+
class CustomObjectTagColumn(columns.TagColumn):
91+
"""
92+
Custom TagColumn that generates tag filter URLs with the custom_object_type slug.
93+
"""
94+
template_code = """
95+
{% load helpers %}
96+
{% for tag in value.all %}
97+
<a href="{% url 'plugins:netbox_custom_objects:customobject_list'
98+
custom_object_type=record.custom_object_type.slug %}?tag={{ tag.slug }}">
99+
<span {% if tag.description %}title="{{ tag.description }}"{% endif %}
100+
class="badge"
101+
style="color: {{ tag.color|fgcolor }}; background-color: #{{ tag.color }}">
102+
{{ tag }}
103+
</span>
104+
</a>
105+
{% empty %}
106+
<span class="text-muted">&mdash;</span>
107+
{% endfor %}
108+
"""
109+
110+
def __init__(self):
111+
# Override parent __init__ to use our custom template
112+
tables.TemplateColumn.__init__(
113+
self,
114+
orderable=False,
115+
template_code=self.template_code,
116+
verbose_name=_('Tags'),
117+
)
118+
119+
90120
class CustomObjectActionsColumn(columns.ActionsColumn):
91121

92122
def render(self, record, table, **kwargs):
@@ -178,6 +208,7 @@ class CustomObjectTable(NetBoxTable):
178208
actions = CustomObjectActionsColumn(
179209
actions=('edit', 'delete'),
180210
)
211+
tags = CustomObjectTagColumn()
181212

182213
class Meta(NetBoxTable.Meta):
183214
model = CustomObject
@@ -188,6 +219,7 @@ class Meta(NetBoxTable.Meta):
188219
"custom_object_type",
189220
"created",
190221
"last_updated",
222+
"tags",
191223
)
192224
default_columns = (
193225
"pk",

netbox_custom_objects/views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from netbox.views import generic
2121
from netbox.views.generic.mixins import TableMixin
2222
from utilities.forms import ConfirmationForm
23+
from utilities.forms.fields import TagFilterField
2324
from utilities.htmx import htmx_partial
2425
from utilities.views import ConditionalLoginRequiredMixin, ViewTab, get_viewname, register_model_view
2526

@@ -359,6 +360,7 @@ def get_filterset_form(self):
359360
attrs = {
360361
"model": model,
361362
"__module__": "database.filterset_forms",
363+
"tag": TagFilterField(model),
362364
}
363365

364366
for field in self.custom_object_type.fields.all():

0 commit comments

Comments
 (0)