|
|
@@ -6,6 +6,11 @@ from django.http import HttpResponseRedirect
|
|
6
|
6
|
|
|
7
|
7
|
|
|
8
|
8
|
class BulkEditMixin():
|
|
|
9
|
+ """
|
|
|
10
|
+ Mixin for views that have a bulk editing facility. This is normally in the
|
|
|
11
|
+ form of tabular data where each row has a checkbox. The UI allows a number
|
|
|
12
|
+ of rows to be selected and then some 'action' to be performed on them.
|
|
|
13
|
+ """
|
|
9
|
14
|
actions = None
|
|
10
|
15
|
current_view = None
|
|
11
|
16
|
checkbox_object_name = None
|
|
|
@@ -20,13 +25,16 @@ class BulkEditMixin():
|
|
20
|
25
|
return None
|
|
21
|
26
|
|
|
22
|
27
|
def post(self, request, *args, **kwargs):
|
|
|
28
|
+ # Dynamic dispatch patter - we forward POST requests onto a method
|
|
|
29
|
+ # designated by the 'action' parameter. The action has to be in a
|
|
|
30
|
+ # whitelist to avoid security issues.
|
|
23
|
31
|
action = request.POST.get('action', '').lower()
|
|
24
|
32
|
if not self.actions or action not in self.actions:
|
|
25
|
33
|
messages.error(self.request, "Invalid action")
|
|
26
|
34
|
return HttpResponseRedirect(reverse(self.current_view))
|
|
27
|
35
|
ids = request.POST.getlist('selected_%s' % self.get_checkbox_object_name())
|
|
28
|
36
|
if not ids:
|
|
29
|
|
- messages.error(self.request, "You need to select some %s" % self.get_checkbox_object_name())
|
|
|
37
|
+ messages.error(self.request, "You need to select some %ss" % self.get_checkbox_object_name())
|
|
30
|
38
|
return HttpResponseRedirect(reverse(self.current_view))
|
|
31
|
39
|
|
|
32
|
40
|
raw_objects = self.model.objects.in_bulk(ids)
|