Selaa lähdekoodia

Create a mixin for filtering partner's products in dashboard views, for easier overriding (#3566)

master
Farooq Azam 5 vuotta sitten
vanhempi
commit
2c3e633c7d
No account linked to committer's email address

+ 13
- 0
src/oscar/apps/dashboard/catalogue/mixins.py Näytä tiedosto

1
+class PartnerProductFilterMixin:
2
+    def filter_queryset(self, queryset):
3
+        """
4
+        Restrict the queryset to products the given user has access to.
5
+        A staff user is allowed to access all Products.
6
+        A non-staff user is only allowed access to a product if they are in at
7
+        least one stock record's partner user list.
8
+        """
9
+        user = self.request.user
10
+        if user.is_staff:
11
+            return queryset
12
+
13
+        return queryset.filter(stockrecords__partner__users__pk=user.pk).distinct()

+ 9
- 12
src/oscar/apps/dashboard/catalogue/views.py Näytä tiedosto

9
 from django.views import generic
9
 from django.views import generic
10
 from django_tables2 import SingleTableMixin, SingleTableView
10
 from django_tables2 import SingleTableMixin, SingleTableView
11
 
11
 
12
-from oscar.core.loading import get_classes, get_model
12
+from oscar.core.decorators import deprecated
13
+from oscar.core.loading import get_class, get_classes, get_model
13
 from oscar.views.generic import ObjectLookupView
14
 from oscar.views.generic import ObjectLookupView
14
 
15
 
15
 (ProductForm,
16
 (ProductForm,
53
                   ('PopUpWindowCreateMixin',
54
                   ('PopUpWindowCreateMixin',
54
                    'PopUpWindowUpdateMixin',
55
                    'PopUpWindowUpdateMixin',
55
                    'PopUpWindowDeleteMixin'))
56
                    'PopUpWindowDeleteMixin'))
57
+PartnerProductFilterMixin = get_class('dashboard.catalogue.mixins', 'PartnerProductFilterMixin')
56
 Product = get_model('catalogue', 'Product')
58
 Product = get_model('catalogue', 'Product')
57
 Category = get_model('catalogue', 'Category')
59
 Category = get_model('catalogue', 'Category')
58
 ProductImage = get_model('catalogue', 'ProductImage')
60
 ProductImage = get_model('catalogue', 'ProductImage')
65
 Option = get_model('catalogue', 'Option')
67
 Option = get_model('catalogue', 'Option')
66
 
68
 
67
 
69
 
70
+@deprecated
68
 def filter_products(queryset, user):
71
 def filter_products(queryset, user):
69
     """
72
     """
70
     Restrict the queryset to products the given user has access to.
73
     Restrict the queryset to products the given user has access to.
78
     return queryset.filter(stockrecords__partner__users__pk=user.pk).distinct()
81
     return queryset.filter(stockrecords__partner__users__pk=user.pk).distinct()
79
 
82
 
80
 
83
 
81
-class ProductListView(SingleTableView):
84
+class ProductListView(PartnerProductFilterMixin, SingleTableView):
82
 
85
 
83
     """
86
     """
84
     Dashboard view of the product list.
87
     Dashboard view of the product list.
113
     def get_table_pagination(self, table):
116
     def get_table_pagination(self, table):
114
         return dict(per_page=settings.OSCAR_DASHBOARD_ITEMS_PER_PAGE)
117
         return dict(per_page=settings.OSCAR_DASHBOARD_ITEMS_PER_PAGE)
115
 
118
 
116
-    def filter_queryset(self, queryset):
117
-        """
118
-        Apply any filters to restrict the products that appear on the list
119
-        """
120
-        return filter_products(queryset, self.request.user)
121
-
122
     def get_queryset(self):
119
     def get_queryset(self):
123
         """
120
         """
124
         Build the queryset for this list
121
         Build the queryset for this list
194
             return self.get_invalid_product_class_url()
191
             return self.get_invalid_product_class_url()
195
 
192
 
196
 
193
 
197
-class ProductCreateUpdateView(generic.UpdateView):
194
+class ProductCreateUpdateView(PartnerProductFilterMixin, generic.UpdateView):
198
     """
195
     """
199
     Dashboard view that is can both create and update products of all kinds.
196
     Dashboard view that is can both create and update products of all kinds.
200
     It can be used in three different ways, each of them with a unique URL
197
     It can be used in three different ways, each of them with a unique URL
252
         """
249
         """
253
         Filter products that the user doesn't have permission to update
250
         Filter products that the user doesn't have permission to update
254
         """
251
         """
255
-        return filter_products(Product.objects.all(), self.request.user)
252
+        return self.filter_queryset(Product.objects.all())
256
 
253
 
257
     def get_object(self, queryset=None):
254
     def get_object(self, queryset=None):
258
         """
255
         """
451
         return self.get_url_with_querystring(url)
448
         return self.get_url_with_querystring(url)
452
 
449
 
453
 
450
 
454
-class ProductDeleteView(generic.DeleteView):
451
+class ProductDeleteView(PartnerProductFilterMixin, generic.DeleteView):
455
     """
452
     """
456
     Dashboard view to delete a product. Has special logic for deleting the
453
     Dashboard view to delete a product. Has special logic for deleting the
457
     last child product.
454
     last child product.
465
         """
462
         """
466
         Filter products that the user doesn't have permission to update
463
         Filter products that the user doesn't have permission to update
467
         """
464
         """
468
-        return filter_products(Product.objects.all(), self.request.user)
465
+        return self.filter_queryset(Product.objects.all())
469
 
466
 
470
     def get_context_data(self, **kwargs):
467
     def get_context_data(self, **kwargs):
471
         ctx = super().get_context_data(**kwargs)
468
         ctx = super().get_context_data(**kwargs)

Loading…
Peruuta
Tallenna