From a0f55f6590daba9dd75668e8a453a043f3ca944b Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Wed, 13 Aug 2025 10:46:17 -0400 Subject: [PATCH] #19735: Fix get_context() for ObjectAction subclasses --- netbox/dcim/object_actions.py | 3 --- netbox/netbox/object_actions.py | 27 ++++++++++++++---------- netbox/utilities/templatetags/buttons.py | 7 +++--- netbox/virtualization/object_actions.py | 3 --- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/netbox/dcim/object_actions.py b/netbox/dcim/object_actions.py index 00a409274..67cb188e8 100644 --- a/netbox/dcim/object_actions.py +++ b/netbox/dcim/object_actions.py @@ -20,10 +20,7 @@ class BulkAddComponents(ObjectAction): @classmethod def get_context(cls, context, obj): return { - 'perms': context.get('perms'), - 'request': context.get('request'), 'formaction': context.get('formaction'), - 'label': cls.label, } diff --git a/netbox/netbox/object_actions.py b/netbox/netbox/object_actions.py index 4bb1630a0..f812c6b40 100644 --- a/netbox/netbox/object_actions.py +++ b/netbox/netbox/object_actions.py @@ -51,13 +51,23 @@ class ObjectAction: return @classmethod - def render(cls, obj, **kwargs): - context = { + def get_context(cls, context, obj): + """ + Return any additional context data needed to render the button. + """ + return {} + + @classmethod + def render(cls, context, obj, **kwargs): + ctx = { + 'perms': context['perms'], + 'request': context['request'], 'url': cls.get_url(obj), 'label': cls.label, + **cls.get_context(context, obj), **kwargs, } - return loader.render_to_string(cls.template_name, context) + return loader.render_to_string(cls.template_name, ctx) class AddObject(ObjectAction): @@ -80,13 +90,10 @@ class CloneObject(ObjectAction): template_name = 'buttons/clone.html' @classmethod - def get_context(cls, context, obj): + def get_url(cls, obj): + url = super().get_url(obj) param_string = prepare_cloned_fields(obj).urlencode() - url = f'{cls.get_url(obj)}?{param_string}' if param_string else None - return { - 'url': url, - 'label': cls.label, - } + return f'{url}?{param_string}' if param_string else None class EditObject(ObjectAction): @@ -142,8 +149,6 @@ class BulkExport(ObjectAction): export_templates = ExportTemplate.objects.restrict(user, 'view').filter(object_types=object_type) return { - 'label': cls.label, - 'perms': context['perms'], 'object_type': object_type, 'url_params': context['request'].GET.urlencode() if context['request'].GET else '', 'export_templates': export_templates, diff --git a/netbox/utilities/templatetags/buttons.py b/netbox/utilities/templatetags/buttons.py index 00ce879a9..d6776d727 100644 --- a/netbox/utilities/templatetags/buttons.py +++ b/netbox/utilities/templatetags/buttons.py @@ -28,10 +28,11 @@ __all__ = ( register = template.Library() -@register.simple_tag -def action_buttons(actions, obj, multi=False, **kwargs): +@register.simple_tag(takes_context=True) +def action_buttons(context, actions, obj, multi=False, **kwargs): buttons = [ - action.render(obj, **kwargs) for action in actions if action.multi == multi + action.render(context, obj, **kwargs) + for action in actions if action.multi == multi ] return mark_safe(''.join(buttons)) diff --git a/netbox/virtualization/object_actions.py b/netbox/virtualization/object_actions.py index 0f248b4e4..c6c6886c3 100644 --- a/netbox/virtualization/object_actions.py +++ b/netbox/virtualization/object_actions.py @@ -19,8 +19,5 @@ class BulkAddComponents(ObjectAction): @classmethod def get_context(cls, context, obj): return { - 'perms': context.get('perms'), - 'request': context.get('request'), 'formaction': context.get('formaction'), - 'label': cls.label, }