Skip to content

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

Optimize discounted price calculation

Krzysztof Świątek28b644780a54
diff --git a/saleor/discount/utils.py b/saleor/discount/utils.py
index bab8152f7d..9e37d97526 100644
--- a/saleor/discount/utils.py
+++ b/saleor/discount/utils.py
@@ -46,19 +46,12 @@ def remove_voucher_usage_by_customer(voucher, customer_email):
voucher_customer.delete()
-def are_product_collections_on_sale(product, discount: DiscountInfo):
- """Check if any collection is on sale."""
- discounted_collections = discount.collection_ids
- product_collections = set(c.id for c in product.collections.all())
- return product_collections.intersection(discounted_collections)
-
-
-def get_product_discount_on_sale(product, discount: DiscountInfo):
+def get_product_discount_on_sale(product, product_collections, discount: DiscountInfo):
"""Return discount value if product is on sale or raise NotApplicable."""
is_product_on_sale = (
product.id in discount.product_ids
or product.category_id in discount.category_ids
- or are_product_collections_on_sale(product, discount)
+ or product_collections.intersection(discount.collection_ids)
)
if is_product_on_sale:
return discount.sale.get_discount()
@@ -69,9 +62,10 @@ def get_product_discount_on_sale(product, discount: DiscountInfo):
def get_product_discounts(product, discounts: Iterable[DiscountInfo]):
"""Return discount values for all discounts applicable to a product."""
+ product_collections = set(product.collections.all().values_list("pk", flat=True))
for discount in discounts:
try:
- yield get_product_discount_on_sale(product, discount)
+ yield get_product_discount_on_sale(product, product_collections, discount)
except NotApplicable:
pass
diff --git a/saleor/graphql/translations/schema.py b/saleor/graphql/translations/schema.py
index a815d4887f..7ac99d8f18 100644
--- a/saleor/graphql/translations/schema.py
+++ b/saleor/graphql/translations/schema.py
@@ -104,7 +104,9 @@ class TranslationQueries(graphene.ObjectType):
graphene.ID, description="ID of the object to retrieve.", required=True
),
kind=graphene.Argument(
- TranslatableKinds, required=True, description="Kind of the object to retrieve."
+ TranslatableKinds,
+ required=True,
+ description="Kind of the object to retrieve.",
),
)
diff --git a/tests/test_discounts.py b/tests/test_discounts.py
index 3992d2c5b1..e134bc5fc2 100644
--- a/tests/test_discounts.py
+++ b/tests/test_discounts.py
@@ -141,11 +141,11 @@ def test_sale_applies_to_correct_products(product_type, category):
discount = DiscountInfo(
sale=sale, product_ids={product.id}, category_ids=set(), collection_ids=set()
)
- product_discount = get_product_discount_on_sale(variant.product, discount)
+ product_discount = get_product_discount_on_sale(variant.product, set(), discount)
discounted_price = product_discount(product.price)
assert discounted_price == Money(7, "USD")
with pytest.raises(NotApplicable):
- get_product_discount_on_sale(sec_variant.product, discount)
+ get_product_discount_on_sale(sec_variant.product, set(), discount)
def test_increase_voucher_usage():