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",