|
|
@@ -8,54 +8,33 @@ from django.views.generic import ListView, DetailView
|
|
8
|
8
|
from django.template.response import TemplateResponse
|
|
9
|
9
|
|
|
10
|
10
|
from oscar.core.loading import import_module
|
|
11
|
|
-
|
|
12
|
|
-product_signals = import_module('product.signals', ['product_viewed', 'product_search'])
|
|
13
|
|
-basket_forms = import_module('basket.forms', ['FormFactory'])
|
|
14
|
|
-history_helpers = import_module('customer.history_helpers', ['receive_product_view'])
|
|
|
11
|
+from oscar.apps.product.signals import product_viewed, product_search
|
|
15
|
12
|
|
|
16
|
13
|
from django.db.models import get_model
|
|
17
|
14
|
|
|
18
|
15
|
item_model = get_model('product','item')
|
|
19
|
16
|
item_class_model = get_model('product', 'itemclass')
|
|
20
|
17
|
|
|
|
18
|
+
|
|
21
|
19
|
class ItemDetailView(DetailView):
|
|
22
|
|
- u"""View a single product."""
|
|
23
|
20
|
template_name = "oscar/product/item.html"
|
|
24
|
|
- _item = None
|
|
|
21
|
+ model = item_model
|
|
|
22
|
+ view_signal = product_viewed
|
|
25
|
23
|
|
|
26
|
24
|
def get(self, request, **kwargs):
|
|
27
|
|
- u"""
|
|
28
|
|
- Ensures that the correct URL is used
|
|
29
|
|
- """
|
|
30
|
|
- item = self.get_object()
|
|
31
|
|
- correct_path = item.get_absolute_url()
|
|
|
25
|
+ # super call sets self.object for us
|
|
|
26
|
+ response = super(ItemDetailView, self).get(request, **kwargs)
|
|
|
27
|
+ correct_path = self.object.get_absolute_url()
|
|
|
28
|
+
|
|
|
29
|
+ # Ensures that the correct URL is used
|
|
32
|
30
|
if correct_path != request.path:
|
|
33
|
31
|
return HttpResponsePermanentRedirect(correct_path)
|
|
34
|
32
|
|
|
35
|
|
- response = super(ItemDetailView, self).get(request, **kwargs)
|
|
36
|
|
-
|
|
37
|
33
|
# Send signal to record the view of this product
|
|
38
|
|
- product_signals.product_viewed.send(sender=self, product=item, user=request.user, request=request, response=response)
|
|
|
34
|
+ self.view_signal.send(sender=self, product=self.object, user=request.user, request=request, response=response)
|
|
39
|
35
|
return response;
|
|
40
|
|
-
|
|
41
|
|
- def get_object(self):
|
|
42
|
|
- u"""
|
|
43
|
|
- Return a product object or a 404.
|
|
44
|
|
-
|
|
45
|
|
- We cache the object as this method gets called twice."""
|
|
46
|
|
- if not self._item:
|
|
47
|
|
- self._item = get_object_or_404(item_model, pk=self.kwargs['item_id'])
|
|
48
|
|
- return self._item
|
|
49
|
|
-
|
|
50
|
|
- def get_context_data(self, **kwargs):
|
|
51
|
|
- context = super(ItemDetailView, self).get_context_data(**kwargs)
|
|
52
|
|
- context['basket_form'] = self.get_add_to_basket_form()
|
|
53
|
|
- return context
|
|
54
|
|
-
|
|
55
|
|
- def get_add_to_basket_form(self):
|
|
56
|
|
- factory = basket_forms.FormFactory()
|
|
57
|
|
- return factory.create(self.object)
|
|
58
|
|
-
|
|
|
36
|
+
|
|
|
37
|
+
|
|
59
|
38
|
class ItemClassListView(ListView):
|
|
60
|
39
|
u"""View products filtered by item-class."""
|
|
61
|
40
|
context_object_name = "products"
|
|
|
@@ -66,12 +45,12 @@ class ItemClassListView(ListView):
|
|
66
|
45
|
item_class = get_object_or_404(item_class_model, slug=self.kwargs['item_class_slug'])
|
|
67
|
46
|
return item_model.browsable.filter(item_class=item_class)
|
|
68
|
47
|
|
|
69
|
|
-
|
|
70
|
48
|
class ProductListView(ListView):
|
|
71
|
49
|
u"""A list of products"""
|
|
72
|
50
|
context_object_name = "products"
|
|
73
|
51
|
template_name = 'oscar/product/browse.html'
|
|
74
|
52
|
paginate_by = 20
|
|
|
53
|
+ search_signal = product_search
|
|
75
|
54
|
|
|
76
|
55
|
def get_search_query(self):
|
|
77
|
56
|
u"""Return a search query from GET"""
|
|
|
@@ -81,11 +60,11 @@ class ProductListView(ListView):
|
|
81
|
60
|
return q
|
|
82
|
61
|
|
|
83
|
62
|
def get_queryset(self):
|
|
84
|
|
- u"""Return a set of prodcuts"""
|
|
|
63
|
+ u"""Return a set of products"""
|
|
85
|
64
|
q = self.get_search_query()
|
|
86
|
65
|
if q:
|
|
87
|
66
|
# Send signal to record the view of this product
|
|
88
|
|
- product_signals.product_search.send(sender=self, query=q, user=self.request.user)
|
|
|
67
|
+ self.search_signal.send(sender=self, query=q, user=self.request.user)
|
|
89
|
68
|
|
|
90
|
69
|
return item_model.browsable.filter(title__icontains=q)
|
|
91
|
70
|
else:
|