|
|
@@ -1,4 +1,4 @@
|
|
1
|
|
-from django.core.exceptions import ObjectDoesNotExist, PermissionDenied, ImproperlyConfigured
|
|
|
1
|
+from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
|
|
2
|
2
|
from django.views import generic
|
|
3
|
3
|
from django.db.models import get_model
|
|
4
|
4
|
from django.http import HttpResponseRedirect, Http404
|
|
|
@@ -35,6 +35,15 @@ StockAlert = get_model('partner', 'StockAlert')
|
|
35
|
35
|
Partner = get_model('partner', 'Partner')
|
|
36
|
36
|
|
|
37
|
37
|
|
|
|
38
|
+def get_queryset_for_user(user):
|
|
|
39
|
+ queryset = Product.objects.base_queryset().order_by('-date_created')
|
|
|
40
|
+ if user.is_staff:
|
|
|
41
|
+ return queryset.all()
|
|
|
42
|
+ else:
|
|
|
43
|
+ return queryset.filter(
|
|
|
44
|
+ stockrecords__partner__users__pk=user.pk).distinct()
|
|
|
45
|
+
|
|
|
46
|
+
|
|
38
|
47
|
class ProductListView(generic.ListView):
|
|
39
|
48
|
template_name = 'dashboard/catalogue/product_list.html'
|
|
40
|
49
|
model = Product
|
|
|
@@ -50,14 +59,6 @@ class ProductListView(generic.ListView):
|
|
50
|
59
|
ctx['queryset_description'] = self.description
|
|
51
|
60
|
return ctx
|
|
52
|
61
|
|
|
53
|
|
- def get_queryset_for_user(self, user):
|
|
54
|
|
- queryset = self.model.objects.base_queryset().select_related(
|
|
55
|
|
- 'stockrecord__partner').order_by('-date_created')
|
|
56
|
|
- if user.is_staff:
|
|
57
|
|
- return queryset.all()
|
|
58
|
|
- else:
|
|
59
|
|
- return queryset.filter(stockrecord__partner__users__pk=user.pk)
|
|
60
|
|
-
|
|
61
|
62
|
def get_queryset(self):
|
|
62
|
63
|
"""
|
|
63
|
64
|
Build the queryset for this list and also update the title that
|
|
|
@@ -65,7 +66,7 @@ class ProductListView(generic.ListView):
|
|
65
|
66
|
"""
|
|
66
|
67
|
description_ctx = {'upc_filter': '',
|
|
67
|
68
|
'title_filter': ''}
|
|
68
|
|
- queryset = self.get_queryset_for_user(self.request.user)
|
|
|
69
|
+ queryset = get_queryset_for_user(self.request.user)
|
|
69
|
70
|
self.form = self.form_class(self.request.GET)
|
|
70
|
71
|
if not self.form.is_valid():
|
|
71
|
72
|
self.description = self.description_template % description_ctx
|
|
|
@@ -137,9 +138,13 @@ class ProductCreateUpdateView(generic.UpdateView):
|
|
137
|
138
|
else:
|
|
138
|
139
|
return None # success
|
|
139
|
140
|
else:
|
|
140
|
|
- obj = super(ProductCreateUpdateView, self).get_object(queryset)
|
|
141
|
|
- self.product_class = obj.product_class
|
|
142
|
|
- return obj
|
|
|
141
|
+ product = super(ProductCreateUpdateView, self).get_object(queryset)
|
|
|
142
|
+ user = self.request.user
|
|
|
143
|
+ self.product_class = product.product_class
|
|
|
144
|
+ if user.is_staff or product.is_user_in_partners_users(user):
|
|
|
145
|
+ return product
|
|
|
146
|
+ else:
|
|
|
147
|
+ raise PermissionDenied
|
|
143
|
148
|
|
|
144
|
149
|
def get_context_data(self, **kwargs):
|
|
145
|
150
|
ctx = super(ProductCreateUpdateView, self).get_context_data(**kwargs)
|
|
|
@@ -270,7 +275,7 @@ class ProductDeleteView(generic.DeleteView):
|
|
270
|
275
|
"""
|
|
271
|
276
|
product = super(ProductDeleteView, self).get_object(queryset)
|
|
272
|
277
|
user = self.request.user
|
|
273
|
|
- if user.is_staff or product.user_in_partner_users(user):
|
|
|
278
|
+ if user.is_staff or product.is_user_in_partners_users(user):
|
|
274
|
279
|
return product
|
|
275
|
280
|
else:
|
|
276
|
281
|
raise PermissionDenied
|