Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Move sale logic to utils file

Anna Siwikc8b30e3f814d
diff --git a/saleor/discount/models.py b/saleor/discount/models.py
index d25711ea36..4a88ddca83 100644
--- a/saleor/discount/models.py
+++ b/saleor/discount/models.py
@@ -11,9 +11,9 @@ from django_prices.models import PriceField
from django_prices.templatetags.prices_i18n import net
from prices import FixedDiscount, Price, percentage_discount
-from . import DiscountValueType, VoucherApplyToProduct, VoucherType
from ..cart.utils import (
get_category_variants_and_prices, get_product_variants_and_prices)
+from . import DiscountValueType, VoucherApplyToProduct, VoucherType
class NotApplicable(ValueError):
@@ -217,27 +217,10 @@ class Sale(models.Model):
def get_discount(self):
if self.type == DiscountValueType.FIXED:
- discount_price = Price(net=self.value,
- currency=settings.DEFAULT_CURRENCY)
+ discount_price = Price(
+ net=self.value, currency=settings.DEFAULT_CURRENCY)
return FixedDiscount(amount=discount_price, name=self.name)
- elif self.type == DiscountValueType.PERCENTAGE:
+ if self.type == DiscountValueType.PERCENTAGE:
return percentage_discount(value=self.value, name=self.name)
raise NotImplementedError('Unknown discount type')
- def _product_has_category_discount(self, product, discounted_categories):
- return any([
- product.category.is_descendant_of(category, include_self=True)
- for category in discounted_categories])
-
- def modifier_for_product(self, product):
- discounted_products = {p.pk for p in self.products.all()}
- discounted_categories = set(self.categories.all())
- if product.pk in discounted_products:
- return self.get_discount()
- if self._product_has_category_discount(
- product, discounted_categories):
- return self.get_discount()
- raise NotApplicable(
- pgettext(
- 'Voucher not applicable',
- 'Discount not applicable for this product'))
diff --git a/saleor/discount/utils.py b/saleor/discount/utils.py
index 5076d0874b..f09a622f7b 100644
--- a/saleor/discount/utils.py
+++ b/saleor/discount/utils.py
@@ -1,4 +1,5 @@
from django.db.models import F
+from django.utils.translation import pgettext
from .models import NotApplicable
@@ -16,7 +17,7 @@ def decrease_voucher_usage(voucher):
def get_product_discounts(product, discounts, **kwargs):
for discount in discounts:
try:
- yield discount.modifier_for_product(product, **kwargs)
+ yield get_product_discount_on_sale(discount, product)
except NotApplicable:
pass
@@ -28,3 +29,23 @@ def calculate_discounted_price(product, price, discounts, **kwargs):
if discounts:
price = min(price | discount for discount in discounts)
return price
+
+
+def is_category_discounted(category, discounted_categories):
+ return any([
+ category.is_descendant_of(c, include_self=True)
+ for c in discounted_categories])
+
+
+def get_product_discount_on_sale(sale, product):
+ discounted_products = {p.pk for p in sale.products.all()}
+ discounted_categories = set(sale.categories.all())
+ is_product_discounted = (
+ product.pk in discounted_products or
+ is_category_discounted(product.category, discounted_categories))
+ if is_product_discounted:
+ return sale.get_discount()
+ raise NotApplicable(
+ pgettext(
+ 'Voucher not applicable',
+ 'Discount not applicable for this product'))
diff --git a/tests/test_discounts.py b/tests/test_discounts.py
index 5ab268794b..da02acb1c5 100644
--- a/tests/test_discounts.py
+++ b/tests/test_discounts.py
@@ -11,7 +11,8 @@ from saleor.discount import (
from saleor.discount.forms import CheckoutDiscountForm
from saleor.discount.models import NotApplicable, Sale, Voucher
from saleor.discount.utils import (
- decrease_voucher_usage, increase_voucher_usage)
+ decrease_voucher_usage, increase_voucher_usage,
+ get_product_discount_on_sale)
from saleor.product.models import Product, ProductVariant
@@ -303,10 +304,10 @@ def test_sale_applies_to_correct_products(product_type, default_category):
name='Test sale', value=5, type=DiscountValueType.FIXED)
sale.products.add(product)
assert product2 not in sale.products.all()
- assert sale.modifier_for_product(variant.product).amount == Price(
- net=5, currency='USD')
+ product_discount = get_product_discount_on_sale(sale, variant.product)
+ assert product_discount.amount == Price(net=5, currency='USD')
with pytest.raises(NotApplicable):
- sale.modifier_for_product(sec_variant.product)
+ get_product_discount_on_sale(sale, sec_variant.product)
def test_increase_voucher_usage():