Fixes #9387: Ensure ActionsColumn extra_buttons are always displayed

This commit is contained in:
jeremystretch 2022-05-24 09:14:25 -04:00
parent a52c68f4c2
commit f03c5037c4
2 changed files with 16 additions and 12 deletions

View File

@ -13,6 +13,7 @@
* [#9094](https://github.com/netbox-community/netbox/issues/9094) - Fix partial address search within Prefix and Aggregate filters * [#9094](https://github.com/netbox-community/netbox/issues/9094) - Fix partial address search within Prefix and Aggregate filters
* [#9358](https://github.com/netbox-community/netbox/issues/9358) - Annotate circuit count for providers list under ASN view * [#9358](https://github.com/netbox-community/netbox/issues/9358) - Annotate circuit count for providers list under ASN view
* [#9387](https://github.com/netbox-community/netbox/issues/9387) - Ensure ActionsColumn `extra_buttons` are always displayed
--- ---

View File

@ -192,32 +192,35 @@ class ActionsColumn(tables.Column):
model = table.Meta.model model = table.Meta.model
request = getattr(table, 'context', {}).get('request') request = getattr(table, 'context', {}).get('request')
url_appendix = f'?return_url={request.path}' if request else '' url_appendix = f'?return_url={request.path}' if request else ''
html = ''
# Compile actions menu
links = [] links = []
user = getattr(request, 'user', AnonymousUser()) user = getattr(request, 'user', AnonymousUser())
for action, attrs in self.actions.items(): for action, attrs in self.actions.items():
permission = f'{model._meta.app_label}.{attrs.permission}_{model._meta.model_name}' permission = f'{model._meta.app_label}.{attrs.permission}_{model._meta.model_name}'
if attrs.permission is None or user.has_perm(permission): if attrs.permission is None or user.has_perm(permission):
url = reverse(get_viewname(model, action), kwargs={'pk': record.pk}) url = reverse(get_viewname(model, action), kwargs={'pk': record.pk})
links.append(f'<li><a class="dropdown-item" href="{url}{url_appendix}">' links.append(
f'<i class="mdi mdi-{attrs.icon}"></i> {attrs.title}</a></li>') f'<li><a class="dropdown-item" href="{url}{url_appendix}">'
f'<i class="mdi mdi-{attrs.icon}"></i> {attrs.title}</a></li>'
if not links: )
return '' if links:
html += (
menu = f'<span class="dropdown">' \ f'<span class="dropdown">'
f'<a class="btn btn-sm btn-secondary dropdown-toggle" href="#" type="button" data-bs-toggle="dropdown">' \ f'<a class="btn btn-sm btn-secondary dropdown-toggle" href="#" type="button" data-bs-toggle="dropdown">'
f'<i class="mdi mdi-wrench"></i></a>' \ f'<i class="mdi mdi-wrench"></i></a>'
f'<ul class="dropdown-menu">{"".join(links)}</ul></span>' f'<ul class="dropdown-menu">{"".join(links)}</ul></span>'
)
# Render any extra buttons from template code # Render any extra buttons from template code
if self.extra_buttons: if self.extra_buttons:
template = Template(self.extra_buttons) template = Template(self.extra_buttons)
context = getattr(table, "context", Context()) context = getattr(table, "context", Context())
context.update({'record': record}) context.update({'record': record})
menu = template.render(context) + menu html = template.render(context) + html
return mark_safe(menu) return mark_safe(html)
class ChoiceFieldColumn(tables.Column): class ChoiceFieldColumn(tables.Column):