瀏覽代碼

Tidied up product app a bit more, using default class-based view functionality as much as I can.

master
Andrew Ingram 14 年之前
父節點
當前提交
80c8013fd9
共有 4 個檔案被更改,包括 20 行新增41 行删除
  1. 1
    1
      oscar/apps/product/__init__.py
  2. 1
    1
      oscar/apps/product/abstract_models.py
  3. 3
    3
      oscar/apps/product/tests.py
  4. 15
    36
      oscar/apps/product/views.py

+ 1
- 1
oscar/apps/product/__init__.py 查看文件

@@ -13,7 +13,7 @@ class ProductApplication(Application):
13 13
         urlpatterns = patterns('',
14 14
             url(r'^$', self.list_view.as_view(), name='list'),
15 15
             url(r'^(?P<item_class_slug>[\w-]+)/$', self.class_list_view.as_view(), name='class-list'),            
16
-            url(r'^(?P<item_class_slug>[\w-]+)/(?P<item_slug>[\w-]*)-(?P<item_id>\d+)/$', self.detail_view.as_view(), name='detail'),
16
+            url(r'^(?P<item_class_slug>[\w-]+)/(?P<item_slug>[\w-]*)-(?P<pk>\d+)/$', self.detail_view.as_view(), name='detail'),
17 17
         )
18 18
         return urlpatterns
19 19
 

+ 1
- 1
oscar/apps/product/abstract_models.py 查看文件

@@ -189,7 +189,7 @@ class AbstractItem(models.Model):
189 189
         return ('products:detail', (), {
190 190
             'item_class_slug': self.get_item_class().slug, 
191 191
             'item_slug': self.slug,
192
-            'item_id': self.id})
192
+            'pk': self.id})
193 193
     
194 194
     def save(self, *args, **kwargs):
195 195
         if self.is_top_level and not self.title:

+ 3
- 3
oscar/apps/product/tests.py 查看文件

@@ -39,7 +39,7 @@ class VariantItemTests(ItemTests):
39 39
     def test_variant_products_inherit_product_class(self):
40 40
         p = Item.objects.create(parent=self.parent)
41 41
         self.assertEquals("Clothing", p.get_item_class().name)
42
-        
42
+
43 43
 
44 44
 class SingleProductViewTest(TestCase):
45 45
     fixtures = ['sample-products']
@@ -51,7 +51,7 @@ class SingleProductViewTest(TestCase):
51 51
         p = Item.objects.get(id=1)
52 52
         args = {'item_class_slug': p.get_item_class().slug, 
53 53
                 'item_slug': 'wrong-slug',
54
-                'item_id': p.id}
55
-        wrong_url = reverse('oscar-product-item', kwargs=args)
54
+                'pk': p.id}
55
+        wrong_url = reverse('products:detail', kwargs=args)
56 56
         response = self.client.get(wrong_url)
57 57
         self.assertEquals(301, response.status_code)

+ 15
- 36
oscar/apps/product/views.py 查看文件

@@ -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:

Loading…
取消
儲存