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 %}