diff --git a/netbox/dcim/templatetags/__init__.py b/netbox/dcim/templatetags/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/netbox/dcim/templatetags/display_region.py b/netbox/dcim/templatetags/display_region.py new file mode 100644 index 0000000000..9c1ca711dc --- /dev/null +++ b/netbox/dcim/templatetags/display_region.py @@ -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([ + '{}'.format( + context['request'].build_absolute_uri(region.get_absolute_url()), + region + ) for region in regions + ]) + ) diff --git a/netbox/templates/dcim/device.html b/netbox/templates/dcim/device.html index 5fa6a3314a..22aeb20eda 100644 --- a/netbox/templates/dcim/device.html +++ b/netbox/templates/dcim/device.html @@ -5,6 +5,7 @@ {% load helpers %} {% load plugins %} {% load i18n %} +{% load display_region %} {% block content %}
@@ -16,14 +17,7 @@
{% trans "Device" %}
{% trans "Region" %} - {% 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 %} diff --git a/netbox/templates/dcim/site.html b/netbox/templates/dcim/site.html index 7f43a0ab33..e4b9395614 100644 --- a/netbox/templates/dcim/site.html +++ b/netbox/templates/dcim/site.html @@ -3,6 +3,7 @@ {% load plugins %} {% load tz %} {% load i18n %} +{% load display_region %} {% block breadcrumbs %} {{ block.super }} @@ -29,14 +30,7 @@
{% trans "Site" %}
{% trans "Region" %} - {% if object.region %} - {% for region in object.region.get_ancestors %} - {{ region|linkify }} / - {% endfor %} - {{ object.region|linkify }} - {% else %} - {{ ''|placeholder }} - {% endif %} + {% display_region object %}