Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Improve contextual markers and translations for discount app
Marco Badanaf607c766513
diff --git a/saleor/discount/forms.py b/saleor/discount/forms.pyindex 0b11a2584c..397a80f751 100644--- a/saleor/discount/forms.py+++ b/saleor/discount/forms.py@@ -9,7 +9,7 @@ class VoucherField(forms.ModelChoiceField):default_error_messages = {'invalid_choice': pgettext_lazy('voucher', pgettext_lazy(- 'voucher', 'Discount code incorrect or expired')),+ 'Voucher form error', 'Discount code incorrect or expired')),}@@ -17,7 +17,9 @@ class CheckoutDiscountForm(forms.Form):voucher = VoucherField(queryset=Voucher.objects.active(), to_field_name='code',- label=pgettext_lazy('voucher', 'Gift card or discount code'),+ label=pgettext_lazy(+ 'Checkout discount form label for voucher field',+ 'Gift card or discount code'),widget=forms.TextInput)def __init__(self, *args, **kwargs):diff --git a/saleor/discount/models.py b/saleor/discount/models.pyindex 339c665099..b697ab1ee0 100644--- a/saleor/discount/models.py+++ b/saleor/discount/models.py@@ -49,17 +49,17 @@ class Voucher(models.Model):APPLY_TO_PRODUCT_CHOICES = ((APPLY_TO_ONE_PRODUCT,- pgettext_lazy('voucher', 'Apply to a single item')),+ pgettext_lazy('Voucher application', 'Apply to a single item')),(APPLY_TO_ALL_PRODUCTS,- pgettext_lazy('voucher', 'Apply to all matching 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', settings.DEFAULT_CURRENCY)),- (DISCOUNT_VALUE_PERCENTAGE, pgettext_lazy('voucher', '%')))+ pgettext_lazy('Voucher discount type', settings.DEFAULT_CURRENCY)),+ (DISCOUNT_VALUE_PERCENTAGE, pgettext_lazy('Voucher discount type', '%')))PRODUCT_TYPE = 'product'CATEGORY_TYPE = 'category'@@ -67,41 +67,49 @@ class Voucher(models.Model):VALUE_TYPE = 'value'TYPE_CHOICES = (- (VALUE_TYPE, pgettext_lazy('voucher', 'All purchases')),- (PRODUCT_TYPE, pgettext_lazy('voucher', 'One product')),- (CATEGORY_TYPE, pgettext_lazy('voucherl', 'A category of products')),- (SHIPPING_TYPE, pgettext_lazy('voucher', 'Shipping')))+ (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(- pgettext_lazy('voucher', 'discount for'), max_length=20,+ pgettext_lazy('Voucher field', 'discount for'), max_length=20,choices=TYPE_CHOICES, default=VALUE_TYPE)name = models.CharField(- pgettext_lazy('voucher', 'name'), max_length=255, null=True,+ pgettext_lazy('Voucher field', 'name'), max_length=255, null=True,blank=True)code = models.CharField(- pgettext_lazy('voucher', 'code'), max_length=12, unique=True,+ pgettext_lazy('Voucher field', 'code'), max_length=12, unique=True,db_index=True)usage_limit = models.PositiveIntegerField(- pgettext_lazy('voucher', 'usage limit'), null=True, blank=True)+ pgettext_lazy('Voucher field', 'usage limit'), null=True, blank=True)used = models.PositiveIntegerField(default=0, editable=False)start_date = models.DateField(- pgettext_lazy('voucher', 'start date'), default=date.today)+ pgettext_lazy('Voucher field', 'start date'), default=date.today)end_date = models.DateField(- pgettext_lazy('voucher', 'end date'), null=True, blank=True)+ pgettext_lazy('Voucher field', 'end date'), null=True, blank=True)discount_value_type = models.CharField(- pgettext_lazy('voucher', 'discount type'), max_length=10,+ pgettext_lazy('Voucher field', 'discount type'), max_length=10,choices=DISCOUNT_VALUE_TYPE_CHOICES, default=DISCOUNT_VALUE_FIXED)discount_value = models.DecimalField(- pgettext_lazy('voucher', 'discount value'), max_digits=12,+ pgettext_lazy('Voucher field', 'discount value'), max_digits=12,decimal_places=2)# not mandatory fields, usage depends on type- product = models.ForeignKey('product.Product', blank=True, null=True)- category = models.ForeignKey('product.Category', blank=True, null=True)- apply_to = models.CharField(max_length=20, blank=True, null=True)- limit = PriceField(max_digits=12, decimal_places=2, null=True,- blank=True, currency=settings.DEFAULT_CURRENCY)+ product = models.ForeignKey(+ 'product.Product', blank=True, null=True,+ verbose_name=pgettext_lazy('Voucher field', 'product'))+ category = models.ForeignKey(+ 'product.Category', blank=True, null=True,+ verbose_name=pgettext_lazy('Voucher field', 'category'))+ apply_to = models.CharField(+ pgettext_lazy('Voucher field', 'apply to'),+ max_length=20, blank=True, null=True)+ limit = PriceField(+ pgettext_lazy('Voucher field', 'limit'),+ max_digits=12, decimal_places=2, null=True,+ blank=True, currency=settings.DEFAULT_CURRENCY)objects = VoucherQueryset.as_manager()@@ -110,6 +118,10 @@ class Voucher(models.Model):return (self.discount_value == Decimal(100) andself.discount_value_type == Voucher.DISCOUNT_VALUE_PERCENTAGE)+ class Meta:+ verbose_name = pgettext_lazy('Voucher model', 'voucher')+ verbose_name_plural = pgettext_lazy('Voucher model', 'vouchers')+def __str__(self):if self.name:return self.name@@ -117,23 +129,23 @@ class Voucher(models.Model):self.discount_value, self.get_discount_value_type_display())if self.type == Voucher.SHIPPING_TYPE:if self.is_free:- return pgettext('voucher', 'Free shipping')+ return pgettext('Voucher type', 'Free shipping')else:- return pgettext('voucher', '%(discount)s off shipping') % {+ return pgettext('Voucher type', '%(discount)s off shipping') % {'discount': discount}if self.type == Voucher.PRODUCT_TYPE:- return pgettext('voucher', '%(discount)s off %(product)s') % {+ return pgettext('Voucher type', '%(discount)s off %(product)s') % {'discount': discount, 'product': self.product}if self.type == Voucher.CATEGORY_TYPE:- return pgettext('voucher', '%(discount)s off %(category)s') % {+ return pgettext('Voucher type', '%(discount)s off %(category)s') % {'discount': discount, 'category': self.category}- return pgettext('voucher', '%(discount)s off') % {'discount': discount}+ return pgettext('Voucher type', '%(discount)s off') % {'discount': discount}def get_apply_to_display(self):if self.type == Voucher.SHIPPING_TYPE and self.apply_to:return countries.name(self.apply_to)if self.type == Voucher.SHIPPING_TYPE:- return pgettext('voucher', 'Any country')+ 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)@@ -162,7 +174,7 @@ class Voucher(models.Model):limit = self.limit if self.limit is not None else valueif value < limit:msg = pgettext(- 'voucher',+ 'Voucher not applicable','This offer is only valid for orders over %(amount)s.')raise NotApplicable(msg % {'amount': net(limit)})@@ -175,17 +187,17 @@ class Voucher(models.Model):elif self.type == Voucher.SHIPPING_TYPE:if not checkout.is_shipping_required:msg = pgettext(- 'voucher', 'Your order does not require shipping.')+ 'Voucher not applicable', 'Your order does not require shipping.')raise NotApplicable(msg)shipping_method = checkout.shipping_methodif not shipping_method:msg = pgettext(- 'voucher', 'Please select a shipping method first.')+ 'Voucher not applicable', 'Please select a shipping method first.')raise NotApplicable(msg)if (self.apply_to andshipping_method.country_code != self.apply_to):msg = pgettext(- 'voucher', 'This offer is only valid in %(country)s.')+ 'Voucher not applicable', 'This offer is only valid in %(country)s.')raise NotApplicable(msg % {'country': self.get_apply_to_display()})cart_total = checkout.get_subtotal()@@ -203,7 +215,7 @@ class Voucher(models.Model):checkout.cart, self.category)))if len(prices) == 0:msg = pgettext(- 'voucher', 'This offer is only valid for selected items.')+ 'Voucher not applicable', 'This offer is only valid for selected items.')raise NotApplicable(msg)if self.apply_to == Voucher.APPLY_TO_ALL_PRODUCTS:discounts = (@@ -227,18 +239,27 @@ class Sale(models.Model):PERCENTAGE = 'percentage'DISCOUNT_TYPE_CHOICES = (- (FIXED, pgettext_lazy('discount_type', settings.DEFAULT_CURRENCY)),- (PERCENTAGE, pgettext_lazy('discount_type', '%')))+ (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)- 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)+ name = models.CharField(pgettext_lazy('Sale (discount) field', 'name'), max_length=255)+ type = models.CharField(+ pgettext_lazy('Sale (discount) field', 'type'),+ max_length=10, choices=DISCOUNT_TYPE_CHOICES, default=FIXED)+ value = models.DecimalField(+ pgettext_lazy('Sale (discount) field', 'value'),+ max_digits=12, decimal_places=2, default=0)+ products = models.ManyToManyField(+ 'product.Product', blank=True,+ verbose_name=pgettext_lazy('Sale (discount) field', 'products'))+ categories = models.ManyToManyField(+ 'product.Category', blank=True,+ verbose_name=pgettext_lazy('Sale (discount) field', 'categories'))class Meta:app_label = 'discount'+ verbose_name = pgettext_lazy('Sale (discount) model', 'sale')+ verbose_name_plural = pgettext_lazy('Sales (discounts) model', 'sales')def __repr__(self):return 'Sale(name=%r, value=%r, type=%s)' % (@@ -268,11 +289,17 @@ class Sale(models.Model):discounted_products = [p.pk for p in self.products.all()]discounted_categories = list(self.categories.all())if discounted_products and variant.product.pk not in discounted_products:- raise NotApplicable('Discount not applicable for this product')+ raise NotApplicable(+ pgettext(+ 'Voucher not applicable',+ 'Discount not applicable for this product'))if (discounted_categories and not- self._product_has_category_discount(- variant.product, discounted_categories)):- raise NotApplicable('Discount too high for this product')+ self._product_has_category_discount(+ variant.product, discounted_categories)):+ raise NotApplicable(+ pgettext(+ 'Voucher not applicable',+ 'Discount too high for this product'))return self.get_discount()