瀏覽代碼

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

Jeremy Stretch 5 年之前
父節點
當前提交
f35715683e

+ 12 - 0
docs/additional-features/custom-links.md

@@ -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.
 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
 ## 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.
 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.

+ 4 - 0
docs/release-notes/version-2.9.md

@@ -2,6 +2,10 @@
 
 
 ## v2.9.5 (FUTURE)
 ## 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
 ### Bug Fixes
 
 
 * [#4523](https://github.com/netbox-community/netbox/issues/4523) - Populate site vlan list when bulk editing interfaces under certain circumstances
 * [#4523](https://github.com/netbox-community/netbox/issues/4523) - Populate site vlan list when bulk editing interfaces under certain circumstances

+ 12 - 7
netbox/extras/templatetags/custom_links.py

@@ -20,8 +20,8 @@ GROUP_BUTTON = '<div class="btn-group">\n' \
 GROUP_LINK = '<li><a href="{}"{}>{}</a></li>\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.
     Render all applicable links for the given object.
     """
     """
@@ -30,8 +30,13 @@ def custom_links(obj):
     if not custom_links:
     if not custom_links:
         return ''
         return ''
 
 
-    context = {
+    # Pass select context data when rendering the CustomLink
+    link_context = {
         'obj': obj,
         '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 = ''
     template_code = ''
     group_names = OrderedDict()
     group_names = OrderedDict()
@@ -47,9 +52,9 @@ def custom_links(obj):
         # Add non-grouped links
         # Add non-grouped links
         else:
         else:
             try:
             try:
-                text_rendered = render_jinja2(cl.text, context)
+                text_rendered = render_jinja2(cl.text, link_context)
                 if text_rendered:
                 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 ''
                     link_target = ' target="_blank"' if cl.new_window else ''
                     template_code += LINK_BUTTON.format(
                     template_code += LINK_BUTTON.format(
                         link_rendered, link_target, cl.button_class, text_rendered
                         link_rendered, link_target, cl.button_class, text_rendered
@@ -65,10 +70,10 @@ def custom_links(obj):
 
 
         for cl in links:
         for cl in links:
             try:
             try:
-                text_rendered = render_jinja2(cl.text, context)
+                text_rendered = render_jinja2(cl.text, link_context)
                 if text_rendered:
                 if text_rendered:
                     link_target = ' target="_blank"' if cl.new_window else ''
                     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(
                     links_rendered.append(
                         GROUP_LINK.format(link_rendered, link_target, text_rendered)
                         GROUP_LINK.format(link_rendered, link_target, text_rendered)
                     )
                     )