-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
084a5c5
initial work to render hierarchical region #13735
abhi1693 8b1e185
adds site display #13735
abhi1693 28fc655
cleanup #13735
abhi1693 da72ab8
adds display region tag #13735
abhi1693 f615eec
refactored region hierarchy #13735
abhi1693 d974495
refactored region hierarchy #13735
abhi1693 2ea37ad
renamed display_region to nested_tree #13735
abhi1693 01c9cc0
Make render_tree suitable for generic use
jeremystretch 292202a
Remove errant item from __all__
jeremystretch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or 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,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('—') | ||
|
||
# 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) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.