-
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.
initial work to render hierarchical region #13735
- Loading branch information
Showing
4 changed files
with
43 additions
and
16 deletions.
There are no files selected for viewing
Empty file.
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from django import template | ||
from django.utils.safestring import mark_safe | ||
|
||
from dcim.models import Site | ||
|
||
register = template.Library() | ||
|
||
|
||
@register.simple_tag(takes_context=True) | ||
def display_region(context, obj): | ||
""" | ||
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('—') | ||
|
||
# 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 | ||
|
||
# Retrieve all regions in the hierarchy | ||
regions = region.get_ancestors(include_self=True) | ||
|
||
# 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 | ||
]) | ||
) |
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
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