Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add admin actions to change form page in all models #905

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions froide/helper/templates/admin/change_form.html
Original file line number Diff line number Diff line change
@@ -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 %}
<form class="submit-row"
action="{% url opts|admin_urlname:'changelist' %}"
method="post">
{% csrf_token %}
{{ action_form }}
<input type="submit" value="{% translate "Go" %}">
</form>
{% endif %}
{% endif %}
{% endblock object-tools %}
26 changes: 26 additions & 0 deletions froide/helper/templatetags/admin_action_helper.py
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions froide/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ 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",
"django.contrib.sitemaps",
"django.contrib.humanize",
"django.contrib.gis",
"channels",
# overwrite management command in
# django_elasticsearch_dsl
"froide.helper",
# external
"django_elasticsearch_dsl",
"taggit",
Expand Down