-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
36 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('<a href="{}">{}</a>'.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 = [ | ||
'<a href="{}">{}</a>'.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([ | ||
'<a href="{}">{}</a>'.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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters