refactored region hierarchy #13735

This commit is contained in:
Abhimanyu Saharan 2023-11-09 20:48:18 +05:30
parent da72ab8a7a
commit f615eec28c
8 changed files with 55 additions and 51 deletions

View File

@ -1,37 +0,0 @@
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
def _display_site(obj):
"""
Render a link to the site of an object.
"""
site = getattr(obj, 'site', None)
return mark_safe('<a href="{}">{}</a>'.format(site.get_absolute_url(), site)) if site else None
@register.simple_tag(takes_context=True)
def display_region(context, obj, include_site=False):
"""
Renders hierarchical region data for a given object, optionally including the site.
"""
# Retrieve the region or site information
region = getattr(obj, 'region', None) or getattr(obj.site, 'region', None) if hasattr(obj, 'site') else None
site_link = _display_site(obj) if include_site else None
# Return a placeholder if no region or site is found
if not region and not site_link:
return mark_safe('&mdash;')
# Build the region links if the region is available
region_links = ' / '.join(
'<a href="{}">{}</a>'.format(context['request'].build_absolute_uri(reg.get_absolute_url()), reg)
for reg in region.get_ancestors(include_self=True)
) if region else ''
# Concatenate region and site links
links = ' / '.join(filter(None, [region_links, site_link]))
return mark_safe(links)

View File

@ -0,0 +1,25 @@
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
@register.simple_tag(takes_context=True)
def display_region(context, obj):
"""
Renders hierarchical region data for a given object.
"""
# Retrieve the region or site information
region = getattr(obj, 'region', None) or getattr(obj.site, 'region', None)
# Return a placeholder if no region or site is found
if not region:
return mark_safe('&mdash;')
# Build the region links if the region is available
return mark_safe(
' / '.join(
'<a href="{}">{}</a>'.format(context['request'].build_absolute_uri(reg.get_absolute_url()), reg)
for reg in region.get_ancestors(include_self=True)
) if region else ''
)

View File

@ -5,7 +5,7 @@
{% load helpers %} {% load helpers %}
{% load plugins %} {% load plugins %}
{% load i18n %} {% load i18n %}
{% load display_region %} {% load mptt %}
{% block content %} {% block content %}
<div class="row"> <div class="row">

View File

@ -4,7 +4,7 @@
{% load static %} {% load static %}
{% load plugins %} {% load plugins %}
{% load i18n %} {% load i18n %}
{% load display_region %} {% load mptt %}
{% block content %} {% block content %}
<div class="row"> <div class="row">
@ -16,11 +16,15 @@
<div class="card-body"> <div class="card-body">
<table class="table table-hover attr-table"> <table class="table table-hover attr-table">
<tr> <tr>
<th scope="row">{% trans "Site" %}</th> <th scope="row">{% trans "Region" %}</th>
<td> <td>
{% display_region object include_site=True %} {% display_region object %}
</td> </td>
</tr> </tr>
<tr>
<th scope="row">{% trans "Site" %}</th>
<td>{{ object.site|linkify }}</td>
</tr>
<tr> <tr>
<th scope="row">{% trans "Location" %}</th> <th scope="row">{% trans "Location" %}</th>
<td> <td>

View File

@ -4,7 +4,7 @@
{% load static %} {% load static %}
{% load plugins %} {% load plugins %}
{% load i18n %} {% load i18n %}
{% load display_region %} {% load mptt %}
{% block breadcrumbs %} {% block breadcrumbs %}
{{ block.super }} {{ block.super }}
@ -23,11 +23,15 @@
<table class="table table-hover attr-table"> <table class="table table-hover attr-table">
{% with rack=object.rack %} {% with rack=object.rack %}
<tr> <tr>
<th scope="row">{% trans "Site" %}</th> <th scope="row">{% trans "Region" %}</th>
<td> <td>
{% display_region rack include_site=True %} {% display_region rack %}
</td> </td>
</tr> </tr>
<tr>
<th scope="row">{% trans "Site" %}</th>
<td>{{ rack.site|linkify }}</td>
</tr>
<tr> <tr>
<th scope="row">{% trans "Location" %}</th> <th scope="row">{% trans "Location" %}</th>
<td>{{ rack.location|linkify|placeholder }}</td> <td>{{ rack.location|linkify|placeholder }}</td>

View File

@ -3,7 +3,7 @@
{% load plugins %} {% load plugins %}
{% load tz %} {% load tz %}
{% load i18n %} {% load i18n %}
{% load display_region %} {% load mptt %}
{% block breadcrumbs %} {% block breadcrumbs %}
{{ block.super }} {{ block.super }}

View File

@ -3,7 +3,7 @@
{% load helpers %} {% load helpers %}
{% load plugins %} {% load plugins %}
{% load i18n %} {% load i18n %}
{% load display_region %} {% load mptt %}
{% block content %} {% block content %}
<div class="row"> <div class="row">
@ -46,11 +46,15 @@
</td> </td>
</tr> </tr>
<tr> <tr>
<th scope="row">{% trans "Site" %}</th> <th scope="row">{% trans "Region" %}</th>
<td> <td>
{% display_region object include_site=True %} {% display_region object %}
</td> </td>
</tr> </tr>
<tr>
<th scope="row">{% trans "Site" %}</th>
<td>{{ object.site|linkify|placeholder }}</td>
</tr>
<tr> <tr>
<th scope="row">{% trans "VLAN" %}</th> <th scope="row">{% trans "VLAN" %}</th>
<td> <td>

View File

@ -3,7 +3,7 @@
{% load render_table from django_tables2 %} {% load render_table from django_tables2 %}
{% load plugins %} {% load plugins %}
{% load i18n %} {% load i18n %}
{% load display_region %} {% load mptt %}
{% block content %} {% block content %}
<div class="row"> <div class="row">
@ -15,11 +15,15 @@
<div class="card-body"> <div class="card-body">
<table class="table table-hover attr-table"> <table class="table table-hover attr-table">
<tr> <tr>
<th scope="row">{% trans "Site" %}</th> <th scope="row">{% trans "Region" %}</th>
<td> <td>
{% display_region object include_site=True %} {% display_region object %}
</td> </td>
</tr> </tr>
<tr>
<th scope="row">{% trans "Site" %}</th>
<td>{{ object.site|linkify|placeholder }}</td>
</tr>
<tr> <tr>
<th scope="row">{% trans "Group" %}</th> <th scope="row">{% trans "Group" %}</th>
<td>{{ object.group|linkify|placeholder }}</td> <td>{{ object.group|linkify|placeholder }}</td>