diff --git a/netbox/netbox/object_actions.py b/netbox/netbox/object_actions.py index 73315ce4c..7a21739b9 100644 --- a/netbox/netbox/object_actions.py +++ b/netbox/netbox/object_actions.py @@ -1,3 +1,4 @@ +from django.template import loader from django.urls import reverse from django.urls.exceptions import NoReverseMatch from django.utils.translation import gettext as _ @@ -27,12 +28,14 @@ class ObjectAction: Params: name: The action name appended to the module for view resolution label: Human-friendly label for the rendered button + template_name: Name of the HTML template which renders the button multi: Set to True if this action is performed by selecting multiple objects (i.e. using a table) permissions_required: The set of permissions a user must have to perform the action url_kwargs: The set of URL keyword arguments to pass when resolving the view's URL """ name = '' label = None + template_name = None multi = False permissions_required = set() url_kwargs = [] @@ -49,11 +52,13 @@ class ObjectAction: return @classmethod - def get_context(cls, context, obj): - return { + def render(cls, obj, **kwargs): + context = { 'url': cls.get_url(obj), 'label': cls.label, + **kwargs, } + return loader.render_to_string(cls.template_name, context) class AddObject(ObjectAction): diff --git a/netbox/templates/core/buttons/bulk_sync.html b/netbox/templates/core/buttons/bulk_sync.html index e92ad15df..e70b3a459 100644 --- a/netbox/templates/core/buttons/bulk_sync.html +++ b/netbox/templates/core/buttons/bulk_sync.html @@ -1,3 +1,3 @@ - diff --git a/netbox/templates/dcim/buttons/bulk_disconnect.html b/netbox/templates/dcim/buttons/bulk_disconnect.html index 9ab53472b..0c9a5fc16 100644 --- a/netbox/templates/dcim/buttons/bulk_disconnect.html +++ b/netbox/templates/dcim/buttons/bulk_disconnect.html @@ -1,3 +1,3 @@ - diff --git a/netbox/templates/generic/object_children.html b/netbox/templates/generic/object_children.html index b9eabdc9d..da3ce9814 100644 --- a/netbox/templates/generic/object_children.html +++ b/netbox/templates/generic/object_children.html @@ -35,7 +35,7 @@ Context:
{% block bulk_controls %} - {% action_buttons actions model multi=True %} + {% action_buttons actions model multi=True return_url=request.path %} {% block bulk_extra_controls %}{% endblock %} {% endblock bulk_controls %}
diff --git a/netbox/utilities/templates/buttons/bulk_delete.html b/netbox/utilities/templates/buttons/bulk_delete.html index 42dd7ce30..64673d53c 100644 --- a/netbox/utilities/templates/buttons/bulk_delete.html +++ b/netbox/utilities/templates/buttons/bulk_delete.html @@ -1,3 +1,3 @@ - diff --git a/netbox/utilities/templates/buttons/bulk_edit.html b/netbox/utilities/templates/buttons/bulk_edit.html index bc50d9b6e..78fc8070d 100644 --- a/netbox/utilities/templates/buttons/bulk_edit.html +++ b/netbox/utilities/templates/buttons/bulk_edit.html @@ -1,3 +1,3 @@ - diff --git a/netbox/utilities/templates/buttons/bulk_rename.html b/netbox/utilities/templates/buttons/bulk_rename.html index 6e268cc62..b7d3d7c3b 100644 --- a/netbox/utilities/templates/buttons/bulk_rename.html +++ b/netbox/utilities/templates/buttons/bulk_rename.html @@ -1,5 +1,5 @@ {% if url %} - {% endif %} diff --git a/netbox/utilities/templatetags/buttons.py b/netbox/utilities/templatetags/buttons.py index 404386910..1b43fa395 100644 --- a/netbox/utilities/templatetags/buttons.py +++ b/netbox/utilities/templatetags/buttons.py @@ -1,6 +1,5 @@ from django import template from django.contrib.contenttypes.models import ContentType -from django.template import loader from django.urls import NoReverseMatch, reverse from django.utils.safestring import mark_safe from django.utils.translation import gettext as _ @@ -29,11 +28,10 @@ __all__ = ( register = template.Library() -@register.simple_tag(takes_context=True) -def action_buttons(context, actions, obj, multi=False): +@register.simple_tag +def action_buttons(actions, obj, multi=False, **kwargs): buttons = [ - loader.render_to_string(action.template_name, action.get_context(context, obj)) - for action in actions if action.multi == multi + action.render(obj, **kwargs) for action in actions if action.multi == multi ] return mark_safe(''.join(buttons))