|
1 | 1 | from dataclasses import dataclass |
2 | 2 | from typing import Any |
3 | 3 | from django.contrib.contenttypes.models import ContentType |
| 4 | +from django.template import Template, Context |
4 | 5 | from netbox.plugins import PluginTemplateExtension |
5 | 6 | from extras.choices import CustomFieldTypeChoices |
6 | | -from utilities.jinja2 import render_jinja2 |
| 7 | +from utilities.paginator import EnhancedPaginator |
7 | 8 | from netbox_custom_objects.models import CustomObjectTypeField |
| 9 | +from netbox_custom_objects.tables import LinkedCustomObjectTable |
8 | 10 |
|
9 | 11 | __all__ = ( |
10 | 12 | "CustomObjectSchema", |
@@ -38,6 +40,7 @@ class LinkedCustomObject: |
38 | 40 | class CustomObjectLink(PluginTemplateExtension): |
39 | 41 |
|
40 | 42 | def left_page(self): |
| 43 | + # Get custom objects linking to this object |
41 | 44 | content_type = ContentType.objects.get_for_model( |
42 | 45 | self.context["object"]._meta.model |
43 | 46 | ) |
@@ -77,37 +80,31 @@ def left_page(self): |
77 | 80 | custom_object=model_object, field=field |
78 | 81 | ) |
79 | 82 | ) |
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> |
102 | 101 | {% 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 |
111 | 108 |
|
112 | 109 |
|
113 | 110 | template_extensions = ( |
|
0 commit comments