Skip to content

Commit ff021a8

Browse files
Adds region hierarchy in templates (#14213)
* initial work to render hierarchical region #13735 * adds site display #13735 * cleanup #13735 * adds display region tag #13735 * refactored region hierarchy #13735 * refactored region hierarchy #13735 * renamed display_region to nested_tree #13735 * Make render_tree suitable for generic use * Remove errant item from __all__ --------- Co-authored-by: Jeremy Stretch <[email protected]>
1 parent 3a3d439 commit ff021a8

File tree

7 files changed

+73
-90
lines changed

7 files changed

+73
-90
lines changed

netbox/templates/dcim/device.html

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{% load helpers %}
66
{% load plugins %}
77
{% load i18n %}
8+
{% load mptt %}
89

910
{% block content %}
1011
<div class="row">
@@ -15,33 +16,15 @@ <h5 class="card-header">{% trans "Device" %}</h5>
1516
<table class="table table-hover attr-table">
1617
<tr>
1718
<th scope="row">{% trans "Region" %}</th>
18-
<td>
19-
{% if object.site.region %}
20-
{% for region in object.site.region.get_ancestors %}
21-
{{ region|linkify }} /
22-
{% endfor %}
23-
{{ object.site.region|linkify }}
24-
{% else %}
25-
{{ ''|placeholder }}
26-
{% endif %}
27-
</td>
19+
<td>{% nested_tree object.site.region %}</td>
2820
</tr>
2921
<tr>
3022
<th scope="row">{% trans "Site" %}</th>
3123
<td>{{ object.site|linkify }}</td>
3224
</tr>
3325
<tr>
3426
<th scope="row">{% trans "Location" %}</th>
35-
<td>
36-
{% if object.location %}
37-
{% for location in object.location.get_ancestors %}
38-
{{ location|linkify }} /
39-
{% endfor %}
40-
{{ object.location|linkify }}
41-
{% else %}
42-
{{ ''|placeholder }}
43-
{% endif %}
44-
</td>
27+
<td>{% nested_tree object.location %}</td>
4528
</tr>
4629
<tr>
4730
<th scope="row">{% trans "Rack" %}</th>

netbox/templates/dcim/rack.html

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{% load static %}
55
{% load plugins %}
66
{% load i18n %}
7+
{% load mptt %}
78

89
{% block content %}
910
<div class="row">
@@ -15,26 +16,18 @@ <h5 class="card-header">
1516
<div class="card-body">
1617
<table class="table table-hover attr-table">
1718
<tr>
18-
<th scope="row">{% trans "Site" %}</th>
19+
<th scope="row">{% trans "Region" %}</th>
1920
<td>
20-
{% if object.site.region %}
21-
{{ object.site.region|linkify }} /
22-
{% endif %}
23-
{{ object.site|linkify }}
21+
{% nested_tree object.site.region %}
2422
</td>
2523
</tr>
24+
<tr>
25+
<th scope="row">{% trans "Site" %}</th>
26+
<td>{{ object.site|linkify }}</td>
27+
</tr>
2628
<tr>
2729
<th scope="row">{% trans "Location" %}</th>
28-
<td>
29-
{% if object.location %}
30-
{% for location in object.location.get_ancestors %}
31-
{{ location|linkify }} /
32-
{% endfor %}
33-
{{ object.location|linkify }}
34-
{% else %}
35-
{{ ''|placeholder }}
36-
{% endif %}
37-
</td>
30+
<td>{% nested_tree object.location %}</td>
3831
</tr>
3932
<tr>
4033
<th scope="row">{% trans "Facility ID" %}</th>

netbox/templates/dcim/rackreservation.html

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{% load static %}
55
{% load plugins %}
66
{% load i18n %}
7+
{% load mptt %}
78

89
{% block breadcrumbs %}
910
{{ block.super }}
@@ -20,25 +21,24 @@ <h5 class="card-header">
2021
</h5>
2122
<div class="card-body">
2223
<table class="table table-hover attr-table">
23-
{% with rack=object.rack %}
24-
<tr>
25-
<th scope="row">{% trans "Site" %}</th>
26-
<td>
27-
{% if rack.site.region %}
28-
{{ rack.site.region|linkify }} /
29-
{% endif %}
30-
{{ rack.site|linkify }}
31-
</td>
32-
</tr>
33-
<tr>
34-
<th scope="row">{% trans "Location" %}</th>
35-
<td>{{ rack.location|linkify|placeholder }}</td>
36-
</tr>
37-
<tr>
38-
<th scope="row">{% trans "Rack" %}</th>
39-
<td>{{ rack|linkify }}</td>
40-
</tr>
41-
{% endwith %}
24+
<tr>
25+
<th scope="row">{% trans "Region" %}</th>
26+
<td>
27+
{% nested_tree object.rack.site.region %}
28+
</td>
29+
</tr>
30+
<tr>
31+
<th scope="row">{% trans "Site" %}</th>
32+
<td>{{ object.rack.site|linkify }}</td>
33+
</tr>
34+
<tr>
35+
<th scope="row">{% trans "Location" %}</th>
36+
<td>{{ object.rack.location|linkify|placeholder }}</td>
37+
</tr>
38+
<tr>
39+
<th scope="row">{% trans "Rack" %}</th>
40+
<td>{{ object.rack|linkify }}</td>
41+
</tr>
4242
</table>
4343
</div>
4444
</div>

netbox/templates/dcim/site.html

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{% load plugins %}
44
{% load tz %}
55
{% load i18n %}
6+
{% load mptt %}
67

78
{% block breadcrumbs %}
89
{{ block.super }}
@@ -29,27 +30,13 @@ <h5 class="card-header">{% trans "Site" %}</h5>
2930
<tr>
3031
<th scope="row">{% trans "Region" %}</th>
3132
<td>
32-
{% if object.region %}
33-
{% for region in object.region.get_ancestors %}
34-
{{ region|linkify }} /
35-
{% endfor %}
36-
{{ object.region|linkify }}
37-
{% else %}
38-
{{ ''|placeholder }}
39-
{% endif %}
33+
{% nested_tree object.region %}
4034
</td>
4135
</tr>
4236
<tr>
4337
<th scope="row">{% trans "Group" %}</th>
4438
<td>
45-
{% if object.group %}
46-
{% for group in object.group.get_ancestors %}
47-
{{ group|linkify }} /
48-
{% endfor %}
49-
{{ object.group|linkify }}
50-
{% else %}
51-
{{ ''|placeholder }}
52-
{% endif %}
39+
{% nested_tree object.group %}
5340
</td>
5441
</tr>
5542
<tr>

netbox/templates/ipam/prefix.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{% load helpers %}
44
{% load plugins %}
55
{% load i18n %}
6+
{% load mptt %}
67

78
{% block content %}
89
<div class="row">
@@ -44,18 +45,17 @@ <h5 class="card-header">{% trans "Prefix" %}</h5>
4445
{% endif %}
4546
</td>
4647
</tr>
48+
{% if object.site.region %}
49+
<tr>
50+
<th scope="row">{% trans "Region" %}</th>
51+
<td>
52+
{% nested_tree object.site.region %}
53+
</td>
54+
</tr>
55+
{% endif %}
4756
<tr>
4857
<th scope="row">{% trans "Site" %}</th>
49-
<td>
50-
{% if object.site %}
51-
{% if object.site.region %}
52-
{{ object.site.region|linkify }} /
53-
{% endif %}
54-
{{ object.site|linkify }}
55-
{% else %}
56-
{{ ''|placeholder }}
57-
{% endif %}
58-
</td>
58+
<td>{{ object.site|linkify|placeholder }}</td>
5959
</tr>
6060
<tr>
6161
<th scope="row">{% trans "VLAN" %}</th>

netbox/templates/ipam/vlan.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{% load render_table from django_tables2 %}
44
{% load plugins %}
55
{% load i18n %}
6+
{% load mptt %}
67

78
{% block content %}
89
<div class="row">
@@ -13,18 +14,17 @@ <h5 class="card-header">
1314
</h5>
1415
<div class="card-body">
1516
<table class="table table-hover attr-table">
17+
{% if object.site.region %}
18+
<tr>
19+
<th scope="row">{% trans "Region" %}</th>
20+
<td>
21+
{% nested_tree object.site.region %}
22+
</td>
23+
</tr>
24+
{% endif %}
1625
<tr>
1726
<th scope="row">{% trans "Site" %}</th>
18-
<td>
19-
{% if object.site %}
20-
{% if object.site.region %}
21-
{{ object.site.region|linkify }} /
22-
{% endif %}
23-
{{ object.site|linkify }}
24-
{% else %}
25-
{{ ''|placeholder }}
26-
{% endif %}
27-
</td>
27+
<td>{{ object.site|linkify|placeholder }}</td>
2828
</tr>
2929
<tr>
3030
<th scope="row">{% trans "Group" %}</th>

netbox/utilities/templatetags/mptt.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from django import template
2+
from django.utils.safestring import mark_safe
3+
4+
register = template.Library()
5+
6+
7+
@register.simple_tag()
8+
def nested_tree(obj):
9+
"""
10+
Renders the entire hierarchy of a recursively-nested object (such as Region or SiteGroup).
11+
"""
12+
if not obj:
13+
return mark_safe('&mdash;')
14+
15+
nodes = obj.get_ancestors(include_self=True)
16+
return mark_safe(
17+
' / '.join(
18+
f'<a href="{node.get_absolute_url()}">{node}</a>' for node in nodes
19+
)
20+
)

0 commit comments

Comments
 (0)