Skip to content

Commit bd563e1

Browse files
authored
Closes: #308 - Use a NetBoxTable derivative for the "Custom objects linking to this object" panel (#405)
1 parent 153b821 commit bd563e1

2 files changed

Lines changed: 48 additions & 33 deletions

File tree

netbox_custom_objects/tables.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
from netbox.tables import NetBoxTable, columns
1010
from utilities.permissions import get_permission_for_model
1111

12-
from netbox_custom_objects.models import CustomObject, CustomObjectType
12+
from netbox_custom_objects.models import CustomObject, CustomObjectType, CustomObjectTypeField
1313
from netbox_custom_objects.utilities import get_viewname
1414

15-
__all__ = ("CustomObjectTable",)
15+
__all__ = ("CustomObjectTable", "LinkedCustomObjectTable")
1616

1717

1818
OBJECTCHANGE_FULL_NAME = """
@@ -229,3 +229,21 @@ class Meta(NetBoxTable.Meta):
229229
"created",
230230
"last_updated",
231231
)
232+
233+
234+
class LinkedCustomObjectTable(NetBoxTable):
235+
custom_object_type = tables.Column(
236+
accessor="custom_object__custom_object_type",
237+
linkify=True,
238+
verbose_name=_("Type"),
239+
)
240+
custom_object = tables.Column(
241+
linkify=True,
242+
verbose_name=_("Custom Object"),
243+
)
244+
field = tables.Column(verbose_name=_("Field"))
245+
246+
class Meta(NetBoxTable.Meta):
247+
model = CustomObjectTypeField
248+
fields = ("custom_object_type", "custom_object", "field")
249+
default_columns = ("custom_object_type", "custom_object", "field")

netbox_custom_objects/template_content.py

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from dataclasses import dataclass
22
from typing import Any
33
from django.contrib.contenttypes.models import ContentType
4+
from django.template import Template, Context
45
from netbox.plugins import PluginTemplateExtension
56
from extras.choices import CustomFieldTypeChoices
6-
from utilities.jinja2 import render_jinja2
7+
from utilities.paginator import EnhancedPaginator
78
from netbox_custom_objects.models import CustomObjectTypeField
9+
from netbox_custom_objects.tables import LinkedCustomObjectTable
810

911
__all__ = (
1012
"CustomObjectSchema",
@@ -38,6 +40,7 @@ class LinkedCustomObject:
3840
class CustomObjectLink(PluginTemplateExtension):
3941

4042
def left_page(self):
43+
# Get custom objects linking to this object
4144
content_type = ContentType.objects.get_for_model(
4245
self.context["object"]._meta.model
4346
)
@@ -77,37 +80,31 @@ def left_page(self):
7780
custom_object=model_object, field=field
7881
)
7982
)
80-
return render_jinja2(
81-
"""
82-
<div class="card">
83-
<h2 class="card-header">Custom Objects linking to this object</h2>
84-
<table class="table table-hover attr-table">
85-
<thead>
86-
<tr>
87-
<th>Type</th>
88-
<th>Custom Object</th>
89-
<th>Field</th>
90-
</tr>
91-
</thead>
92-
{% if linked_custom_objects|count <= 20 %}
93-
{% for obj in linked_custom_objects %}
94-
<tr>
95-
<td>{{ obj.field.custom_object_type }}</td>
96-
<th scope="row">
97-
<a href="{{ obj.custom_object.get_absolute_url() }}">{{ obj.custom_object }}</a>
98-
</th>
99-
<td>{{ obj.field }}</td>
100-
</tr>
101-
{% endfor %}
83+
84+
request = self.context["request"]
85+
linked_objects_table = LinkedCustomObjectTable(linked_custom_objects, orderable=False)
86+
linked_objects_table.configure(request)
87+
linked_objects_table.paginate(page=request.GET.get("page", 1), per_page=50, paginator_class=EnhancedPaginator)
88+
89+
template_str = """
90+
{% load render_table from django_tables2 %}
91+
{% load i18n %}
92+
<div class="card">
93+
<h2 class="card-header">{% trans "Custom Objects linking to this object" %}</h2>
94+
{% if table.rows %}
95+
<div class="table-responsive">
96+
{% render_table table 'inc/table.html' %}
97+
{% include 'inc/paginator.html' with paginator=table.paginator page=table.page %}
98+
</div>
99+
{% else %}
100+
<div class="card-body text-muted">{% trans "None" %}</div>
102101
{% endif %}
103-
<tr>
104-
<td colspan="3">{{ linked_custom_objects|count }} objects</td>
105-
</tr>
106-
</table>
107-
</div>
108-
""",
109-
{"linked_custom_objects": linked_custom_objects},
110-
)
102+
</div>
103+
"""
104+
template = Template(template_str)
105+
context = Context({'table': linked_objects_table, "request": request})
106+
rendered_content = template.render(context)
107+
return rendered_content
111108

112109

113110
template_extensions = (

0 commit comments

Comments
 (0)