Skip to content

Adds region hierarchy in templates #14213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
37 changes: 37 additions & 0 deletions netbox/dcim/templatetags/display_region.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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)
10 changes: 2 additions & 8 deletions netbox/templates/dcim/device.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{% load helpers %}
{% load plugins %}
{% load i18n %}
{% load display_region %}

{% block content %}
<div class="row">
Expand All @@ -16,14 +17,7 @@ <h5 class="card-header">{% trans "Device" %}</h5>
<tr>
<th scope="row">{% trans "Region" %}</th>
<td>
{% 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 %}
</td>
</tr>
<tr>
Expand Down
6 changes: 2 additions & 4 deletions netbox/templates/dcim/rack.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{% load static %}
{% load plugins %}
{% load i18n %}
{% load display_region %}

{% block content %}
<div class="row">
Expand All @@ -17,10 +18,7 @@ <h5 class="card-header">
<tr>
<th scope="row">{% trans "Site" %}</th>
<td>
{% if object.site.region %}
{{ object.site.region|linkify }} /
{% endif %}
{{ object.site|linkify }}
{% display_region object include_site=True %}
</td>
</tr>
<tr>
Expand Down
6 changes: 2 additions & 4 deletions netbox/templates/dcim/rackreservation.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{% load static %}
{% load plugins %}
{% load i18n %}
{% load display_region %}

{% block breadcrumbs %}
{{ block.super }}
Expand All @@ -24,10 +25,7 @@ <h5 class="card-header">
<tr>
<th scope="row">{% trans "Site" %}</th>
<td>
{% if rack.site.region %}
{{ rack.site.region|linkify }} /
{% endif %}
{{ rack.site|linkify }}
{% display_region rack include_site=True %}
</td>
</tr>
<tr>
Expand Down
10 changes: 2 additions & 8 deletions netbox/templates/dcim/site.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{% load plugins %}
{% load tz %}
{% load i18n %}
{% load display_region %}

{% block breadcrumbs %}
{{ block.super }}
Expand All @@ -29,14 +30,7 @@ <h5 class="card-header">{% trans "Site" %}</h5>
<tr>
<th scope="row">{% trans "Region" %}</th>
<td>
{% if object.region %}
{% for region in object.region.get_ancestors %}
{{ region|linkify }} /
{% endfor %}
{{ object.region|linkify }}
{% else %}
{{ ''|placeholder }}
{% endif %}
{% display_region object %}
</td>
</tr>
<tr>
Expand Down
10 changes: 2 additions & 8 deletions netbox/templates/ipam/prefix.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{% load helpers %}
{% load plugins %}
{% load i18n %}
{% load display_region %}

{% block content %}
<div class="row">
Expand Down Expand Up @@ -47,14 +48,7 @@ <h5 class="card-header">{% trans "Prefix" %}</h5>
<tr>
<th scope="row">{% trans "Site" %}</th>
<td>
{% if object.site %}
{% if object.site.region %}
{{ object.site.region|linkify }} /
{% endif %}
{{ object.site|linkify }}
{% else %}
{{ ''|placeholder }}
{% endif %}
{% display_region object include_site=True %}
</td>
</tr>
<tr>
Expand Down
10 changes: 2 additions & 8 deletions netbox/templates/ipam/vlan.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{% load render_table from django_tables2 %}
{% load plugins %}
{% load i18n %}
{% load display_region %}

{% block content %}
<div class="row">
Expand All @@ -16,14 +17,7 @@ <h5 class="card-header">
<tr>
<th scope="row">{% trans "Site" %}</th>
<td>
{% if object.site %}
{% if object.site.region %}
{{ object.site.region|linkify }} /
{% endif %}
{{ object.site|linkify }}
{% else %}
{{ ''|placeholder }}
{% endif %}
{% display_region object include_site=True %}
</td>
</tr>
<tr>
Expand Down