瀏覽代碼

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
         urlpatterns = patterns('',
13
         urlpatterns = patterns('',
14
             url(r'^$', self.list_view.as_view(), name='list'),
14
             url(r'^$', self.list_view.as_view(), name='list'),
15
             url(r'^(?P<item_class_slug>[\w-]+)/$', self.class_list_view.as_view(), name='class-list'),            
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
         return urlpatterns
18
         return urlpatterns
19
 
19
 

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

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

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

39
     def test_variant_products_inherit_product_class(self):
39
     def test_variant_products_inherit_product_class(self):
40
         p = Item.objects.create(parent=self.parent)
40
         p = Item.objects.create(parent=self.parent)
41
         self.assertEquals("Clothing", p.get_item_class().name)
41
         self.assertEquals("Clothing", p.get_item_class().name)
42
-        
42
+
43
 
43
 
44
 class SingleProductViewTest(TestCase):
44
 class SingleProductViewTest(TestCase):
45
     fixtures = ['sample-products']
45
     fixtures = ['sample-products']
51
         p = Item.objects.get(id=1)
51
         p = Item.objects.get(id=1)
52
         args = {'item_class_slug': p.get_item_class().slug, 
52
         args = {'item_class_slug': p.get_item_class().slug, 
53
                 'item_slug': 'wrong-slug',
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
         response = self.client.get(wrong_url)
56
         response = self.client.get(wrong_url)
57
         self.assertEquals(301, response.status_code)
57
         self.assertEquals(301, response.status_code)

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

8
 from django.template.response import TemplateResponse
8
 from django.template.response import TemplateResponse
9
 
9
 
10
 from oscar.core.loading import import_module
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
 from django.db.models import get_model
13
 from django.db.models import get_model
17
 
14
 
18
 item_model = get_model('product','item')
15
 item_model = get_model('product','item')
19
 item_class_model = get_model('product', 'itemclass')
16
 item_class_model = get_model('product', 'itemclass')
20
 
17
 
18
+
21
 class ItemDetailView(DetailView):
19
 class ItemDetailView(DetailView):
22
-    u"""View a single product."""
23
     template_name = "oscar/product/item.html"
20
     template_name = "oscar/product/item.html"
24
-    _item = None
21
+    model = item_model
22
+    view_signal = product_viewed
25
     
23
     
26
     def get(self, request, **kwargs):
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
         if correct_path != request.path:
30
         if correct_path != request.path:
33
             return HttpResponsePermanentRedirect(correct_path)
31
             return HttpResponsePermanentRedirect(correct_path)
34
         
32
         
35
-        response = super(ItemDetailView, self).get(request, **kwargs)
36
-        
37
         # Send signal to record the view of this product
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
         return response;
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
 class ItemClassListView(ListView):
38
 class ItemClassListView(ListView):
60
     u"""View products filtered by item-class."""
39
     u"""View products filtered by item-class."""
61
     context_object_name = "products"
40
     context_object_name = "products"
66
         item_class = get_object_or_404(item_class_model, slug=self.kwargs['item_class_slug'])
45
         item_class = get_object_or_404(item_class_model, slug=self.kwargs['item_class_slug'])
67
         return item_model.browsable.filter(item_class=item_class)
46
         return item_model.browsable.filter(item_class=item_class)
68
 
47
 
69
-
70
 class ProductListView(ListView):
48
 class ProductListView(ListView):
71
     u"""A list of products"""
49
     u"""A list of products"""
72
     context_object_name = "products"
50
     context_object_name = "products"
73
     template_name = 'oscar/product/browse.html'
51
     template_name = 'oscar/product/browse.html'
74
     paginate_by = 20
52
     paginate_by = 20
53
+    search_signal = product_search
75
 
54
 
76
     def get_search_query(self):
55
     def get_search_query(self):
77
         u"""Return a search query from GET"""
56
         u"""Return a search query from GET"""
81
         return q
60
         return q
82
 
61
 
83
     def get_queryset(self):
62
     def get_queryset(self):
84
-        u"""Return a set of prodcuts"""
63
+        u"""Return a set of products"""
85
         q = self.get_search_query()
64
         q = self.get_search_query()
86
         if q:
65
         if q:
87
             # Send signal to record the view of this product
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
             return item_model.browsable.filter(title__icontains=q)
69
             return item_model.browsable.filter(title__icontains=q)
91
         else:
70
         else:

Loading…
取消
儲存