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

Allow for children to be explicitly included in ranges.

master
John Parton 11 лет назад
Родитель
Сommit
5b2b50e112
2 измененных файлов: 23 добавлений и 9 удалений
  1. 3
    6
      src/oscar/apps/offer/abstract_models.py
  2. 20
    3
      tests/integration/offer/range_tests.py

+ 3
- 6
src/oscar/apps/offer/abstract_models.py Просмотреть файл

@@ -824,9 +824,6 @@ class AbstractRange(models.Model):
824 824
         Default display_order for a new product in the range is 0; this puts
825 825
         the product at the top of the list.
826 826
         """
827
-        if product.is_child:
828
-            raise ValueError(
829
-                "Ranges can only contain parent and stand-alone products.")
830 827
 
831 828
         initial_order = display_order or 0
832 829
         RangeProduct = get_model('offer', 'RangeProduct')
@@ -851,9 +848,6 @@ class AbstractRange(models.Model):
851 848
         """
852 849
         Check whether the passed product is part of this range.
853 850
         """
854
-        # Child products are never part of the range, but the parent may be.
855
-        if product.is_child:
856
-            product = product.parent
857 851
 
858 852
         # Delegate to a proxy class if one is provided
859 853
         if self.proxy:
@@ -867,6 +861,9 @@ class AbstractRange(models.Model):
867 861
         if product.product_class_id in self._class_ids():
868 862
             return True
869 863
         included_product_ids = self._included_product_ids()
864
+        # If the product's parent is in the range, the child is automatically included as well
865
+        if product.is_child and product.parent.id in included_product_ids:
866
+            return True
870 867
         if product.id in included_product_ids:
871 868
             return True
872 869
         test_categories = self.included_categories.all()

+ 20
- 3
tests/integration/offer/range_tests.py Просмотреть файл

@@ -28,6 +28,26 @@ class TestWholeSiteRange(TestCase):
28 28
         self.assertFalse(self.range.contains_product(self.prod))
29 29
 
30 30
 
31
+class TestChildRange(TestCase):
32
+
33
+    def setUp(self):
34
+        self.range = models.Range.objects.create(
35
+            name='Child-specific range', includes_all_products=False)
36
+        self.parent = create_product(structure='parent')
37
+        self.child1 = create_product(structure='child', parent=self.parent)
38
+        self.child2 = create_product(structure='child', parent=self.parent)
39
+        self.range.add_product(self.child1)
40
+
41
+    def test_includes_child(self):
42
+        self.assertTrue(self.range.contains_product(self.child1))
43
+
44
+    def test_does_not_include_parent(self):
45
+        self.assertFalse(self.range.contains_product(self.parent))
46
+
47
+    def test_does_not_include_sibling(self):
48
+        self.assertFalse(self.range.contains_product(self.child2))
49
+
50
+
31 51
 class TestPartialRange(TestCase):
32 52
 
33 53
     def setUp(self):
@@ -50,9 +70,6 @@ class TestPartialRange(TestCase):
50 70
         self.assertTrue(self.range.contains_product(self.parent))
51 71
         self.assertTrue(self.range.contains_product(self.child))
52 72
 
53
-    def test_cant_add_child_product(self):
54
-        self.assertRaises(ValueError, self.range.add_product, self.child)
55
-
56 73
     def test_included_class_with_exception(self):
57 74
         self.range.classes.add(self.parent.get_product_class())
58 75
         self.range.excluded_products.add(self.parent)

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