Closes #5202: Extend the available context data when rendering custom links

This commit is contained in:
Jeremy Stretch 2020-10-09 12:36:09 -04:00
parent b5a65bc66c
commit f35715683e
3 changed files with 28 additions and 7 deletions

View File

@ -17,6 +17,18 @@ When viewing a device named Router4, this link would render as:
Custom links appear as buttons at the top right corner of the page. Numeric weighting can be used to influence the ordering of links.
## Context Data
The following context data is available within the template when rendering a custom link's text or URL.
| Variable | Description |
|----------|-------------|
| `obj` | The NetBox object being displayed |
| `debug` | A boolean indicating whether debugging is enabled |
| `request` | The current WSGI request |
| `user` | The current user (if authenticated) |
| `perms` | The [permissions](https://docs.djangoproject.com/en/stable/topics/auth/default/#permissions) assigned to the user |
## Conditional Rendering
Only links which render with non-empty text are included on the page. You can employ conditional Jinja2 logic to control the conditions under which a link gets rendered.

View File

@ -2,6 +2,10 @@
## v2.9.5 (FUTURE)
### Enhancements
* [#5202](https://github.com/netbox-community/netbox/issues/5202) - Extend the available context data when rendering custom links
### Bug Fixes
* [#4523](https://github.com/netbox-community/netbox/issues/4523) - Populate site vlan list when bulk editing interfaces under certain circumstances

View File

@ -20,8 +20,8 @@ GROUP_BUTTON = '<div class="btn-group">\n' \
GROUP_LINK = '<li><a href="{}"{}>{}</a></li>\n'
@register.simple_tag()
def custom_links(obj):
@register.simple_tag(takes_context=True)
def custom_links(context, obj):
"""
Render all applicable links for the given object.
"""
@ -30,8 +30,13 @@ def custom_links(obj):
if not custom_links:
return ''
context = {
# Pass select context data when rendering the CustomLink
link_context = {
'obj': obj,
'debug': context['debug'], # django.template.context_processors.debug
'request': context['request'], # django.template.context_processors.request
'user': context['user'], # django.contrib.auth.context_processors.auth
'perms': context['perms'], # django.contrib.auth.context_processors.auth
}
template_code = ''
group_names = OrderedDict()
@ -47,9 +52,9 @@ def custom_links(obj):
# Add non-grouped links
else:
try:
text_rendered = render_jinja2(cl.text, context)
text_rendered = render_jinja2(cl.text, link_context)
if text_rendered:
link_rendered = render_jinja2(cl.url, context)
link_rendered = render_jinja2(cl.url, link_context)
link_target = ' target="_blank"' if cl.new_window else ''
template_code += LINK_BUTTON.format(
link_rendered, link_target, cl.button_class, text_rendered
@ -65,10 +70,10 @@ def custom_links(obj):
for cl in links:
try:
text_rendered = render_jinja2(cl.text, context)
text_rendered = render_jinja2(cl.text, link_context)
if text_rendered:
link_target = ' target="_blank"' if cl.new_window else ''
link_rendered = render_jinja2(cl.url, context)
link_rendered = render_jinja2(cl.url, link_context)
links_rendered.append(
GROUP_LINK.format(link_rendered, link_target, text_rendered)
)