Ver código fonte

Adjust generic bulkedit mixin to work with public-facing views

master
David Winterbottom 13 anos atrás
pai
commit
d8e026010f
1 arquivos alterados com 23 adições e 9 exclusões
  1. 23
    9
      oscar/views/generic.py

+ 23
- 9
oscar/views/generic.py Ver arquivo

7
 class PostActionMixin(object):
7
 class PostActionMixin(object):
8
     """
8
     """
9
     Simple mixin to forward POST request that contain a key 'action'
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
     This only works with DetailView
12
     This only works with DetailView
13
     """
13
     """
14
     def post(self, request, *args, **kwargs):
14
     def post(self, request, *args, **kwargs):
23
             else:
23
             else:
24
                 messages.error(request, _("Invalid form submission"))
24
                 messages.error(request, _("Invalid form submission"))
25
         return super(PostActionMixin, self).post(request, *args, **kwargs)
25
         return super(PostActionMixin, self).post(request, *args, **kwargs)
26
-    
26
+
27
 
27
 
28
 class BulkEditMixin(object):
28
 class BulkEditMixin(object):
29
     """
29
     """
32
     of rows to be selected and then some 'action' to be performed on them.
32
     of rows to be selected and then some 'action' to be performed on them.
33
     """
33
     """
34
     action_param = 'action'
34
     action_param = 'action'
35
+
36
+    # Permitted methods that can be used to act on the selected objects
35
     actions = None
37
     actions = None
36
     current_view = None
38
     current_view = None
37
     checkbox_object_name = None
39
     checkbox_object_name = None
38
 
40
 
39
     def get_checkbox_object_name(self):
41
     def get_checkbox_object_name(self):
40
-        object_list = self.get_queryset()
41
         if self.checkbox_object_name:
42
         if self.checkbox_object_name:
42
             return self.checkbox_object_name
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
             return smart_str(object_list.model._meta.object_name.lower())
46
             return smart_str(object_list.model._meta.object_name.lower())
45
         else:
47
         else:
46
             return None
48
             return None
47
 
49
 
50
+    def get_queryset(self):
51
+        pass
52
+
48
     def get_error_url(self, request):
53
     def get_error_url(self, request):
49
         return request.META['HTTP_REFERER']
54
         return request.META['HTTP_REFERER']
50
 
55
 
60
             messages.error(self.request, _("Invalid action"))
65
             messages.error(self.request, _("Invalid action"))
61
             return HttpResponseRedirect(self.get_error_url(request))
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
         if not ids:
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
             return HttpResponseRedirect(self.get_error_url(request))
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
         return getattr(self, action)(request, objects)
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)

Carregando…
Cancelar
Salvar