Skip to content

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

Make applying voucher methods more general

Anna Siwik46a773ca0508
diff --git a/saleor/cart/utils.py b/saleor/cart/utils.py
index 6417bc84a1..6d449a20de 100644
--- a/saleor/cart/utils.py
+++ b/saleor/cart/utils.py
@@ -71,7 +71,7 @@ def get_product_variants_and_prices(cart, product):
def get_category_variants_and_prices(cart, root_category):
"""Get variants and unit prices from cart lines matching the category.
- Product is assumed to be in the the category if it belongs to any of its
+ Product is assumed to be in the category if it belongs to any of its
descendant subcategories.
"""
products = {cart_line.variant.product for cart_line in cart.lines.all()}
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.py
index 5f1c813999..36a6d07893 100644
--- a/saleor/checkout/core.py
+++ b/saleor/checkout/core.py
@@ -16,11 +16,11 @@ from ..cart.utils import get_or_empty_db_cart
from ..core import analytics
from ..core.utils import ZERO_TAXED_MONEY
from ..discount.models import NotApplicable, Voucher
-from ..discount.utils import (
- get_voucher_discount_for_checkout, increase_voucher_usage)
+from ..discount.utils import increase_voucher_usage
from ..order.models import Order
from ..order.utils import add_variant_to_order
from ..shipping.models import ANY_COUNTRY, ShippingMethodCountry
+from .utils import get_voucher_discount_for_checkout
STORAGE_SESSION_KEY = 'checkout_storage'
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
new file mode 100644
index 0000000000..4bc4914e7e
--- /dev/null
+++ b/saleor/checkout/utils.py
@@ -0,0 +1,73 @@
+from django.utils.translation import pgettext
+
+from ..cart.utils import (
+ get_category_variants_and_prices, get_product_variants_and_prices)
+from ..discount import VoucherType
+from ..discount.models import NotApplicable
+from ..discount.utils import (
+ get_product_or_category_voucher_discount, get_shipping_voucher_discount,
+ get_value_voucher_discount)
+
+
+def _get_value_voucher_discount_for_checkout(voucher, checkout):
+ """Calculate discount value for a voucher of value type."""
+ return get_value_voucher_discount(
+ voucher, checkout.get_subtotal())
+
+
+def _get_shipping_voucher_discount_for_checkout(voucher, checkout):
+ """Calculate discount value for a voucher of shipping type."""
+ if not checkout.is_shipping_required:
+ msg = pgettext(
+ 'Voucher not applicable',
+ 'Your order does not require shipping.')
+ raise NotApplicable(msg)
+ shipping_method = checkout.shipping_method
+ if not shipping_method:
+ msg = pgettext(
+ 'Voucher not applicable',
+ 'Please select a shipping method first.')
+ raise NotApplicable(msg)
+ not_valid_for_country = (
+ voucher.apply_to and shipping_method.country_code != voucher.apply_to)
+ if not_valid_for_country:
+ msg = pgettext(
+ 'Voucher not applicable',
+ 'This offer is only valid in %(country)s.')
+ raise NotApplicable(
+ msg % {'country': voucher.get_apply_to_display()})
+ return get_shipping_voucher_discount(
+ voucher, checkout.get_subtotal(), shipping_method.get_total_price())
+
+
+def _get_product_or_category_voucher_discount_for_checkout(voucher, checkout):
+ """Calculate discount value for a voucher of product or category type."""
+ if voucher.type == VoucherType.PRODUCT:
+ prices = [
+ item[1] for item in get_product_variants_and_prices(
+ checkout.cart, voucher.product)]
+ else:
+ prices = [
+ item[1] for item in get_category_variants_and_prices(
+ checkout.cart, voucher.category)]
+ if not prices:
+ msg = pgettext(
+ 'Voucher not applicable',
+ 'This offer is only valid for selected items.')
+ raise NotApplicable(msg)
+ return get_product_or_category_voucher_discount(voucher, prices)
+
+
+def get_voucher_discount_for_checkout(voucher, checkout):
+ """Calculate discount value depending on voucher and discount types.
+
+ Raise NotApplicable if voucher of given type cannot be applied.
+ """
+ if voucher.type == VoucherType.VALUE:
+ return _get_value_voucher_discount_for_checkout(voucher, checkout)
+ if voucher.type == VoucherType.SHIPPING:
+ return _get_shipping_voucher_discount_for_checkout(voucher, checkout)
+ if voucher.type in (VoucherType.PRODUCT, VoucherType.CATEGORY):
+ return _get_product_or_category_voucher_discount_for_checkout(
+ voucher, checkout)
+ raise NotImplementedError('Unknown discount type')
diff --git a/saleor/dashboard/order/utils.py b/saleor/dashboard/order/utils.py
index 88383775a3..ce14141f30 100644
--- a/saleor/dashboard/order/utils.py
+++ b/saleor/dashboard/order/utils.py
@@ -2,7 +2,6 @@ from django.conf import settings
from django.contrib.sites.shortcuts import get_current_site
from django.template.loader import get_template
-from ...account.models import Address
from ...product.utils import decrease_stock
INVOICE_TEMPLATE = 'dashboard/order/pdf/invoice.html'
diff --git a/saleor/discount/forms.py b/saleor/discount/forms.py
index 9453a6b138..4db214b3f6 100644
--- a/saleor/discount/forms.py
+++ b/saleor/discount/forms.py
@@ -4,8 +4,8 @@ from django import forms
from django.utils.encoding import smart_text
from django.utils.translation import pgettext_lazy
+from ..checkout.utils import get_voucher_discount_for_checkout
from .models import NotApplicable, Voucher
-from .utils import get_voucher_discount_for_checkout
class VoucherField(forms.ModelChoiceField):
diff --git a/saleor/discount/models.py b/saleor/discount/models.py
index 6ba002992f..490bb6887d 100644
--- a/saleor/discount/models.py
+++ b/saleor/discount/models.py
@@ -5,7 +5,6 @@ from functools import partial
from django.conf import settings
from django.db import models
from django.db.models import F, Q
-from django.utils.encoding import smart_text
from django.utils.translation import pgettext, pgettext_lazy
from django_countries import countries
from django_prices.models import MoneyField
diff --git a/saleor/discount/utils.py b/saleor/discount/utils.py
index 16abe8d323..b90d6c27a6 100644
--- a/saleor/discount/utils.py
+++ b/saleor/discount/utils.py
@@ -1,12 +1,9 @@
from django.conf import settings
from django.db.models import F
-from django.utils.encoding import smart_text
from django.utils.translation import pgettext
-from prices import Money, TaxedMoney
+from prices import Money
-from . import VoucherApplyToProduct, VoucherType
-from ..cart.utils import (
- get_category_variants_and_prices, get_product_variants_and_prices)
+from . import VoucherApplyToProduct
from ..core.utils import ZERO_TAXED_MONEY
from .models import NotApplicable
@@ -63,54 +60,21 @@ def calculate_discounted_price(product, price, discounts):
return price
-def _get_value_voucher_discount_for_checkout(voucher, checkout):
+def get_value_voucher_discount(voucher, total_price):
"""Calculate discount value for a voucher of value type."""
- cart_total = checkout.get_subtotal()
- voucher.validate_limit(cart_total)
- return voucher.get_discount_amount_for(cart_total)
+ voucher.validate_limit(total_price)
+ return voucher.get_discount_amount_for(total_price)
-def _get_shipping_voucher_discount_for_checkout(voucher, checkout):
+def get_shipping_voucher_discount(
+ voucher, total_price, shipping_price):
"""Calculate discount value for a voucher of shipping type."""
- if not checkout.is_shipping_required:
- msg = pgettext(
- 'Voucher not applicable',
- 'Your order does not require shipping.')
- raise NotApplicable(msg)
- shipping_method = checkout.shipping_method
- if not shipping_method:
- msg = pgettext(
- 'Voucher not applicable',
- 'Please select a shipping method first.')
- raise NotApplicable(msg)
- not_valid_for_country = (
- voucher.apply_to and shipping_method.country_code != voucher.apply_to)
- if not_valid_for_country:
- msg = pgettext(
- 'Voucher not applicable',
- 'This offer is only valid in %(country)s.')
- raise NotApplicable(
- msg % {'country': voucher.get_apply_to_display()})
- cart_total = checkout.get_subtotal()
- voucher.validate_limit(cart_total)
- return voucher.get_discount_amount_for(shipping_method.get_total_price())
+ voucher.validate_limit(total_price)
+ return voucher.get_discount_amount_for(shipping_price)
-def _get_product_or_category_voucher_discount_for_checkout(voucher, checkout):
+def get_product_or_category_voucher_discount(voucher, prices):
"""Calculate discount value for a voucher of product or category type."""
- if voucher.type == VoucherType.PRODUCT:
- prices = [
- item[1] for item in get_product_variants_and_prices(
- checkout.cart, voucher.product)]
- else:
- prices = [
- item[1] for item in get_category_variants_and_prices(
- checkout.cart, voucher.category)]
- if not prices:
- msg = pgettext(
- 'Voucher not applicable',
- 'This offer is only valid for selected items.')
- raise NotApplicable(msg)
if voucher.apply_to == VoucherApplyToProduct.ALL_PRODUCTS:
discounts = (
voucher.get_discount_amount_for(price) for price in prices)
@@ -119,18 +83,3 @@ def _get_product_or_category_voucher_discount_for_checkout(voucher, checkout):
return total_amount
product_total = sum(prices, ZERO_TAXED_MONEY)
return voucher.get_discount_amount_for(product_total)
-
-
-def get_voucher_discount_for_checkout(voucher, checkout):
- """Calculate discount value depending on voucher and discount types.
-
- Raise NotApplicable if voucher of given type cannot be applied.
- """
- if voucher.type == VoucherType.VALUE:
- return _get_value_voucher_discount_for_checkout(voucher, checkout)
- if voucher.type == VoucherType.SHIPPING:
- return _get_shipping_voucher_discount_for_checkout(voucher, checkout)
- if voucher.type in (VoucherType.PRODUCT, VoucherType.CATEGORY):
- return _get_product_or_category_voucher_discount_for_checkout(
- voucher, checkout)
- raise NotImplementedError('Unknown discount type')
diff --git a/tests/test_discounts.py b/tests/test_discounts.py
index 02d73f9b1b..62c757be42 100644
--- a/tests/test_discounts.py
+++ b/tests/test_discounts.py
@@ -1,18 +1,18 @@
from datetime import date, timedelta
-from decimal import Decimal
from unittest.mock import Mock
import pytest
from freezegun import freeze_time
from prices import Money, TaxedMoney
+from saleor.checkout.utils import get_voucher_discount_for_checkout
from saleor.discount import (
DiscountValueType, VoucherApplyToProduct, VoucherType)
from saleor.discount.forms import CheckoutDiscountForm
from saleor.discount.models import NotApplicable, Sale, Voucher
from saleor.discount.utils import (
decrease_voucher_usage, get_product_discount_on_sale,
- get_voucher_discount_for_checkout, increase_voucher_usage)
+ increase_voucher_usage)
from saleor.product.models import Product, ProductVariant
@@ -154,7 +154,7 @@ def test_shipping_voucher_checkout_discount_not_applicable(
def test_product_voucher_checkout_discount_not_applicable(
settings, monkeypatch):
monkeypatch.setattr(
- 'saleor.discount.utils.get_product_variants_and_prices',
+ 'saleor.checkout.utils.get_product_variants_and_prices',
lambda cart, product: [])
voucher = Voucher(
code='unique', type=VoucherType.PRODUCT,
@@ -170,7 +170,7 @@ def test_product_voucher_checkout_discount_not_applicable(
def test_category_voucher_checkout_discount_not_applicable(
settings, monkeypatch):
monkeypatch.setattr(
- 'saleor.discount.utils.get_category_variants_and_prices',
+ 'saleor.checkout.utils.get_category_variants_and_prices',
lambda cart, product: [])
voucher = Voucher(
code='unique', type=VoucherType.CATEGORY,
@@ -281,7 +281,7 @@ def test_products_voucher_checkout_discount_not(
settings, monkeypatch, prices, discount_value, discount_type, apply_to,
expected_value):
monkeypatch.setattr(
- 'saleor.discount.utils.get_product_variants_and_prices',
+ 'saleor.checkout.utils.get_product_variants_and_prices',
lambda cart, product: (
(None, TaxedMoney(
net=Money(price, 'USD'), gross=Money(price, 'USD')))