Skip to content

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

Move Voucher and Sale choices to separate classes

Anna Siwik4032a9e7ed0c
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index 69153a321c..95063fb12b 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -13,6 +13,7 @@ from faker.providers import BaseProvider
from payments import PaymentStatus
from prices import Price
+from ...discount import VoucherType, DiscountValueType
from ...discount.models import Sale, Voucher
from ...order import GroupStatus
from ...order.models import DeliveryGroup, Order, OrderLine, Payment
@@ -463,7 +464,7 @@ def create_fake_order():
def create_fake_sale():
sale = Sale.objects.create(
name='Happy %s day!' % fake.word(),
- type=Sale.PERCENTAGE,
+ type=DiscountValueType.PERCENTAGE,
value=random.choice([10, 20, 30, 40, 50]))
for product in Product.objects.all().order_by('?')[:4]:
sale.products.add(product)
@@ -500,9 +501,9 @@ def create_shipping_methods():
def create_vouchers():
voucher, created = Voucher.objects.get_or_create(
code='FREESHIPPING', defaults={
- 'type': Voucher.SHIPPING_TYPE,
+ 'type': VoucherType.SHIPPING,
'name': 'Free shipping',
- 'discount_value_type': Voucher.DISCOUNT_VALUE_PERCENTAGE,
+ 'discount_value_type': DiscountValueType.PERCENTAGE,
'discount_value': 100})
if created:
yield 'Voucher #%d' % voucher.id
@@ -511,9 +512,9 @@ def create_vouchers():
voucher, created = Voucher.objects.get_or_create(
code='DISCOUNT', defaults={
- 'type': Voucher.VALUE_TYPE,
+ 'type': VoucherType.VALUE,
'name': 'Big order discount',
- 'discount_value_type': Voucher.DISCOUNT_VALUE_FIXED,
+ 'discount_value_type': DiscountValueType.FIXED,
'discount_value': 25,
'limit': 200})
if created:
diff --git a/saleor/dashboard/discount/forms.py b/saleor/dashboard/discount/forms.py
index 89348b900f..2dee739cb5 100644
--- a/saleor/dashboard/discount/forms.py
+++ b/saleor/dashboard/discount/forms.py
@@ -8,6 +8,7 @@ from django_prices.forms import PriceField
from ...core.forms import (
AjaxSelect2ChoiceField, AjaxSelect2MultipleChoiceField)
+from ...discount import DiscountValueType, VoucherApplyToProduct
from ...discount.models import Sale, Voucher
from ...product.models import Product
from ...shipping.models import ShippingMethodCountry, COUNTRY_CODE_CHOICES
@@ -47,7 +48,7 @@ class SaleForm(forms.ModelForm):
cleaned_data = super().clean()
discount_type = cleaned_data['type']
value = cleaned_data['value']
- if discount_type == Sale.PERCENTAGE and value > 100:
+ if discount_type == DiscountValueType.PERCENTAGE and value > 100:
self.add_error('value', pgettext_lazy(
'Sale (discount) error',
'Sale cannot exceed 100%'))
@@ -153,7 +154,7 @@ class CommonVoucherForm(forms.ModelForm):
use_required_attribute = False
apply_to = forms.ChoiceField(
- choices=Voucher.APPLY_TO_PRODUCT_CHOICES, required=False)
+ choices=VoucherApplyToProduct.CHOICES, required=False)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@@ -166,7 +167,7 @@ class CommonVoucherForm(forms.ModelForm):
# Percentage case is limited to the all value and the apply_to field
# is not used in this case so we set it to None.
if (self.instance.discount_value_type ==
- Voucher.DISCOUNT_VALUE_PERCENTAGE):
+ DiscountValueType.PERCENTAGE):
self.instance.apply_to = None
return super().save(commit)
diff --git a/saleor/dashboard/discount/views.py b/saleor/dashboard/discount/views.py
index 6762043d8e..ad7f8b8d5f 100644
--- a/saleor/dashboard/discount/views.py
+++ b/saleor/dashboard/discount/views.py
@@ -6,6 +6,7 @@ from django.template.response import TemplateResponse
from django.utils.translation import pgettext_lazy
from ...core.utils import get_paginator_items
+from ...discount import VoucherType
from ...discount.models import Sale, Voucher
from ..views import staff_member_required
from .filters import SaleFilter, VoucherFilter
@@ -91,18 +92,18 @@ def voucher_add(request):
instance = Voucher()
voucher_form = forms.VoucherForm(request.POST or None, instance=instance)
type_base_forms = {
- Voucher.SHIPPING_TYPE: forms.ShippingVoucherForm(
+ VoucherType.SHIPPING: forms.ShippingVoucherForm(
request.POST or None, instance=instance,
- prefix=Voucher.SHIPPING_TYPE),
- Voucher.VALUE_TYPE: forms.ValueVoucherForm(
+ prefix=VoucherType.SHIPPING),
+ VoucherType.VALUE: forms.ValueVoucherForm(
request.POST or None, instance=instance,
- prefix=Voucher.VALUE_TYPE),
- Voucher.PRODUCT_TYPE: forms.ProductVoucherForm(
+ prefix=VoucherType.VALUE),
+ VoucherType.PRODUCT: forms.ProductVoucherForm(
request.POST or None, instance=instance,
- prefix=Voucher.PRODUCT_TYPE),
- Voucher.CATEGORY_TYPE: forms.CategoryVoucherForm(
+ prefix=VoucherType.PRODUCT),
+ VoucherType.CATEGORY: forms.CategoryVoucherForm(
request.POST or None, instance=instance,
- prefix=Voucher.CATEGORY_TYPE)}
+ prefix=VoucherType.CATEGORY)}
if voucher_form.is_valid():
voucher_type = voucher_form.cleaned_data['type']
form_type = type_base_forms.get(voucher_type)
@@ -128,18 +129,18 @@ def voucher_edit(request, pk):
instance = get_object_or_404(Voucher, pk=pk)
voucher_form = forms.VoucherForm(request.POST or None, instance=instance)
type_base_forms = {
- Voucher.SHIPPING_TYPE: forms.ShippingVoucherForm(
+ VoucherType.SHIPPING: forms.ShippingVoucherForm(
request.POST or None, instance=instance,
- prefix=Voucher.SHIPPING_TYPE),
- Voucher.VALUE_TYPE: forms.ValueVoucherForm(
+ prefix=VoucherType.SHIPPING),
+ VoucherType.VALUE: forms.ValueVoucherForm(
request.POST or None, instance=instance,
- prefix=Voucher.VALUE_TYPE),
- Voucher.PRODUCT_TYPE: forms.ProductVoucherForm(
+ prefix=VoucherType.VALUE),
+ VoucherType.PRODUCT: forms.ProductVoucherForm(
request.POST or None, instance=instance,
- prefix=Voucher.PRODUCT_TYPE),
- Voucher.CATEGORY_TYPE: forms.CategoryVoucherForm(
+ prefix=VoucherType.PRODUCT),
+ VoucherType.CATEGORY: forms.CategoryVoucherForm(
request.POST or None, instance=instance,
- prefix=Voucher.CATEGORY_TYPE)}
+ prefix=VoucherType.CATEGORY)}
if voucher_form.is_valid():
voucher_type = voucher_form.cleaned_data['type']
form_type = type_base_forms.get(voucher_type)
diff --git a/saleor/discount/__init__.py b/saleor/discount/__init__.py
index e69de29bb2..b2637ce1ee 100644
--- a/saleor/discount/__init__.py
+++ b/saleor/discount/__init__.py
@@ -0,0 +1,37 @@
+from django.conf import settings
+from django.utils.translation import pgettext_lazy
+
+
+class DiscountValueType:
+ FIXED = 'fixed'
+ PERCENTAGE = 'percentage'
+
+ CHOICES = [
+ (FIXED, pgettext_lazy(
+ 'Discount type', settings.DEFAULT_CURRENCY)),
+ (PERCENTAGE, pgettext_lazy('Discount type', '%'))]
+
+
+class VoucherType:
+ PRODUCT = 'product'
+ CATEGORY = 'category'
+ SHIPPING = 'shipping'
+ VALUE = 'value'
+
+ CHOICES = [
+ (VALUE, pgettext_lazy('Voucher: discount for', 'All purchases')),
+ (PRODUCT, pgettext_lazy('Voucher: discount for', 'One product')),
+ (CATEGORY, pgettext_lazy(
+ 'Voucher: discount for', 'A category of products')),
+ (SHIPPING, pgettext_lazy('Voucher: discount for', 'Shipping'))]
+
+
+class VoucherApplyToProduct:
+ ONE_PRODUCT = 'one'
+ ALL_PRODUCTS = 'all'
+
+ CHOICES = [
+ (ONE_PRODUCT, pgettext_lazy(
+ 'Voucher application', 'Apply to a single item')),
+ (ALL_PRODUCTS, pgettext_lazy(
+ 'Voucher application', 'Apply to all matching products'))]
diff --git a/saleor/discount/models.py b/saleor/discount/models.py
index c601d31004..5c29b0ed87 100644
--- a/saleor/discount/models.py
+++ b/saleor/discount/models.py
@@ -13,6 +13,7 @@ from prices import FixedDiscount, Price, percentage_discount
from ..cart.utils import (
get_category_variants_and_prices, get_product_variants_and_prices)
+from . import DiscountValueType, VoucherApplyToProduct, VoucherType
class NotApplicable(ValueError):
@@ -36,45 +37,8 @@ class VoucherQueryset(models.QuerySet):
class Voucher(models.Model):
-
- APPLY_TO_ONE_PRODUCT = 'one'
- APPLY_TO_ALL_PRODUCTS = 'all'
-
- APPLY_TO_PRODUCT_CHOICES = [
- (
- APPLY_TO_ONE_PRODUCT,
- pgettext_lazy('Voucher application', 'Apply to a single item')),
- (
- APPLY_TO_ALL_PRODUCTS,
- pgettext_lazy(
- 'Voucher application', 'Apply to all matching products'))]
-
- DISCOUNT_VALUE_FIXED = 'fixed'
- DISCOUNT_VALUE_PERCENTAGE = 'percentage'
-
- DISCOUNT_VALUE_TYPE_CHOICES = [
- (
- DISCOUNT_VALUE_FIXED,
- pgettext_lazy('Voucher discount type', settings.DEFAULT_CURRENCY)),
- (
- DISCOUNT_VALUE_PERCENTAGE,
- pgettext_lazy('Voucher discount type', '%'))]
-
- PRODUCT_TYPE = 'product'
- CATEGORY_TYPE = 'category'
- SHIPPING_TYPE = 'shipping'
- VALUE_TYPE = 'value'
-
- TYPE_CHOICES = [
- (VALUE_TYPE, pgettext_lazy('Voucher: discount for', 'All purchases')),
- (PRODUCT_TYPE, pgettext_lazy('Voucher: discount for', 'One product')),
- (
- CATEGORY_TYPE,
- pgettext_lazy('Voucher: discount for', 'A category of products')),
- (SHIPPING_TYPE, pgettext_lazy('Voucher: discount for', 'Shipping'))]
-
type = models.CharField(
- max_length=20, choices=TYPE_CHOICES, default=VALUE_TYPE)
+ max_length=20, choices=VoucherType.CHOICES, default=VoucherType.VALUE)
name = models.CharField(max_length=255, null=True, blank=True)
code = models.CharField(max_length=12, unique=True, db_index=True)
usage_limit = models.PositiveIntegerField(null=True, blank=True)
@@ -83,8 +47,8 @@ class Voucher(models.Model):
end_date = models.DateField(null=True, blank=True)
discount_value_type = models.CharField(
- max_length=10, choices=DISCOUNT_VALUE_TYPE_CHOICES,
- default=DISCOUNT_VALUE_FIXED)
+ max_length=10, choices=DiscountValueType.CHOICES,
+ default=DiscountValueType.FIXED)
discount_value = models.DecimalField(max_digits=12, decimal_places=2)
# not mandatory fields, usage depends on type
@@ -102,8 +66,9 @@ class Voucher(models.Model):
@property
def is_free(self):
- return (self.discount_value == Decimal(100) and
- self.discount_value_type == Voucher.DISCOUNT_VALUE_PERCENTAGE)
+ return (
+ self.discount_value == Decimal(100) and
+ self.discount_value_type == DiscountValueType.PERCENTAGE)
class Meta:
permissions = (
@@ -117,19 +82,19 @@ class Voucher(models.Model):
return self.name
discount = '%s %s' % (
self.discount_value, self.get_discount_value_type_display())
- if self.type == Voucher.SHIPPING_TYPE:
+ if self.type == VoucherType.SHIPPING:
if self.is_free:
return pgettext('Voucher type', 'Free shipping')
else:
return pgettext(
'Voucher type',
'%(discount)s off shipping') % {'discount': discount}
- if self.type == Voucher.PRODUCT_TYPE:
+ if self.type == VoucherType.PRODUCT:
return pgettext(
'Voucher type',
'%(discount)s off %(product)s') % {
'discount': discount, 'product': self.product}
- if self.type == Voucher.CATEGORY_TYPE:
+ if self.type == VoucherType.CATEGORY:
return pgettext(
'Voucher type',
'%(discount)s off %(category)s') % {
@@ -138,22 +103,22 @@ class Voucher(models.Model):
'Voucher type', '%(discount)s off') % {'discount': discount}
def get_apply_to_display(self):
- if self.type == Voucher.SHIPPING_TYPE and self.apply_to:
+ if self.type == VoucherType.SHIPPING and self.apply_to:
return countries.name(self.apply_to)
- if self.type == Voucher.SHIPPING_TYPE:
+ if self.type == VoucherType.SHIPPING:
return pgettext('Voucher', 'Any country')
if self.apply_to and self.type in {
- Voucher.PRODUCT_TYPE, Voucher.CATEGORY_TYPE}:
- choices = dict(self.APPLY_TO_PRODUCT_CHOICES)
+ VoucherType.PRODUCT, VoucherType.CATEGORY}:
+ choices = dict(VoucherApplyToProduct.CHOICES)
return choices[self.apply_to]
def get_fixed_discount_for(self, amount):
- if self.discount_value_type == self.DISCOUNT_VALUE_FIXED:
+ if self.discount_value_type == DiscountValueType.FIXED:
discount_price = Price(net=self.discount_value,
currency=settings.DEFAULT_CURRENCY)
discount = FixedDiscount(
amount=discount_price, name=smart_text(self))
- elif self.discount_value_type == self.DISCOUNT_VALUE_PERCENTAGE:
+ elif self.discount_value_type == DiscountValueType.PERCENTAGE:
discount = percentage_discount(
value=self.discount_value, name=smart_text(self))
fixed_discount_value = amount - discount.apply(amount)
@@ -175,12 +140,12 @@ class Voucher(models.Model):
raise NotApplicable(msg % {'amount': net(limit)}, limit=limit)
def get_discount_for_checkout(self, checkout):
- if self.type == Voucher.VALUE_TYPE:
+ if self.type == VoucherType.VALUE:
cart_total = checkout.get_subtotal()
self.validate_limit(cart_total)
return self.get_fixed_discount_for(cart_total)
- elif self.type == Voucher.SHIPPING_TYPE:
+ elif self.type == VoucherType.SHIPPING:
if not checkout.is_shipping_required:
msg = pgettext(
'Voucher not applicable',
@@ -203,8 +168,8 @@ class Voucher(models.Model):
self.validate_limit(cart_total)
return self.get_fixed_discount_for(shipping_method.price)
- elif self.type in (Voucher.PRODUCT_TYPE, Voucher.CATEGORY_TYPE):
- if self.type == Voucher.PRODUCT_TYPE:
+ elif self.type in (VoucherType.PRODUCT, VoucherType.CATEGORY):
+ if self.type == VoucherType.PRODUCT:
prices = list(
(item[1] for item in get_product_variants_and_prices(
checkout.cart, self.product)))
@@ -217,7 +182,7 @@ class Voucher(models.Model):
'Voucher not applicable',
'This offer is only valid for selected items.')
raise NotApplicable(msg)
- if self.apply_to == Voucher.APPLY_TO_ALL_PRODUCTS:
+ if self.apply_to == VoucherApplyToProduct.ALL_PRODUCTS:
discounts = (
self.get_fixed_discount_for(price) for price in prices)
discount_total = sum(
@@ -234,16 +199,10 @@ class Voucher(models.Model):
class Sale(models.Model):
- FIXED = 'fixed'
- PERCENTAGE = 'percentage'
-
- DISCOUNT_TYPE_CHOICES = (
- (FIXED, pgettext_lazy('Discount type', settings.DEFAULT_CURRENCY)),
- (PERCENTAGE, pgettext_lazy('Discount type', '%')))
-
name = models.CharField(max_length=255)
type = models.CharField(
- max_length=10, choices=DISCOUNT_TYPE_CHOICES, default=FIXED)
+ max_length=10, choices=DiscountValueType.CHOICES,
+ default=DiscountValueType.FIXED)
value = models.DecimalField(max_digits=12, decimal_places=2, default=0)
products = models.ManyToManyField('product.Product', blank=True)
categories = models.ManyToManyField('product.Category', blank=True)
@@ -264,11 +223,11 @@ class Sale(models.Model):
return self.name
def get_discount(self):
- if self.type == self.FIXED:
+ if self.type == DiscountValueType.FIXED:
discount_price = Price(net=self.value,
currency=settings.DEFAULT_CURRENCY)
return FixedDiscount(amount=discount_price, name=self.name)
- elif self.type == self.PERCENTAGE:
+ elif self.type == DiscountValueType.PERCENTAGE:
return percentage_discount(value=self.value, name=self.name)
raise NotImplementedError('Unknown discount type')
diff --git a/tests/test_discounts.py b/tests/test_discounts.py
index d822e184aa..9f0fb5f69b 100644
--- a/tests/test_discounts.py
+++ b/tests/test_discounts.py
@@ -8,6 +8,8 @@ from prices import FixedDiscount, FractionalDiscount, Price
from saleor.cart.utils import get_category_variants_and_prices
from saleor.checkout.core import 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 (
@@ -20,8 +22,8 @@ from saleor.product.models import Category, Product, ProductVariant
(Price(10, currency='USD'), Price(10, currency='USD'))])
def test_valid_voucher_limit(settings, limit, value):
voucher = Voucher(
- code='unique', type=Voucher.SHIPPING_TYPE,
- discount_value_type=Voucher.DISCOUNT_VALUE_FIXED,
+ code='unique', type=VoucherType.SHIPPING,
+ discount_value_type=DiscountValueType.FIXED,
discount_value=Price(10, currency='USD'),
limit=limit)
voucher.validate_limit(value)
@@ -32,15 +34,15 @@ def test_valid_voucher_limit(settings, limit, value):
def test_variant_discounts(product_in_stock):
variant = product_in_stock.variants.get()
low_discount = Sale.objects.create(
- type=Sale.FIXED,
+ type=DiscountValueType.FIXED,
value=5)
low_discount.products.add(product_in_stock)
discount = Sale.objects.create(
- type=Sale.FIXED,
+ type=DiscountValueType.FIXED,
value=8)
discount.products.add(product_in_stock)
high_discount = Sale.objects.create(
- type=Sale.FIXED,
+ type=DiscountValueType.FIXED,
value=50)
high_discount.products.add(product_in_stock)
final_price = variant.get_price_per_item(
@@ -56,7 +58,7 @@ def test_variant_discounts(product_in_stock):
def test_percentage_discounts(product_in_stock):
variant = product_in_stock.variants.get()
discount = Sale.objects.create(
- type=Sale.PERCENTAGE,
+ type=DiscountValueType.PERCENTAGE,
value=50)
discount.products.add(product_in_stock)
final_price = variant.get_price_per_item(discounts=[discount])
@@ -68,12 +70,12 @@ def test_percentage_discounts(product_in_stock):
@pytest.mark.parametrize(
'total, discount_value, discount_type, limit, expected_value', [
- ('100', 10, Voucher.DISCOUNT_VALUE_FIXED, None, 10),
- ('100.05', 10, Voucher.DISCOUNT_VALUE_PERCENTAGE, 100, 10)])
+ ('100', 10, DiscountValueType.FIXED, None, 10),
+ ('100.05', 10, DiscountValueType.PERCENTAGE, 100, 10)])
def test_value_voucher_checkout_discount(settings, total, discount_value,
discount_type, limit, expected_value):
voucher = Voucher(
- code='unique', type=Voucher.VALUE_TYPE,
+ code='unique', type=VoucherType.VALUE,
discount_value_type=discount_type,
discount_value=discount_value,
limit=Price(limit, currency='USD') if limit is not None else None)
@@ -85,8 +87,8 @@ def test_value_voucher_checkout_discount(settings, total, discount_value,
def test_value_voucher_checkout_discount_not_applicable(settings):
voucher = Voucher(
- code='unique', type=Voucher.VALUE_TYPE,
- discount_value_type=Voucher.DISCOUNT_VALUE_FIXED,
+ code='unique', type=VoucherType.VALUE,
+ discount_value_type=DiscountValueType.FIXED,
discount_value=10,
limit=100)
checkout = Mock(get_subtotal=Mock(
@@ -98,10 +100,10 @@ def test_value_voucher_checkout_discount_not_applicable(settings):
@pytest.mark.parametrize(
'shipping_cost, shipping_country_code, discount_value, discount_type, apply_to, expected_value', [ # noqa
- (10, None, 50, Voucher.DISCOUNT_VALUE_PERCENTAGE, None, 5),
- (10, None, 20, Voucher.DISCOUNT_VALUE_FIXED, None, 10),
- (10, 'PL', 20, Voucher.DISCOUNT_VALUE_FIXED, '', 10),
- (5, 'PL', 5, Voucher.DISCOUNT_VALUE_FIXED, 'PL', 5)])
+ (10, None, 50, DiscountValueType.PERCENTAGE, None, 5),
+ (10, None, 20, DiscountValueType.FIXED, None, 10),
+ (10, 'PL', 20, DiscountValueType.FIXED, '', 10),
+ (5, 'PL', 5, DiscountValueType.FIXED, 'PL', 5)])
def test_shipping_voucher_checkout_discount(
settings, shipping_cost, shipping_country_code, discount_value,
discount_type, apply_to, expected_value):
@@ -111,7 +113,7 @@ def test_shipping_voucher_checkout_discount(
price=Price(shipping_cost, currency='USD'),
country_code=shipping_country_code))
voucher = Voucher(
- code='unique', type=Voucher.SHIPPING_TYPE,
+ code='unique', type=VoucherType.SHIPPING,
discount_value_type=discount_type,
discount_value=discount_value,
apply_to=apply_to,
@@ -123,17 +125,17 @@ def test_shipping_voucher_checkout_discount(
@pytest.mark.parametrize(
'is_shipping_required, shipping_method, discount_value, discount_type, '
'apply_to, limit, subtotal, error_msg', [
- (True, Mock(country_code='PL'), 10, Voucher.DISCOUNT_VALUE_FIXED,
+ (True, Mock(country_code='PL'), 10, DiscountValueType.FIXED,
'US', None, Price(10, currency='USD'),
'This offer is only valid in United States of America.'),
- (True, None, 10, Voucher.DISCOUNT_VALUE_FIXED,
+ (True, None, 10, DiscountValueType.FIXED,
None, None, Price(10, currency='USD'),
'Please select a shipping method first.'),
- (False, None, 10, Voucher.DISCOUNT_VALUE_FIXED,
+ (False, None, 10, DiscountValueType.FIXED,
None, None, Price(10, currency='USD'),
'Your order does not require shipping.'),
(True, Mock(price=Price(10, currency='USD')), 10,
- Voucher.DISCOUNT_VALUE_FIXED, None, 5, Price(2, currency='USD'),
+ DiscountValueType.FIXED, None, 5, Price(2, currency='USD'),
'This offer is only valid for orders over $5.00.')])
def test_shipping_voucher_checkout_discountnot_applicable(
settings, is_shipping_required, shipping_method, discount_value,
@@ -142,7 +144,7 @@ def test_shipping_voucher_checkout_discountnot_applicable(
shipping_method=shipping_method,
get_subtotal=Mock(return_value=subtotal))
voucher = Voucher(
- code='unique', type=Voucher.SHIPPING_TYPE,
+ code='unique', type=VoucherType.SHIPPING,
discount_value_type=discount_type,
discount_value=discount_value,
limit=Price(limit, currency='USD') if limit is not None else None,
@@ -158,8 +160,8 @@ def test_product_voucher_checkout_discount_not_applicable(settings,
'saleor.discount.models.get_product_variants_and_prices',
lambda cart, product: [])
voucher = Voucher(
- code='unique', type=Voucher.PRODUCT_TYPE,
- discount_value_type=Voucher.DISCOUNT_VALUE_FIXED,
+ code='unique', type=VoucherType.PRODUCT,
+ discount_value_type=DiscountValueType.FIXED,
discount_value=10)
checkout = Mock(cart=Mock())
@@ -174,8 +176,8 @@ def test_category_voucher_checkout_discount_not_applicable(settings,
'saleor.discount.models.get_category_variants_and_prices',
lambda cart, product: [])
voucher = Voucher(
- code='unique', type=Voucher.CATEGORY_TYPE,
- discount_value_type=Voucher.DISCOUNT_VALUE_FIXED,
+ code='unique', type=VoucherType.CATEGORY,
+ discount_value_type=DiscountValueType.FIXED,
discount_value=10)
checkout = Mock(cart=Mock())
with pytest.raises(NotApplicable) as e:
@@ -246,17 +248,31 @@ def test_checkout_discount_form_active_queryset_after_some_time(voucher):
@pytest.mark.parametrize(
'prices, discount_value, discount_type, apply_to, expected_value', [
- ([10], 10, Voucher.DISCOUNT_VALUE_FIXED, Voucher.APPLY_TO_ONE_PRODUCT, 10), # noqa
- ([5], 10, Voucher.DISCOUNT_VALUE_FIXED, Voucher.APPLY_TO_ONE_PRODUCT, 5), # noqa
- ([5, 5], 10, Voucher.DISCOUNT_VALUE_FIXED, Voucher.APPLY_TO_ONE_PRODUCT, 10), # noqa
- ([2, 3], 10, Voucher.DISCOUNT_VALUE_FIXED, Voucher.APPLY_TO_ONE_PRODUCT, 5), # noqa
-
- ([10, 10], 5, Voucher.DISCOUNT_VALUE_FIXED, Voucher.APPLY_TO_ALL_PRODUCTS, 10), # noqa
- ([5, 2], 5, Voucher.DISCOUNT_VALUE_FIXED, Voucher.APPLY_TO_ALL_PRODUCTS, 7), # noqa
- ([10, 10, 10], 5, Voucher.DISCOUNT_VALUE_FIXED, Voucher.APPLY_TO_ALL_PRODUCTS, 15), # noqa
-
- ([10], 10, Voucher.DISCOUNT_VALUE_PERCENTAGE, None, 1),
- ([10, 10], 10, Voucher.DISCOUNT_VALUE_PERCENTAGE, None, 2)])
+ (
+ [10], 10, DiscountValueType.FIXED,
+ VoucherApplyToProduct.ONE_PRODUCT, 10),
+ (
+ [5], 10, DiscountValueType.FIXED,
+ VoucherApplyToProduct.ONE_PRODUCT, 5),
+ (
+ [5, 5], 10, DiscountValueType.FIXED,
+ VoucherApplyToProduct.ONE_PRODUCT, 10),
+ (
+ [2, 3], 10, DiscountValueType.FIXED,
+ VoucherApplyToProduct.ONE_PRODUCT, 5),
+
+ (
+ [10, 10], 5, DiscountValueType.FIXED,
+ VoucherApplyToProduct.ALL_PRODUCTS, 10),
+ (
+ [5, 2], 5, DiscountValueType.FIXED,
+ VoucherApplyToProduct.ALL_PRODUCTS, 7),
+ (
+ [10, 10, 10], 5, DiscountValueType.FIXED,
+ VoucherApplyToProduct.ALL_PRODUCTS, 15),
+
+ ([10], 10, DiscountValueType.PERCENTAGE, None, 1),
+ ([10, 10], 10, DiscountValueType.PERCENTAGE, None, 2)])
def test_products_voucher_checkout_discount_not(settings, monkeypatch, prices,
discount_value, discount_type,
apply_to, expected_value):
@@ -265,7 +281,7 @@ def test_products_voucher_checkout_discount_not(settings, monkeypatch, prices,
lambda cart, product: (
(None, Price(p, currency='USD')) for p in prices))
voucher = Voucher(
- code='unique', type=Voucher.PRODUCT_TYPE,
+ code='unique', type=VoucherType.PRODUCT,
discount_value_type=discount_type,
discount_value=discount_value,
apply_to=apply_to)
@@ -285,7 +301,8 @@ def test_sale_applies_to_correct_products(product_class):
product_class=product_class)
sec_variant = ProductVariant.objects.create(
product=product2, sku='secvar', pk=10)
- sale = Sale.objects.create(name='Test sale', value=5, type=Sale.FIXED)
+ sale = Sale.objects.create(
+ 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(
@@ -312,9 +329,9 @@ def test_get_category_variants_and_prices_product_with_many_categories(
assert len(discounted_products) == 1
voucher = Voucher.objects.create(
- category=default_category, type=Voucher.CATEGORY_TYPE,
+ category=default_category, type=VoucherType.CATEGORY,
discount_value='10.0', code='foobar',
- discount_value_type=Voucher.DISCOUNT_VALUE_PERCENTAGE)
+ discount_value_type=DiscountValueType.PERCENTAGE)
checkout_mock = Mock(spec=Checkout, cart=cart)
discount = voucher.get_discount_for_checkout(checkout_mock)
# 10% of 10.00 is 1.00