浏览代码

Prevent trying to save a negative quantity to a Basket

basket.Line.quantity is a positive integer field, but the quantity sumation in
basket.Basket.add_product made it possible to try and save negative values. This
makes add_product never go below 0-quantity, thereby preventing a db exception
from being thrown. Also adds a test for the described behavior.
master
Craig Weber 9 年前
父节点
当前提交
1234497f61
共有 2 个文件被更改,包括 8 次插入1 次删除
  1. 1
    1
      src/oscar/apps/basket/abstract_models.py
  2. 7
    0
      tests/integration/basket/model_tests.py

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

@@ -217,7 +217,7 @@ class AbstractBasket(models.Model):
217 217
                 line.attributes.create(option=option_dict['option'],
218 218
                                        value=option_dict['value'])
219 219
         else:
220
-            line.quantity += quantity
220
+            line.quantity = max(0, line.quantity + quantity)
221 221
             line.save()
222 222
         self.reset_offer_applications()
223 223
 

+ 7
- 0
tests/integration/basket/model_tests.py 查看文件

@@ -28,6 +28,13 @@ class TestAddingAProductToABasket(TestCase):
28 28
         self.assertEqual(line.price_incl_tax, self.purchase_info.price.incl_tax)
29 29
         self.assertEqual(line.price_excl_tax, self.purchase_info.price.excl_tax)
30 30
 
31
+    def test_adding_negative_quantity(self):
32
+        self.assertEqual(1, self.basket.num_lines)
33
+        self.basket.add(self.product, quantity=4)
34
+        self.assertEqual(5, self.basket.line_quantity(self.product, self.record))
35
+        self.basket.add(self.product, quantity=-10)
36
+        self.assertEqual(0, self.basket.line_quantity(self.product, self.record))
37
+
31 38
     def test_means_another_currency_product_cannot_be_added(self):
32 39
         product = factories.create_product()
33 40
         factories.create_stockrecord(

正在加载...
取消
保存