From 0920a9c248aab13a6cb2b15e0d846431ccc8c725 Mon Sep 17 00:00:00 2001 From: Stefan Wehrmeyer Date: Tue, 19 Nov 2024 13:58:02 +0100 Subject: [PATCH] Add admin actions to change form page in all models --- .../helper/templates/admin/change_form.html | 17 ++++++++++++ .../templatetags/admin_action_helper.py | 26 +++++++++++++++++++ froide/settings.py | 6 ++--- 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 froide/helper/templates/admin/change_form.html create mode 100644 froide/helper/templatetags/admin_action_helper.py diff --git a/froide/helper/templates/admin/change_form.html b/froide/helper/templates/admin/change_form.html new file mode 100644 index 000000000..bef872f88 --- /dev/null +++ b/froide/helper/templates/admin/change_form.html @@ -0,0 +1,17 @@ +{% extends "admin/change_form.html" %} +{% load i18n admin_urls admin_action_helper %} +{% block object-tools %} + {{ block.super }} + {% if not add %} + {% render_admin_action_form adminform.model_admin original as action_form %} + {% if action_form %} +
+ {% csrf_token %} + {{ action_form }} + +
+ {% endif %} + {% endif %} +{% endblock object-tools %} diff --git a/froide/helper/templatetags/admin_action_helper.py b/froide/helper/templatetags/admin_action_helper.py new file mode 100644 index 000000000..f0c4813a7 --- /dev/null +++ b/froide/helper/templatetags/admin_action_helper.py @@ -0,0 +1,26 @@ +from django import forms, template +from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME + +register = template.Library() + + +class ActionForm(forms.Form): + action = forms.ChoiceField() + + def __init__(self, *args, **kwargs): + actions = kwargs.pop("actions") + obj_id = kwargs.pop("object_id") + super().__init__(*args, **kwargs) + self.fields["action"].choices = actions + self.fields[ACTION_CHECKBOX_NAME] = forms.CharField(widget=forms.HiddenInput) + self.fields[ACTION_CHECKBOX_NAME].initial = obj_id + + +@register.simple_tag(takes_context=True) +def render_admin_action_form(context, model_admin, obj): + request = context["request"] + choices = model_admin.get_action_choices(request) + if len(choices) < 3: # ignore blank choice and default delete action + return + form = ActionForm(object_id=obj.id, actions=choices) + return form diff --git a/froide/settings.py b/froide/settings.py index cf9aafa7b..0f8c0e7c2 100644 --- a/froide/settings.py +++ b/froide/settings.py @@ -26,6 +26,9 @@ class Base(Configuration): "django.contrib.sites", "django.contrib.messages", "django.contrib.staticfiles", + # overwrite admin templates and + # management command in django_elasticsearch_dsl + "froide.helper", "django.contrib.admin", "django_comments", "django.contrib.flatpages", @@ -33,9 +36,6 @@ class Base(Configuration): "django.contrib.humanize", "django.contrib.gis", "channels", - # overwrite management command in - # django_elasticsearch_dsl - "froide.helper", # external "django_elasticsearch_dsl", "taggit",