Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add discounts class
Michał Ociepka752c61747ce6
diff --git a/discount/__init__.py b/discount/__init__.pynew file mode 100755index 0000000000..375e80a496--- /dev/null+++ b/discount/__init__.py@@ -0,0 +1,48 @@+from prices import PriceModifier+++class Discount(PriceModifier):++ def can_apply(self, item, **kwargs):+ raise NotImplementedError()++ def apply_to_item(self, item, **kwargs):+ raise NotImplementedError()+++class ItemDiscount(Discount):++ item = None++ def can_apply(self, item, **kwargs):+ return hasattr(item, 'get_price')++ def apply_to_item(self, item, **kwargs):+ self.item = item+ # We have to add set_price method to Item Class+ item.price += self+++class ItemSetDiscount(Discount):++ item_set = None++ def can_apply(self, item, **kwargs):+ return hasattr(item, 'get_total')++ def apply_to_item(self, item_set, **kwargs):+ self.item = item_set+ for item in item_set:+ # We definitely have to add set_price method to Item Class+ item.product.price += self+++class DiscountManager(list):++ def apply(self, item, **kwargs):+ for discount in self.filter(item, **kwargs):+ discount.apply_to_item(item)++ def filter(self, item, **kwargs):+ return [discount for discount in self+ if discount.can_apply(item, **kwargs)]diff --git a/discount/models.py b/discount/models.pynew file mode 100755index 0000000000..71a8362390--- /dev/null+++ b/discount/models.py@@ -0,0 +1,3 @@+from django.db import models++# Create your models here.diff --git a/discount/tests.py b/discount/tests.pynew file mode 100755index 0000000000..dd88f8a542--- /dev/null+++ b/discount/tests.py@@ -0,0 +1,76 @@+from . import ItemDiscount, DiscountManager, ItemSetDiscount+from cart import Cart+from decimal import Decimal+from django.test import TestCase+from mock import MagicMock, patch+from prices import Price, inspect_price+from product.models import Product+import operator+++class ProductDiscount(ItemDiscount):++ value = Decimal(5)+ name = 'Product discount'++ def apply(self, price):+ return Price(price.gross - self.value,+ currency=price.currency,+ previous=price,+ modifier=self,+ operation=operator.__sub__)++ def __repr__(self):+ return 'ProductDiscount(%r, name=%r)' % (str(self.value), self.name)+++class CartDiscount(ItemSetDiscount):++ precent = Decimal(0.5)+ name = 'Cart discount'++ def apply(self, price):+ return Price(price.gross - (price.gross * self.precent),+ currency=price.currency,+ previous=price,+ modifier=self,+ operation=operator.__sub__)++ def __repr__(self):+ return 'CartDiscount(%r%%, name=%r)' % (int(self.precent * 100),+ self.name)+++class TestDiscountManager(TestCase):++ def setUp(self):+ self.product = Product(name='test', price=Price(10, currency='USD'),+ sku='1', category_id=1)+ self.cart = Cart()+ self.cart.add(self.product, 2)++ def test_discount_manager_with_one_item_discaunt(self):+ discount = ProductDiscount()+ discounts = DiscountManager([discount])+ discounts.apply(self.product)+ self.assertEqual(self.product.price, Price(5, currency='USD'))++ def test_discount_manager_with_many_item_discaunts(self):+ discount = ProductDiscount()+ discounts = DiscountManager([discount])+ discounts.append(discount)+ discounts.apply(self.product)+ self.assertEqual(self.product.price, Price(0, currency='USD'))++ def test_discount_manager_with_one_item_set_discaunt(self):+ discount = CartDiscount()+ discounts = DiscountManager([discount])+ discounts.apply(self.cart)+ self.assertEqual(self.cart[0].product.price, Price(5, currency='USD'))++ def test_discount_manager_with_different_discaunts(self):+ discount_cart = CartDiscount()+ discount_product = ProductDiscount()+ discounts = DiscountManager([discount_cart, discount_product])+ discounts.apply(self.cart)+ self.assertEqual(self.cart[0].product.price, Price(5, currency='USD'))diff --git a/discount/views.py b/discount/views.pynew file mode 100755index 0000000000..60f00ef0ef--- /dev/null+++ b/discount/views.py@@ -0,0 +1 @@+# Create your views here.diff --git a/saleor/settings.py b/saleor/settings.pyindex c6a1fb2054..370d16f5b9 100644--- a/saleor/settings.py+++ b/saleor/settings.py@@ -105,6 +105,7 @@ INSTALLED_APPS = ['userprofile','registration','payment',+ 'discount',]LOGGING = {