mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-23 07:56:44 -06:00
refactored region hierarchy #13735
This commit is contained in:
parent
da72ab8a7a
commit
f615eec28c
@ -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('—')
|
||||
|
||||
# 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)
|
25
netbox/dcim/templatetags/mptt.py
Normal file
25
netbox/dcim/templatetags/mptt.py
Normal 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('—')
|
||||
|
||||
# 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 ''
|
||||
)
|
@ -5,7 +5,7 @@
|
||||
{% load helpers %}
|
||||
{% load plugins %}
|
||||
{% load i18n %}
|
||||
{% load display_region %}
|
||||
{% load mptt %}
|
||||
|
||||
{% block content %}
|
||||
<div class="row">
|
||||
|
@ -4,7 +4,7 @@
|
||||
{% load static %}
|
||||
{% load plugins %}
|
||||
{% load i18n %}
|
||||
{% load display_region %}
|
||||
{% load mptt %}
|
||||
|
||||
{% block content %}
|
||||
<div class="row">
|
||||
@ -16,11 +16,15 @@
|
||||
<div class="card-body">
|
||||
<table class="table table-hover attr-table">
|
||||
<tr>
|
||||
<th scope="row">{% trans "Site" %}</th>
|
||||
<th scope="row">{% trans "Region" %}</th>
|
||||
<td>
|
||||
{% display_region object include_site=True %}
|
||||
{% display_region object %}
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">{% trans "Site" %}</th>
|
||||
<td>{{ object.site|linkify }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">{% trans "Location" %}</th>
|
||||
<td>
|
||||
|
@ -4,7 +4,7 @@
|
||||
{% load static %}
|
||||
{% load plugins %}
|
||||
{% load i18n %}
|
||||
{% load display_region %}
|
||||
{% load mptt %}
|
||||
|
||||
{% block breadcrumbs %}
|
||||
{{ block.super }}
|
||||
@ -23,11 +23,15 @@
|
||||
<table class="table table-hover attr-table">
|
||||
{% with rack=object.rack %}
|
||||
<tr>
|
||||
<th scope="row">{% trans "Site" %}</th>
|
||||
<th scope="row">{% trans "Region" %}</th>
|
||||
<td>
|
||||
{% display_region rack include_site=True %}
|
||||
{% display_region rack %}
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">{% trans "Site" %}</th>
|
||||
<td>{{ rack.site|linkify }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">{% trans "Location" %}</th>
|
||||
<td>{{ rack.location|linkify|placeholder }}</td>
|
||||
|
@ -3,7 +3,7 @@
|
||||
{% load plugins %}
|
||||
{% load tz %}
|
||||
{% load i18n %}
|
||||
{% load display_region %}
|
||||
{% load mptt %}
|
||||
|
||||
{% block breadcrumbs %}
|
||||
{{ block.super }}
|
||||
|
@ -3,7 +3,7 @@
|
||||
{% load helpers %}
|
||||
{% load plugins %}
|
||||
{% load i18n %}
|
||||
{% load display_region %}
|
||||
{% load mptt %}
|
||||
|
||||
{% block content %}
|
||||
<div class="row">
|
||||
@ -46,11 +46,15 @@
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">{% trans "Site" %}</th>
|
||||
<th scope="row">{% trans "Region" %}</th>
|
||||
<td>
|
||||
{% display_region object include_site=True %}
|
||||
{% display_region object %}
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">{% trans "Site" %}</th>
|
||||
<td>{{ object.site|linkify|placeholder }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">{% trans "VLAN" %}</th>
|
||||
<td>
|
||||
|
@ -3,7 +3,7 @@
|
||||
{% load render_table from django_tables2 %}
|
||||
{% load plugins %}
|
||||
{% load i18n %}
|
||||
{% load display_region %}
|
||||
{% load mptt %}
|
||||
|
||||
{% block content %}
|
||||
<div class="row">
|
||||
@ -15,11 +15,15 @@
|
||||
<div class="card-body">
|
||||
<table class="table table-hover attr-table">
|
||||
<tr>
|
||||
<th scope="row">{% trans "Site" %}</th>
|
||||
<th scope="row">{% trans "Region" %}</th>
|
||||
<td>
|
||||
{% display_region object include_site=True %}
|
||||
{% display_region object %}
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">{% trans "Site" %}</th>
|
||||
<td>{{ object.site|linkify|placeholder }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">{% trans "Group" %}</th>
|
||||
<td>{{ object.group|linkify|placeholder }}</td>
|
||||
|
Loading…
Reference in New Issue
Block a user