Skip to content

Commit

Permalink
initial work to render hierarchical region #13735
Browse files Browse the repository at this point in the history
  • Loading branch information
abhi1693 committed Nov 8, 2023
1 parent 22e474f commit 084a5c5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
Empty file.
39 changes: 39 additions & 0 deletions netbox/dcim/templatetags/display_region.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 display_region(context, obj):
"""
Renders hierarchical region data for a given object.
"""
# Check if the obj is an instance of Site
if isinstance(obj, Site):
if not obj.region:
return mark_safe('—')

# If so, retrieve the Site's Region
region = obj.region
else:
if not hasattr(obj, 'site'):
return mark_safe('—')

# Otherwise, retrieve the Region from the Site associated with the object
region = obj.site.region

# Retrieve all regions in the hierarchy
regions = region.get_ancestors(include_self=True)

# Render the hierarchy as a list of links
return mark_safe(
' / '.join([
'<a href="{}">{}</a>'.format(
context['request'].build_absolute_uri(region.get_absolute_url()),
region
) for region in regions
])
)
10 changes: 2 additions & 8 deletions netbox/templates/dcim/device.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{% load helpers %}
{% load plugins %}
{% load i18n %}
{% load display_region %}

{% block content %}
<div class="row">
Expand All @@ -16,14 +17,7 @@ <h5 class="card-header">{% trans "Device" %}</h5>
<tr>
<th scope="row">{% trans "Region" %}</th>
<td>
{% if object.site.region %}
{% for region in object.site.region.get_ancestors %}
{{ region|linkify }} /
{% endfor %}
{{ object.site.region|linkify }}
{% else %}
{{ ''|placeholder }}
{% endif %}
{% display_region object %}
</td>
</tr>
<tr>
Expand Down
10 changes: 2 additions & 8 deletions netbox/templates/dcim/site.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{% load plugins %}
{% load tz %}
{% load i18n %}
{% load display_region %}

{% block breadcrumbs %}
{{ block.super }}
Expand All @@ -29,14 +30,7 @@ <h5 class="card-header">{% trans "Site" %}</h5>
<tr>
<th scope="row">{% trans "Region" %}</th>
<td>
{% if object.region %}
{% for region in object.region.get_ancestors %}
{{ region|linkify }} /
{% endfor %}
{{ object.region|linkify }}
{% else %}
{{ ''|placeholder }}
{% endif %}
{% display_region object %}
</td>
</tr>
<tr>
Expand Down

0 comments on commit 084a5c5

Please sign in to comment.