From 8b1e18502f71fd39f1e34f2e01dde0ee2dd0cc67 Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Wed, 8 Nov 2023 22:06:33 +0530 Subject: [PATCH] adds site display #13735 --- netbox/dcim/templatetags/display_region.py | 57 +++++++++++++--------- netbox/templates/dcim/rack.html | 6 +-- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/netbox/dcim/templatetags/display_region.py b/netbox/dcim/templatetags/display_region.py index 9c1ca711dcb..53ad159ea58 100644 --- a/netbox/dcim/templatetags/display_region.py +++ b/netbox/dcim/templatetags/display_region.py @@ -1,39 +1,50 @@ from django import template from django.utils.safestring import mark_safe -from dcim.models import Site - register = template.Library() +def _display_site(obj): + """ + Render a link to the site of an object. + """ + if hasattr(obj, 'site'): + return mark_safe('{}'.format( + obj.site.get_absolute_url(), + obj.site + )) + return None + @register.simple_tag(takes_context=True) -def display_region(context, obj): +def display_region(context, obj, include_site=False): """ 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('—') + # Attempt to retrieve the region from obj or its site attribute + region = getattr(obj, 'region', None) or getattr(getattr(obj, 'site', None), 'region', None) - # 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 + # Return a placeholder if no region is found + if not region: + # If include_site is True, attempt to retrieve the site from obj + if include_site: + return _display_site(obj) or mark_safe('—') + return mark_safe('—') # Retrieve all regions in the hierarchy regions = region.get_ancestors(include_self=True) + # Build the URLs and names for the regions + regions_links = [ + '{}'.format( + context['request'].build_absolute_uri(region.get_absolute_url()), region + ) for region in regions + ] + # 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 - ]) - ) + region = mark_safe(' / '.join(regions_links)) + if include_site: + site = _display_site(obj) + if site: + return mark_safe('{} / {}'.format(region, site)) + + return region diff --git a/netbox/templates/dcim/rack.html b/netbox/templates/dcim/rack.html index 671c7ab2eb2..1a3f4e4ef50 100644 --- a/netbox/templates/dcim/rack.html +++ b/netbox/templates/dcim/rack.html @@ -4,6 +4,7 @@ {% load static %} {% load plugins %} {% load i18n %} +{% load display_region %} {% block content %}
@@ -17,10 +18,7 @@
{% trans "Site" %} - {% if object.site.region %} - {{ object.site.region|linkify }} / - {% endif %} - {{ object.site|linkify }} + {% display_region object include_site=True %}