From 5de242fe5357cb4791eaef6b090f602712a20a61 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 24 Jun 2019 12:20:09 -0400 Subject: [PATCH] Closes #3281: Hide custom links which render as empty text --- CHANGELOG.md | 4 +++ netbox/extras/admin.py | 3 +- netbox/extras/templatetags/custom_links.py | 41 +++++++++++++--------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee48d4882..0c9b70a91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ v2.6.1 (FUTURE) +## Enhancements + +* [#3281](https://github.com/digitalocean/netbox/issues/3281) - Hide custom links which render as empty text + ## Bug Fixes * [#3275](https://github.com/digitalocean/netbox/issues/3275) - Fix error when adding power outlets to a device type diff --git a/netbox/extras/admin.py b/netbox/extras/admin.py index a29d0df09..d93b04037 100644 --- a/netbox/extras/admin.py +++ b/netbox/extras/admin.py @@ -87,7 +87,8 @@ class CustomLinkForm(forms.ModelForm): model = CustomLink exclude = [] help_texts = { - 'text': 'Jinja2 template code for the link text. Reference the object as {{ obj }}.', + 'text': 'Jinja2 template code for the link text. Reference the object as {{ obj }}. Links ' + 'which render as empty text will not be displayed.', 'url': 'Jinja2 template code for the link URL. Reference the object as {{ obj }}.', } diff --git a/netbox/extras/templatetags/custom_links.py b/netbox/extras/templatetags/custom_links.py index 193c465a5..ce6cc482a 100644 --- a/netbox/extras/templatetags/custom_links.py +++ b/netbox/extras/templatetags/custom_links.py @@ -15,7 +15,8 @@ GROUP_BUTTON = '
\n' \ '\n' \ - '
' GROUP_LINK = '
  • {}
  • \n' @@ -35,32 +36,40 @@ def custom_links(obj): template_code = '' group_names = OrderedDict() - # Organize custom links by group for cl in custom_links: + + # Organize custom links by group if cl.group_name and cl.group_name in group_names: group_names[cl.group_name].append(cl) elif cl.group_name: group_names[cl.group_name] = [cl] - # Add non-grouped links - for cl in custom_links: - if not cl.group_name: - link_target = ' target="_blank"' if cl.new_window else '' - template_code += LINK_BUTTON.format( - cl.url, link_target, cl.button_class, cl.text - ) + # Add non-grouped links + else: + text_rendered = Environment().from_string(source=cl.text).render(**context) + if text_rendered: + link_target = ' target="_blank"' if cl.new_window else '' + template_code += LINK_BUTTON.format( + cl.url, link_target, cl.button_class, text_rendered + ) # Add grouped links to template for group, links in group_names.items(): - template_code += GROUP_BUTTON.format( - links[0].button_class, group - ) + + links_rendered = [] + for cl in links: - link_target = ' target="_blank"' if cl.new_window else '' - template_code += GROUP_LINK.format( - cl.url, link_target, cl.text + text_rendered = Environment().from_string(source=cl.text).render(**context) + if text_rendered: + link_target = ' target="_blank"' if cl.new_window else '' + links_rendered.append( + GROUP_LINK.format(cl.url, link_target, cl.text) + ) + + if links_rendered: + template_code += GROUP_BUTTON.format( + links[0].button_class, group, ''.join(links_rendered) ) - template_code += '\n\n' # Render template rendered = Environment().from_string(source=template_code).render(**context)