Sfoglia il codice sorgente

Made a few adjustments to recently added products code / just tidying up slightly

master
David Winterbottom 14 anni fa
parent
commit
fec50e08eb

+ 9
- 1
docs/source/reference/settings.rst Vedi File

@@ -26,12 +26,20 @@ Default: 604800 (1 week in seconds)
26 26
 The time to live for the basket cookie in seconds
27 27
 
28 28
 OSCAR_IMAGE_FOLDER
29
----------------------------
29
+------------------
30 30
 
31 31
 Default: images/products-fullsize/%Y/%m/ 
32 32
 
33 33
 The path for uploading images to.
34 34
 
35
+OSCAR_RECENTLY_VIEWED_PRODUCTS
36
+------------------------------
37
+
38
+Default: 5
39
+
40
+The number of recently viewed products to store
41
+
42
+
35 43
 
36 44
 Deprecated settings
37 45
 ===================

+ 8
- 10
oscar/customer/history_helpers.py Vedi File

@@ -1,18 +1,18 @@
1
+import json
2
+
1 3
 from django.dispatch import receiver
2 4
 from django.conf import settings
3 5
 
4 6
 from oscar.services import import_module
5
-
6
-import json
7
-
8 7
 product_signals = import_module('product.signals', ['product_viewed'])
9
-max_products = settings.OSCAR_RECENTLY_VIEWED_PRODUCTS
8
+
9
+MAX_PRODUCTS = getattr(settings, 'OSCAR_RECENTLY_VIEWED_PRODUCTS', 5)
10 10
 
11 11
 # Helpers
12 12
 
13 13
 def get_recently_viewed_product_ids(request):
14 14
     u"""
15
-    The list of ids of the last products browsed by the user
15
+    Returns the list of ids of the last products browsed by the user
16 16
     
17 17
     Limited to the max number defined in settings.py
18 18
     under OSCAR_RECENTLY_VIEWED_PRODUCTS.
@@ -22,7 +22,7 @@ def get_recently_viewed_product_ids(request):
22 22
         try:
23 23
             product_ids = _get_list_from_json_string(request.COOKIES['oscar_recently_viewed_products'])
24 24
         except ValueError:
25
-            # This can occure if something messes up the cookie
25
+            # This can occur if something messes up the cookie
26 26
             pass
27 27
     return product_ids
28 28
 
@@ -35,11 +35,9 @@ def _update_recently_viewed_products(product, request, response):
35 35
     if product.id in product_ids:
36 36
         product_ids.remove(product.id)
37 37
     product_ids.append(product.id)
38
-    if (len(product_ids) > max_products):
39
-        assert False
40
-        del product_ids[max_products:]
38
+    if (len(product_ids) > MAX_PRODUCTS):
39
+        del product_ids[MAX_PRODUCTS:]
41 40
     response.set_cookie('oscar_recently_viewed_products', _get_json_string_from_list(product_ids))
42
-    return
43 41
 
44 42
 def _get_list_from_json_string(cookie_value):
45 43
     u""" Simple function to convert lists to json """

+ 2
- 3
oscar/customer/templatetags/history_tags.py Vedi File

@@ -1,13 +1,11 @@
1 1
 from django import template
2 2
 
3 3
 from oscar.services import import_module
4
-
5 4
 product_models = import_module('product.models', ['Item', 'ItemClass'])
6 5
 history_helpers = import_module('customer.history_helpers', ['get_recently_viewed_product_ids'])
7 6
 
8 7
 register = template.Library()
9 8
 
10
-
11 9
 @register.inclusion_tag('customer/history/recently-viewed-products.html', takes_context=True)
12 10
 def recently_viewed_products(context):
13 11
     u"""
@@ -16,7 +14,8 @@ def recently_viewed_products(context):
16 14
     request = context['request']
17 15
     product_ids = history_helpers.get_recently_viewed_product_ids(request)
18 16
     product_dict = product_models.Item.browsable.in_bulk(product_ids)
19
-    u"""Reordering as the id order gets messed up in the query"""
17
+    
18
+    # Reordering as the id order gets messed up in the query
20 19
     product_ids.reverse()
21 20
     products = [product_dict[id] for id in product_ids]
22 21
     return {'products': products}

+ 5
- 12
oscar/customer/tests.py Vedi File

@@ -3,28 +3,21 @@ from django.test.client import Client
3 3
 from django.core.urlresolvers import reverse
4 4
 from django.http import HttpRequest
5 5
 
6
-from oscar.product.models import Item, ItemClass
7 6
 from oscar.customer.history_helpers import get_recently_viewed_product_ids
7
+from oscar.utils import create_product
8 8
 
9 9
 class HistoryHelpersTest(unittest.TestCase):
10 10
     
11 11
     def setUp(self):
12 12
         self.client = Client()
13
-        
14
-        # Create a dummy product
15
-        ic,_ = ItemClass.objects.get_or_create(name='Dummy class')
16
-        self.dummy_product = Item.objects.create(title='Dummy product', item_class=ic)
17
-        args = {'item_class_slug': self.dummy_product.get_item_class().slug, 
18
-                'item_slug': self.dummy_product.slug,
19
-                'item_id': self.dummy_product.id}
20
-        self.dummy_product_url = reverse('oscar-product-item', kwargs=args)
13
+        self.product = create_product()
21 14
     
22 15
     def test_viewing_product_creates_cookie(self):
23
-        response = self.client.get(self.dummy_product_url)
16
+        response = self.client.get(self.product.get_absolute_url())
24 17
         self.assertTrue('oscar_recently_viewed_products' in response.cookies)
25 18
         
26 19
     def test_id_gets_added_to_cookie(self):
27
-        response = self.client.get(self.dummy_product_url)
20
+        response = self.client.get(self.product.get_absolute_url())
28 21
         request = HttpRequest()
29 22
         request.COOKIES['oscar_recently_viewed_products'] = response.cookies['oscar_recently_viewed_products'].value
30
-        self.assertTrue(self.dummy_product.id in get_recently_viewed_product_ids(request))
23
+        self.assertTrue(self.product.id in get_recently_viewed_product_ids(request))

Loading…
Annulla
Salva