Просмотр исходного кода

Updated category creation code.

master
David Winterbottom 14 лет назад
Родитель
Сommit
4fce370963

+ 5
- 0
docs/source/recipes.rst Просмотреть файл

@@ -10,6 +10,11 @@ Customisation
10 10
 * :doc:`recipes/how_to_customise_models`
11 11
 * :doc:`recipes/how_to_override_a_core_class`
12 12
 
13
+Catalogue
14
+---------
15
+
16
+* :doc:`recipes/how_to_create_categories.rst`
17
+
13 18
 Checkout
14 19
 --------
15 20
 

+ 17
- 0
docs/source/recipes/how_to_create_categories.rst Просмотреть файл

@@ -0,0 +1,17 @@
1
+========================
2
+How to create categories
3
+========================
4
+
5
+The simplest way is to use a string which represents the breadcrumbs::
6
+
7
+    from oscar.apps.catalogue.categories import create_from_breadcrumbs
8
+
9
+    categories = (
10
+        'Food > Cheese',
11
+        'Food > Meat',
12
+        'Clothes > Man > Jackets',
13
+        'Clothes > Womam > Skirts',
14
+    )
15
+    for breadcrumbs in categories:
16
+        create_from_breadcrumbs(breadcrumbs)
17
+

+ 29
- 0
oscar/apps/catalogue/categories.py Просмотреть файл

@@ -0,0 +1,29 @@
1
+from django.db.models import get_model
2
+
3
+Category = get_model('catalogue', 'category')
4
+
5
+
6
+def create_from_sequence(bits):
7
+    if len(bits) == 1:
8
+        # Get or create root node
9
+        try:
10
+            root = Category.objects.get(depth=1, name=bits[0])
11
+        except Category.DoesNotExist:
12
+            root = Category.add_root(name=bits[0])
13
+        return [root]
14
+    else:
15
+        parents = create_from_sequence(bits[:-1])
16
+        try:
17
+            child = parents[-1].get_children().get(name=bits[-1])
18
+        except Category.DoesNotExist:
19
+            child = parents[-1].add_child(name=bits[-1])
20
+        parents.append(child)
21
+        return parents
22
+
23
+def create_from_breadcrumbs(breadcrumb_str, separator='>'):
24
+    """
25
+    Create categories from a breadcrumb string
26
+    """
27
+    category_names = [x.strip() for x in breadcrumb_str.split(separator)]
28
+    categories = create_from_sequence(category_names)
29
+    return categories[-1]

+ 15
- 8
oscar/apps/catalogue/tests.py Просмотреть файл

@@ -4,21 +4,24 @@ from django.core.exceptions import ValidationError
4 4
 from django.core.urlresolvers import reverse
5 5
 
6 6
 from oscar.apps.catalogue.models import Product, ProductClass, Category
7
-from oscar.apps.catalogue.utils import breadcrumbs_to_category
7
+from oscar.apps.catalogue.categories import create_from_breadcrumbs
8 8
 
9 9
 
10 10
 class CategoryTests(TestCase):
11
+
12
+    def setUp(self):
13
+        Category.objects.all().delete()
11 14
     
12
-    def test_create_category_root(self):
15
+    def test_creating_category_root(self):
13 16
         trail = 'Books'
14
-        category = breadcrumbs_to_category(trail)
17
+        category = create_from_breadcrumbs(trail)
15 18
         self.assertIsNotNone(category)
16 19
         self.assertEquals(category.name, 'Books')
17 20
         self.assertEquals(category.slug, 'books')      
18 21
     
19
-    def test_subcategory(self):
22
+    def test_creating_parent_and_child_categories(self):
20 23
         trail = 'Books > Science-Fiction'
21
-        category = breadcrumbs_to_category(trail)
24
+        category = create_from_breadcrumbs(trail)
22 25
         
23 26
         self.assertIsNotNone(category)
24 27
         self.assertEquals(category.name, 'Science-Fiction')
@@ -27,11 +30,11 @@ class CategoryTests(TestCase):
27 30
         self.assertEquals(2, Category.objects.count())
28 31
         self.assertEquals(category.slug, 'books/science-fiction')
29 32
         
30
-    def test_subsubcategory(self):
33
+    def test_creating_multiple_categories(self):
31 34
         trail = 'Books > Science-Fiction > Star Trek'
32
-        breadcrumbs_to_category(trail)
35
+        create_from_breadcrumbs(trail)
33 36
         trail = 'Books > Factual > Popular Science'
34
-        category = breadcrumbs_to_category(trail)        
37
+        category = create_from_breadcrumbs(trail)        
35 38
         
36 39
         self.assertIsNotNone(category)
37 40
         self.assertEquals(category.name, 'Popular Science')
@@ -40,6 +43,10 @@ class CategoryTests(TestCase):
40 43
         self.assertEquals(5, Category.objects.count())
41 44
         self.assertEquals(category.slug, 'books/factual/popular-science', )        
42 45
 
46
+    def test_alternative_separator_can_be_used(self):
47
+        trail = 'Food|Cheese|Blue'
48
+        create_from_breadcrumbs(trail, separator='|')
49
+        self.assertEquals(3, len(Category.objects.all()))
43 50
 
44 51
 class ItemTests(TestCase):
45 52
 

+ 0
- 23
oscar/apps/catalogue/utils.py Просмотреть файл

@@ -18,29 +18,6 @@ Product = get_model('catalogue', 'product')
18 18
 ProductImage = get_model('catalogue', 'productimage')
19 19
 
20 20
 
21
-def create_categories(bits):
22
-    if len(bits) == 1:
23
-        # Get or create root node
24
-        try:
25
-            root = Category.objects.get(depth=1, name=bits[0])
26
-        except Category.DoesNotExist:
27
-            root = Category.add_root(name=bits[0])
28
-        return [root]
29
-    else:
30
-        parents = create_categories(bits[:-1])
31
-        try:
32
-            child = parents[-1].get_children().get(name=bits[-1])
33
-        except Category.DoesNotExist:
34
-            child = parents[-1].add_child(name=bits[-1])
35
-        parents.append(child)
36
-        return parents
37
-
38
-def breadcrumbs_to_category(breadcrumbs, separator='>'):
39
-    bits = [x.strip() for x in breadcrumbs.split(separator)]
40
-    categories = create_categories(bits)
41
-    return categories[-1]
42
-
43
-
44 21
 class Importer(object):
45 22
     
46 23
     allowed_extensions = ['.jpeg','.jpg','.gif','.png']

Загрузка…
Отмена
Сохранить