Skip to content

Commit 583f67c

Browse files
committed
Optionally remember last action and allow disabling default message
Bump version from 0.5.4 -> 0.6.0
1 parent 0368a77 commit 583f67c

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ class MyModelAdmin(ChangeFormActionsMixin, admin.ModelAdmin):
5252
actions = [...]
5353
```
5454

55+
## Django Settings
56+
57+
```py
58+
# Whether to display an `Action: <name>` success message after running
59+
# an action. Disable this if you want to display no message or a custom
60+
# message, e.g. as part of each action.
61+
CHANGEFORM_ACTIONS_ENABLE_DEFAULT_MESSAGE: bool = True
62+
63+
# Enable this to preselect the last action in the dropdown, which is
64+
# helpful to repeatedly run the same action several times.
65+
CHANGEFORM_ACTIONS_REMEMBER_LAST_ACTION: bool = False
66+
```
67+
5568
## Development Setup
5669

5770
This project uses [uv](https://docs.astral.sh/uv/) to handle python versions and dependencies.

changeform_actions/admin.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Optional, Tuple
22

3+
from django.conf import settings
34
from django.template.context import ContextDict
45
from django.template.loader import render_to_string
56
from django.urls import reverse
@@ -84,6 +85,13 @@ def get_changeform_actions_dropdown(self, request, context) -> str:
8485
action_form = ActionForm(auto_id=None)
8586
action_form.fields["action"].choices = action_choices
8687

88+
if getattr(settings, "CHANGEFORM_ACTIONS_REMEMBER_LAST_ACTION", False):
89+
last_action: Optional[str] = request.session.get(
90+
"changeform_actions_last_action", None
91+
)
92+
if last_action and last_action in dict(action_choices):
93+
action_form.initial["action"] = last_action
94+
8795
return render_to_string(
8896
"admin/actions_for_changeform.html",
8997
request=request,

changeform_actions/views.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.apps import apps
2+
from django.conf import settings
23
from django.contrib import admin
34
from django.contrib.admin.views.decorators import staff_member_required
45
from django.db.models import QuerySet
@@ -57,6 +58,14 @@ def run_action_for_model_instance(request):
5758
for action, name, label in model_admin._get_base_actions():
5859
if name == action_name:
5960
action(model_admin, request, queryset)
60-
model_admin.message_user(request, _("Action:") + f" {label}")
61+
62+
if getattr(settings, "CHANGEFORM_ACTIONS_ENABLE_DEFAULT_MESSAGE", True):
63+
model_admin.message_user(request, _("Action:") + f" {label}")
64+
65+
if (
66+
getattr(settings, "CHANGEFORM_ACTIONS_REMEMBER_LAST_ACTION", False)
67+
and request.session
68+
):
69+
request.session["changeform_actions_last_action"] = action_name
6170

6271
return HttpResponseRedirect(referer_url)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "django-admin-changeform-actions"
33
authors = [
44
{ name="Christoph Buelter", email="buelter.christoph@gmail.com" },
55
]
6-
version = "0.5.4"
6+
version = "0.6.0"
77
license = "MIT"
88
description = "Reuse model admin changelist actions on changeform pages"
99
readme = "README.md"

0 commit comments

Comments
 (0)