mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-14 09:51:22 -06:00
Closes #3281: Hide custom links which render as empty text
This commit is contained in:
parent
251ba08e09
commit
5de242fe53
@ -1,5 +1,9 @@
|
|||||||
v2.6.1 (FUTURE)
|
v2.6.1 (FUTURE)
|
||||||
|
|
||||||
|
## Enhancements
|
||||||
|
|
||||||
|
* [#3281](https://github.com/digitalocean/netbox/issues/3281) - Hide custom links which render as empty text
|
||||||
|
|
||||||
## Bug Fixes
|
## Bug Fixes
|
||||||
|
|
||||||
* [#3275](https://github.com/digitalocean/netbox/issues/3275) - Fix error when adding power outlets to a device type
|
* [#3275](https://github.com/digitalocean/netbox/issues/3275) - Fix error when adding power outlets to a device type
|
||||||
|
@ -87,7 +87,8 @@ class CustomLinkForm(forms.ModelForm):
|
|||||||
model = CustomLink
|
model = CustomLink
|
||||||
exclude = []
|
exclude = []
|
||||||
help_texts = {
|
help_texts = {
|
||||||
'text': 'Jinja2 template code for the link text. Reference the object as <code>{{ obj }}</code>.',
|
'text': 'Jinja2 template code for the link text. Reference the object as <code>{{ obj }}</code>. Links '
|
||||||
|
'which render as empty text will not be displayed.',
|
||||||
'url': 'Jinja2 template code for the link URL. Reference the object as <code>{{ obj }}</code>.',
|
'url': 'Jinja2 template code for the link URL. Reference the object as <code>{{ obj }}</code>.',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,7 +15,8 @@ GROUP_BUTTON = '<div class="btn-group">\n' \
|
|||||||
'<button type="button" class="btn btn-sm btn-{} dropdown-toggle" data-toggle="dropdown">\n' \
|
'<button type="button" class="btn btn-sm btn-{} dropdown-toggle" data-toggle="dropdown">\n' \
|
||||||
'{} <span class="caret"></span>\n' \
|
'{} <span class="caret"></span>\n' \
|
||||||
'</button>\n' \
|
'</button>\n' \
|
||||||
'<ul class="dropdown-menu pull-right">\n'
|
'<ul class="dropdown-menu pull-right">\n' \
|
||||||
|
'{}</ul></div>'
|
||||||
GROUP_LINK = '<li><a href="{}"{}>{}</a></li>\n'
|
GROUP_LINK = '<li><a href="{}"{}>{}</a></li>\n'
|
||||||
|
|
||||||
|
|
||||||
@ -35,32 +36,40 @@ def custom_links(obj):
|
|||||||
template_code = ''
|
template_code = ''
|
||||||
group_names = OrderedDict()
|
group_names = OrderedDict()
|
||||||
|
|
||||||
# Organize custom links by group
|
|
||||||
for cl in custom_links:
|
for cl in custom_links:
|
||||||
|
|
||||||
|
# Organize custom links by group
|
||||||
if cl.group_name and cl.group_name in group_names:
|
if cl.group_name and cl.group_name in group_names:
|
||||||
group_names[cl.group_name].append(cl)
|
group_names[cl.group_name].append(cl)
|
||||||
elif cl.group_name:
|
elif cl.group_name:
|
||||||
group_names[cl.group_name] = [cl]
|
group_names[cl.group_name] = [cl]
|
||||||
|
|
||||||
# Add non-grouped links
|
# Add non-grouped links
|
||||||
for cl in custom_links:
|
else:
|
||||||
if not cl.group_name:
|
text_rendered = Environment().from_string(source=cl.text).render(**context)
|
||||||
|
if text_rendered:
|
||||||
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(
|
||||||
cl.url, link_target, cl.button_class, cl.text
|
cl.url, link_target, cl.button_class, text_rendered
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add grouped links to template
|
# Add grouped links to template
|
||||||
for group, links in group_names.items():
|
for group, links in group_names.items():
|
||||||
template_code += GROUP_BUTTON.format(
|
|
||||||
links[0].button_class, group
|
links_rendered = []
|
||||||
)
|
|
||||||
for cl in links:
|
for cl in links:
|
||||||
|
text_rendered = Environment().from_string(source=cl.text).render(**context)
|
||||||
|
if text_rendered:
|
||||||
link_target = ' target="_blank"' if cl.new_window else ''
|
link_target = ' target="_blank"' if cl.new_window else ''
|
||||||
template_code += GROUP_LINK.format(
|
links_rendered.append(
|
||||||
cl.url, link_target, cl.text
|
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 += '</ul>\n</div>\n'
|
|
||||||
|
|
||||||
# Render template
|
# Render template
|
||||||
rendered = Environment().from_string(source=template_code).render(**context)
|
rendered = Environment().from_string(source=template_code).render(**context)
|
||||||
|
Loading…
Reference in New Issue
Block a user