Parcourir la source

Adjust generic bulkedit mixin to work with public-facing views

master
David Winterbottom il y a 13 ans
Parent
révision
d8e026010f
1 fichiers modifiés avec 23 ajouts et 9 suppressions
  1. 23
    9
      oscar/views/generic.py

+ 23
- 9
oscar/views/generic.py Voir le fichier

@@ -7,8 +7,8 @@ from django.utils.translation import ugettext_lazy as _
7 7
 class PostActionMixin(object):
8 8
     """
9 9
     Simple mixin to forward POST request that contain a key 'action'
10
-    onto a method of form "do_{action}".  
11
-    
10
+    onto a method of form "do_{action}".
11
+
12 12
     This only works with DetailView
13 13
     """
14 14
     def post(self, request, *args, **kwargs):
@@ -23,7 +23,7 @@ class PostActionMixin(object):
23 23
             else:
24 24
                 messages.error(request, _("Invalid form submission"))
25 25
         return super(PostActionMixin, self).post(request, *args, **kwargs)
26
-    
26
+
27 27
 
28 28
 class BulkEditMixin(object):
29 29
     """
@@ -32,19 +32,24 @@ class BulkEditMixin(object):
32 32
     of rows to be selected and then some 'action' to be performed on them.
33 33
     """
34 34
     action_param = 'action'
35
+
36
+    # Permitted methods that can be used to act on the selected objects
35 37
     actions = None
36 38
     current_view = None
37 39
     checkbox_object_name = None
38 40
 
39 41
     def get_checkbox_object_name(self):
40
-        object_list = self.get_queryset()
41 42
         if self.checkbox_object_name:
42 43
             return self.checkbox_object_name
43
-        elif hasattr(object_list, 'model'):
44
+        object_list = self.get_queryset()
45
+        if hasattr(object_list, 'model'):
44 46
             return smart_str(object_list.model._meta.object_name.lower())
45 47
         else:
46 48
             return None
47 49
 
50
+    def get_queryset(self):
51
+        pass
52
+
48 53
     def get_error_url(self, request):
49 54
         return request.META['HTTP_REFERER']
50 55
 
@@ -60,12 +65,21 @@ class BulkEditMixin(object):
60 65
             messages.error(self.request, _("Invalid action"))
61 66
             return HttpResponseRedirect(self.get_error_url(request))
62 67
 
63
-        ids = request.POST.getlist('selected_%s' % self.get_checkbox_object_name())
68
+        ids = request.POST.getlist(
69
+            'selected_%s' % self.get_checkbox_object_name())
70
+        ids = map(int, ids)
64 71
         if not ids:
65
-            messages.error(self.request, _("You need to select some %ss") % self.get_checkbox_object_name())
72
+            messages.error(
73
+                self.request, _("You need to select some %ss") % self.get_checkbox_object_name())
66 74
             return HttpResponseRedirect(self.get_error_url(request))
67 75
 
68
-        raw_objects = self.model.objects.in_bulk(ids)
69
-        objects = [raw_objects[int(id)] for id in ids]
76
+        objects = self.get_objects(ids)
70 77
         return getattr(self, action)(request, objects)
71 78
 
79
+    def get_objects(self, ids):
80
+        object_dict = self.get_object_dict(ids)
81
+        # Rearrange back into the original order
82
+        return [object_dict[id] for id in ids]
83
+
84
+    def get_object_dict(self, ids):
85
+        return self.model.objects.in_bulk(ids)

Chargement…
Annuler
Enregistrer