Skip to content

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

Drop apply_on field

Artur Smęta5470b7622c0
diff --git a/saleor/dashboard/discount/forms.py b/saleor/dashboard/discount/forms.py
index 8bc0820a0c..956723347b 100644
--- a/saleor/dashboard/discount/forms.py
+++ b/saleor/dashboard/discount/forms.py
@@ -1,5 +1,4 @@
from django import forms
-from django.core.exceptions import NON_FIELD_ERRORS
from django.utils.translation import pgettext_lazy
from ...product.models import Discount
@@ -9,26 +8,15 @@ class DiscountForm(forms.ModelForm):
class Meta:
model = Discount
exclude = []
+ widgets = {
+ 'type': forms.RadioSelect()}
def clean(self):
cleaned_data = super(DiscountForm, self).clean()
discount_type = cleaned_data['type']
- apply_on = cleaned_data['apply_on']
value = cleaned_data['value']
- required_msg = pgettext_lazy('discount error', 'This field is required')
if discount_type == Discount.PERCENTAGE and value > 100:
self.add_error('value', pgettext_lazy('discount error',
'Percentage discount '
'cannot be higher than 100%'))
- if (apply_on == Discount.APPLY_ON_PRODUCTS and not
- cleaned_data['products']):
- self.add_error('products', required_msg)
- elif (apply_on == Discount.APPLY_ON_CATEGORIES and not
- cleaned_data['categories']):
- self.add_error('categories', required_msg)
- elif apply_on == Discount.APPLY_ON_BOTH and not (
- cleaned_data['products'] or cleaned_data['categories']):
- self.add_error('products', required_msg)
- self.add_error('categories', required_msg)
- # TODO: Implement cost price checks
return cleaned_data
diff --git a/saleor/product/models/discounts.py b/saleor/product/models/discounts.py
index 5ccaa9d2aa..c69ad1a111 100644
--- a/saleor/product/models/discounts.py
+++ b/saleor/product/models/discounts.py
@@ -17,28 +17,17 @@ class NotApplicable(ValueError):
class Discount(models.Model):
FIXED = 'fixed'
PERCENTAGE = 'percentage'
- APPLY_ON_PRODUCTS = 'products'
- APPLY_ON_CATEGORIES = 'categories'
- APPLY_ON_BOTH = 'both'
DISCOUNT_TYPE_CHOICES = (
(FIXED, pgettext_lazy('discount type', 'Fixed amount')),
(PERCENTAGE, pgettext_lazy('discount_type', 'Percentage discount')))
- APPLY_ON_CHOICES = (
- (APPLY_ON_PRODUCTS, pgettext_lazy('discount apply on', 'Products')),
- (APPLY_ON_CATEGORIES, pgettext_lazy('discount apply on', 'Categories')),
- (APPLY_ON_BOTH, pgettext_lazy('discount apply on',
- 'Both products and categories')))
-
name = models.CharField(max_length=255)
- type = models.CharField(max_length=10, choices=DISCOUNT_TYPE_CHOICES)
- apply_on = models.CharField(max_length=10, choices=APPLY_ON_CHOICES,
- default=APPLY_ON_BOTH)
+ type = models.CharField(max_length=10, choices=DISCOUNT_TYPE_CHOICES,
+ default=FIXED)
products = models.ManyToManyField('Product', blank=True)
- categories = models.ManyToManyField('Category', blank=True)
value = models.DecimalField(
- max_digits=12, decimal_places=2,
+ max_digits=12, decimal_places=2, default=0,
help_text=pgettext_lazy('discount help',
'Value for a discount. It could be fixed '
'amount or percentage value in range 0-100'))
@@ -47,8 +36,8 @@ class Discount(models.Model):
app_label = 'product'
def __repr__(self):
- return 'FixedProductDiscount(name=%r, discount=%r)' % (
- str(self.discount), self.name)
+ return 'Discount(name=%r, value=%r, type=%s)' % (
+ str(self.discount), self.name, self.get_type_display())
def __str__(self):
return self.name
@@ -63,20 +52,8 @@ class Discount(models.Model):
raise NotImplementedError('Unknown discount type')
def modifier_for_product(self, product):
- product_categories = product.categories.all()
- discounted_categories = self.categories.all()
check_price = product.get_price_per_item()
- product_in_discount = product in self.products.all()
- category_in_discount = any(c in discounted_categories
- for c in product_categories)
-
- if self.apply_on == self.APPLY_ON_BOTH:
- if not (product_in_discount or category_in_discount):
- raise NotApplicable('Discount not applicable for this product')
- else:
- if self.apply_on == self.APPLY_ON_PRODUCTS and not product_in_discount:
- raise NotApplicable('Discount not applicable for this product')
- if self.apply_on == self.APPLY_ON_CATEGORIES and not category_in_discount:
+ if product not in self.products.all():
raise NotApplicable('Discount not applicable for this product')
discount = self.get_discount()
after_discount = discount.apply(check_price)