Make render_tree suitable for generic use

This commit is contained in:
Jeremy Stretch 2023-11-29 15:50:59 -05:00
parent 2ea37ad221
commit 01c9cc04fb
10 changed files with 63 additions and 96 deletions

View File

@ -1,29 +0,0 @@
from django import template
from django.utils.safestring import mark_safe
from dcim.models import Site
register = template.Library()
@register.simple_tag(takes_context=True)
def nested_tree(context, obj):
"""
Renders hierarchical region data for a given object.
"""
# Retrieve the region or site information
if isinstance(obj, Site):
region = obj.region
else:
region = getattr(obj, 'region', None) or getattr(obj.site, 'region', None)
# Return a placeholder if no region or site is found
if not region:
return mark_safe('—')
# Build the region links if the region is available
return mark_safe(
' / '.join(
'<a href="{}">{}</a>'.format(context['request'].build_absolute_uri(reg.get_absolute_url()), reg)
for reg in region.get_ancestors(include_self=True)
) if region else ''
)

View File

@ -16,9 +16,7 @@
<table class="table table-hover attr-table"> <table class="table table-hover attr-table">
<tr> <tr>
<th scope="row">{% trans "Region" %}</th> <th scope="row">{% trans "Region" %}</th>
<td> <td>{% nested_tree object.site.region %}</td>
{% nested_tree object %}
</td>
</tr> </tr>
<tr> <tr>
<th scope="row">{% trans "Site" %}</th> <th scope="row">{% trans "Site" %}</th>
@ -26,16 +24,7 @@
</tr> </tr>
<tr> <tr>
<th scope="row">{% trans "Location" %}</th> <th scope="row">{% trans "Location" %}</th>
<td> <td>{% nested_tree object.location %}</td>
{% if object.location %}
{% for location in object.location.get_ancestors %}
{{ location|linkify }} /
{% endfor %}
{{ object.location|linkify }}
{% else %}
{{ ''|placeholder }}
{% endif %}
</td>
</tr> </tr>
<tr> <tr>
<th scope="row">{% trans "Rack" %}</th> <th scope="row">{% trans "Rack" %}</th>

View File

@ -18,7 +18,7 @@
<tr> <tr>
<th scope="row">{% trans "Region" %}</th> <th scope="row">{% trans "Region" %}</th>
<td> <td>
{% nested_tree object %} {% nested_tree object.site.region %}
</td> </td>
</tr> </tr>
<tr> <tr>
@ -27,16 +27,7 @@
</tr> </tr>
<tr> <tr>
<th scope="row">{% trans "Location" %}</th> <th scope="row">{% trans "Location" %}</th>
<td> <td>{% nested_tree object.location %}</td>
{% if object.location %}
{% for location in object.location.get_ancestors %}
{{ location|linkify }} /
{% endfor %}
{{ object.location|linkify }}
{% else %}
{{ ''|placeholder }}
{% endif %}
</td>
</tr> </tr>
<tr> <tr>
<th scope="row">{% trans "Facility ID" %}</th> <th scope="row">{% trans "Facility ID" %}</th>

View File

@ -21,26 +21,24 @@
</h5> </h5>
<div class="card-body"> <div class="card-body">
<table class="table table-hover attr-table"> <table class="table table-hover attr-table">
{% with rack=object.rack %} <tr>
<tr> <th scope="row">{% trans "Region" %}</th>
<th scope="row">{% trans "Region" %}</th> <td>
<td> {% nested_tree object.rack.site.region %}
{% nested_tree rack %} </td>
</td> </tr>
</tr> <tr>
<tr> <th scope="row">{% trans "Site" %}</th>
<th scope="row">{% trans "Site" %}</th> <td>{{ object.rack.site|linkify }}</td>
<td>{{ rack.site|linkify }}</td> </tr>
</tr> <tr>
<tr> <th scope="row">{% trans "Location" %}</th>
<th scope="row">{% trans "Location" %}</th> <td>{{ object.rack.location|linkify|placeholder }}</td>
<td>{{ rack.location|linkify|placeholder }}</td> </tr>
</tr> <tr>
<tr> <th scope="row">{% trans "Rack" %}</th>
<th scope="row">{% trans "Rack" %}</th> <td>{{ object.rack|linkify }}</td>
<td>{{ rack|linkify }}</td> </tr>
</tr>
{% endwith %}
</table> </table>
</div> </div>
</div> </div>

View File

@ -30,20 +30,13 @@
<tr> <tr>
<th scope="row">{% trans "Region" %}</th> <th scope="row">{% trans "Region" %}</th>
<td> <td>
{% nested_tree object %} {% nested_tree object.region %}
</td> </td>
</tr> </tr>
<tr> <tr>
<th scope="row">{% trans "Group" %}</th> <th scope="row">{% trans "Group" %}</th>
<td> <td>
{% if object.group %} {% nested_tree object.group %}
{% for group in object.group.get_ancestors %}
{{ group|linkify }} /
{% endfor %}
{{ object.group|linkify }}
{% else %}
{{ ''|placeholder }}
{% endif %}
</td> </td>
</tr> </tr>
<tr> <tr>

View File

@ -45,15 +45,17 @@
{% endif %} {% endif %}
</td> </td>
</tr> </tr>
{% if object.site.region %}
<tr>
<th scope="row">{% trans "Region" %}</th>
<td>
{% nested_tree object.site.region %}
</td>
</tr>
{% endif %}
<tr> <tr>
<th scope="row">{% trans "Region" %}</th> <th scope="row">{% trans "Site" %}</th>
<td> <td>{{ object.site|linkify|placeholder }}</td>
{% nested_tree object %}
</td>
</tr>
<tr>
<th scope="row">{% trans "Site" %}</th>
<td>{{ object.site|linkify|placeholder }}</td>
</tr> </tr>
<tr> <tr>
<th scope="row">{% trans "VLAN" %}</th> <th scope="row">{% trans "VLAN" %}</th>

View File

@ -14,12 +14,14 @@
</h5> </h5>
<div class="card-body"> <div class="card-body">
<table class="table table-hover attr-table"> <table class="table table-hover attr-table">
<tr> {% if object.site.region %}
<th scope="row">{% trans "Region" %}</th> <tr>
<td> <th scope="row">{% trans "Region" %}</th>
{% nested_tree object %} <td>
</td> {% nested_tree object.site.region %}
</tr> </td>
</tr>
{% endif %}
<tr> <tr>
<th scope="row">{% trans "Site" %}</th> <th scope="row">{% trans "Site" %}</th>
<td>{{ object.site|linkify|placeholder }}</td> <td>{{ object.site|linkify|placeholder }}</td>

View File

@ -27,6 +27,7 @@ __all__ = (
'icon_from_status', 'icon_from_status',
'kg_to_pounds', 'kg_to_pounds',
'meters_to_feet', 'meters_to_feet',
'nested_tree',
'percentage', 'percentage',
'querystring', 'querystring',
'startswith', 'startswith',

View File

@ -0,0 +1,20 @@
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
@register.simple_tag()
def nested_tree(obj):
"""
Renders the entire hierarchy of a recursively-nested object (such as Region or SiteGroup).
"""
if not obj:
return mark_safe('&mdash;')
nodes = obj.get_ancestors(include_self=True)
return mark_safe(
' / '.join(
f'<a href="{node.get_absolute_url()}">{node}</a>' for node in nodes
)
)