Skip to content

Commit 30e0484

Browse files
committed
New perform_action for admin
1 parent 1593d4a commit 30e0484

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

bolt-admin/bolt/admin/assets/admin/list.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
jQuery(function ($) {
22
var $actionCheckbox = $("[data-action-checkbox]");
33
var $actionPks = $('[name="action_pks"]');
4-
var $actionSelect = $('[name="action_key"]');
4+
var $actionSelect = $('[name="action_name"]');
55
var $actionSubmit = $('[data-actions-form] [type="submit"]');
66

77
$actionCheckbox.on("change", function () {

bolt-admin/bolt/admin/templates/admin/list.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
{% if actions %}
2929
<form method="POST" data-actions-form>
3030
{{ csrf_input }}
31-
<select name="action_key">
31+
<select name="action_name" class="text-sm border-gray-200 rounded-md">
3232
<option value="">Actions</option>
3333
{% for action in actions %}
3434
<option>{{ action }}</option>

bolt-admin/bolt/admin/views/base.py

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import TYPE_CHECKING
22

33
from bolt.db import models
4-
from bolt.http import HttpResponse
4+
from bolt.http import HttpResponse, HttpResponseRedirect
55
from bolt.paginator import Paginator
66
from bolt.urls import reverse
77
from bolt.utils.text import slugify
@@ -117,8 +117,8 @@ def render_card(self, card: "Card"):
117117

118118
class AdminListView(AdminView):
119119
template_name = "admin/list.html"
120-
fields: list
121-
actions: dict[str] = {}
120+
fields: list[str]
121+
actions: list[str] = []
122122
filters: list[str] = []
123123
page_size = 100
124124
show_search = False
@@ -154,16 +154,23 @@ def get_context(self):
154154
return context
155155

156156
def post(self) -> HttpResponse:
157-
action_key = self.request.POST.get("action_key")
157+
# won't be "key" anymore, just list
158+
action_name = self.request.POST.get("action_name")
158159
actions = self.get_actions()
159-
if action_key and action_key in actions:
160-
action_callable = actions[action_key]
161-
if isinstance(action_callable, str):
162-
action_callable = getattr(self, action_callable)
163-
return action_callable(self.request.POST.getlist("action_pks"))
160+
if action_name and action_name in actions:
161+
target_pks = self.request.POST["action_pks"].split(",")
162+
response = self.perform_action(action_name, target_pks)
163+
if response:
164+
return response
165+
else:
166+
# message in session first
167+
return HttpResponseRedirect(".")
164168

165169
raise ValueError("Invalid action")
166170

171+
def perform_action(self, action: str, target_pks: list) -> HttpResponse | None:
172+
raise NotImplementedError
173+
167174
def get_objects(self) -> list:
168175
return []
169176

0 commit comments

Comments
 (0)