Skip to content

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

Remove gettext occurrences from discount

IKarbowiak6b6d6a3668c3
diff --git a/saleor/discount/__init__.py b/saleor/discount/__init__.py
index f88fc9e776..74030bbba3 100644
--- a/saleor/discount/__init__.py
+++ b/saleor/discount/__init__.py
@@ -2,7 +2,6 @@ from dataclasses import dataclass
from typing import TYPE_CHECKING, List, Set, Union
from django.conf import settings
-from django.utils.translation import pgettext_lazy
if TYPE_CHECKING:
# flake8: noqa
@@ -14,8 +13,8 @@ class DiscountValueType:
PERCENTAGE = "percentage"
CHOICES = [
- (FIXED, pgettext_lazy("Discount type", settings.DEFAULT_CURRENCY)),
- (PERCENTAGE, pgettext_lazy("Discount type", "%")),
+ (FIXED, settings.DEFAULT_CURRENCY),
+ (PERCENTAGE, "%"),
]
@@ -25,14 +24,9 @@ class VoucherType:
SPECIFIC_PRODUCT = "specific_product"
CHOICES = [
- (ENTIRE_ORDER, pgettext_lazy("Voucher: discount for", "Entire order")),
- (SHIPPING, pgettext_lazy("Voucher: discount for", "Shipping")),
- (
- SPECIFIC_PRODUCT,
- pgettext_lazy(
- "Voucher: discount for", "Specific products, collections and categories"
- ),
- ),
+ (ENTIRE_ORDER, "Entire order"),
+ (SHIPPING, "Shipping"),
+ (SPECIFIC_PRODUCT, "Specific products, collections and categories"),
]
diff --git a/saleor/discount/models.py b/saleor/discount/models.py
index 246b8b6ce3..8ae7eb82c2 100644
--- a/saleor/discount/models.py
+++ b/saleor/discount/models.py
@@ -5,7 +5,6 @@ from django.conf import settings
from django.db import models
from django.db.models import F, Q
from django.utils import timezone
-from django.utils.translation import pgettext, pgettext_lazy
from django_countries.fields import CountryField
from django_prices.models import MoneyField
from django_prices.templatetags.prices import amount
@@ -101,15 +100,11 @@ class Voucher(models.Model):
)
if self.type == VoucherType.SHIPPING:
if self.is_free:
- return pgettext("Voucher type", "Free shipping")
- return pgettext("Voucher type", "%(discount)s off shipping") % {
- "discount": discount
- }
+ return "Free shipping"
+ return f"{discount} off shipping"
if self.type == VoucherType.SPECIFIC_PRODUCT:
- return pgettext("Voucher type", "%(discount)s off specific products") % {
- "discount": discount
- }
- return pgettext("Voucher type", "%(discount)s off") % {"discount": discount}
+ return f"%{discount} off specific products"
+ return f"{discount} off"
@property
def is_free(self):
@@ -135,27 +130,18 @@ class Voucher(models.Model):
def validate_min_spent(self, value: Money):
if self.min_spent and value < self.min_spent:
- msg = pgettext(
- "Voucher not applicable",
- "This offer is only valid for orders over %(amount)s.",
- )
- raise NotApplicable(
- msg % {"amount": amount(self.min_spent)}, min_spent=self.min_spent
- )
+ msg = f"This offer is only valid for orders over {amount(self.min_spent)}."
+ raise NotApplicable(msg, min_spent=self.min_spent)
def validate_min_checkout_items_quantity(self, quantity):
min_checkout_items_quantity = self.min_checkout_items_quantity
if min_checkout_items_quantity and min_checkout_items_quantity > quantity:
- msg = pgettext(
- "Voucher not applicable",
- (
- "This offer is only valid for orders with a minimum of "
- "%(min_checkout_items_quantity)d quantity."
- ),
+ msg = (
+ "This offer is only valid for orders with a minimum of "
+ f"{min_checkout_items_quantity} quantity."
)
raise NotApplicable(
- msg % {"min_checkout_items_quantity": min_checkout_items_quantity},
- min_checkout_items_quantity=min_checkout_items_quantity,
+ msg, min_checkout_items_quantity=min_checkout_items_quantity,
)
def validate_once_per_customer(self, customer_email):
@@ -163,9 +149,7 @@ class Voucher(models.Model):
voucher=self, customer_email=customer_email
)
if voucher_customer:
- msg = pgettext(
- "Voucher not applicable", "This offer is valid only once per customer."
- )
+ msg = "This offer is valid only once per customer."
raise NotApplicable(msg)
@@ -230,7 +214,7 @@ class Sale(models.Model):
permissions = (
(
DiscountPermissions.MANAGE_DISCOUNTS.codename,
- pgettext_lazy("Permission description", "Manage sales and vouchers."),
+ "Manage sales and vouchers.",
),
)
diff --git a/saleor/discount/utils.py b/saleor/discount/utils.py
index b43e001725..7f21a578d1 100644
--- a/saleor/discount/utils.py
+++ b/saleor/discount/utils.py
@@ -4,7 +4,6 @@ from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Set, Union
from django.db.models import F
from django.utils import timezone
-from django.utils.translation import pgettext
from prices import Money
from ..checkout import calculations
@@ -38,12 +37,7 @@ def add_voucher_usage_by_customer(voucher: "Voucher", customer_email: str) -> No
voucher=voucher, customer_email=customer_email
)
if voucher_customer:
- raise NotApplicable(
- pgettext(
- "Voucher not applicable",
- ("This offer is only valid once per customer."),
- )
- )
+ raise NotApplicable("This offer is only valid once per customer.")
VoucherCustomer.objects.create(voucher=voucher, customer_email=customer_email)
@@ -66,9 +60,7 @@ def get_product_discount_on_sale(
)
if is_product_on_sale:
return discount.sale.get_discount()
- raise NotApplicable(
- pgettext("Voucher not applicable", "Discount not applicable for this product")
- )
+ raise NotApplicable("Discount not applicable for this product")
def get_product_discounts(product: "Product", discounts: "DiscountsListType") -> Money:
diff --git a/tests/test_discounts.py b/tests/test_discounts.py
index c6f71406b5..03646f4622 100644
--- a/tests/test_discounts.py
+++ b/tests/test_discounts.py
@@ -34,6 +34,19 @@ def test_valid_voucher_min_spent_amount(min_spent_amount, value):
voucher.validate_min_spent(value)
+def test_valid_voucher_min_checkout_items_quantity(voucher):
+ voucher.min_checkout_items_quantity = 3
+ voucher.save()
+
+ with pytest.raises(NotApplicable) as e:
+ voucher.validate_min_checkout_items_quantity(2)
+
+ assert (
+ str(e.value)
+ == "This offer is only valid for orders with a minimum of 3 quantity."
+ )
+
+
@pytest.mark.integration
@pytest.mark.django_db(transaction=True)
def test_variant_discounts(product):