From 084a5c5ec891c4878144ab3c109983912bc4788c Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Wed, 8 Nov 2023 21:56:50 +0530 Subject: [PATCH] initial work to render hierarchical region #13735 --- netbox/dcim/templatetags/__init__.py | 0 netbox/dcim/templatetags/display_region.py | 39 ++++++++++++++++++++++ netbox/templates/dcim/device.html | 10 ++---- netbox/templates/dcim/site.html | 10 ++---- 4 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 netbox/dcim/templatetags/__init__.py create mode 100644 netbox/dcim/templatetags/display_region.py diff --git a/netbox/dcim/templatetags/__init__.py b/netbox/dcim/templatetags/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/netbox/dcim/templatetags/display_region.py b/netbox/dcim/templatetags/display_region.py new file mode 100644 index 00000000000..9c1ca711dcb --- /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 5fa6a3314a3..22aeb20eda5 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 7f43a0ab33a..e4b93956140 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 %}