Skip to content

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

Move discounts methods to separate utils file

Anna Siwik49742b75d4a2
diff --git a/saleor/discount/models.py b/saleor/discount/models.py
index 5f0814541b..c601d31004 100644
--- a/saleor/discount/models.py
+++ b/saleor/discount/models.py
@@ -292,20 +292,3 @@ class Sale(models.Model):
pgettext(
'Voucher not applicable',
'Discount not applicable for this product'))
-
-
-def get_product_discounts(product, discounts, **kwargs):
- for discount in discounts:
- try:
- yield discount.modifier_for_product(product, **kwargs)
- except NotApplicable:
- pass
-
-
-def calculate_discounted_price(product, price, discounts, **kwargs):
- if discounts:
- discounts = list(
- get_product_discounts(product, discounts, **kwargs))
- if discounts:
- price = min(price | discount for discount in discounts)
- return price
diff --git a/saleor/discount/utils.py b/saleor/discount/utils.py
index a5df5fdf38..5076d0874b 100644
--- a/saleor/discount/utils.py
+++ b/saleor/discount/utils.py
@@ -1,5 +1,7 @@
from django.db.models import F
+from .models import NotApplicable
+
def increase_voucher_usage(voucher):
voucher.used = F('used') + 1
@@ -9,3 +11,20 @@ def increase_voucher_usage(voucher):
def decrease_voucher_usage(voucher):
voucher.used = F('used') - 1
voucher.save(update_fields=['used'])
+
+
+def get_product_discounts(product, discounts, **kwargs):
+ for discount in discounts:
+ try:
+ yield discount.modifier_for_product(product, **kwargs)
+ except NotApplicable:
+ pass
+
+
+def calculate_discounted_price(product, price, discounts, **kwargs):
+ if discounts:
+ discounts = list(
+ get_product_discounts(product, discounts, **kwargs))
+ if discounts:
+ price = min(price | discount for discount in discounts)
+ return price
diff --git a/saleor/product/models.py b/saleor/product/models.py
index 777a0cf164..4ef4647858 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -18,7 +18,7 @@ from satchless.item import InsufficientStock, Item, ItemRange
from text_unidecode import unidecode
from versatileimagefield.fields import PPOIField, VersatileImageField
-from ..discount.models import calculate_discounted_price
+from ..discount.utils import calculate_discounted_price
from .utils import get_attributes_display_map