Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Trigger price relaculation when sale is deleted (#15570)
Maciej Korycinski905c3846d444
diff --git a/saleor/graphql/discount/mutations/sale/sale_channel_listing_update.py b/saleor/graphql/discount/mutations/sale/sale_channel_listing_update.pyindex 27cab4bf37..e335b8ebff 100644--- a/saleor/graphql/discount/mutations/sale/sale_channel_listing_update.py+++ b/saleor/graphql/discount/mutations/sale/sale_channel_listing_update.py@@ -12,6 +12,7 @@ from .....discount.error_codes import DiscountErrorCodefrom .....discount.models import Promotion, PromotionRulefrom .....discount.utils import mark_catalogue_promotion_rules_as_dirtyfrom .....permission.enums import DiscountPermissions+from .....product.utils.product import mark_products_in_channels_as_dirtyfrom ....channel import ChannelContextfrom ....channel.mutations import BaseChannelListingMutationfrom ....core import ResolveInfo@@ -26,6 +27,7 @@ from ...dataloaders import (PromotionRulesByPromotionIdLoader,SaleChannelListingByPromotionIdLoader,)+from ...utils import get_products_for_ruleclass SaleChannelListingAddInput(BaseInputObjectType):@@ -220,10 +222,22 @@ class SaleChannelListingUpdate(BaseChannelListingMutation):rule: PromotionRule,cleaned_input: dict,):+ add_channels = cleaned_input.get("add_channels", [])+ remove_channels = cleaned_input.get("remove_channels", [])+ if remove_channels and not add_channels:+ # In case of only removing the channels, we need to mark the product to be+ # recalculated.+ product_ids = list(get_products_for_rule(rule).values_list("id", flat=True))+ mark_as_dirty_func = mark_products_in_channels_as_dirty+ func_arg = {int(channel_id): product_ids for channel_id in remove_channels}+ else:+ mark_as_dirty_func = mark_catalogue_promotion_rules_as_dirty # type: ignore+ func_arg = [promotion.pk] # type: ignore[assignment]+with traced_atomic_transaction():cls.add_channels(promotion, rule, cleaned_input.get("add_channels", []))cls.remove_channels(promotion, cleaned_input.get("remove_channels", []))- cls.call_event(mark_catalogue_promotion_rules_as_dirty, [promotion.pk])+ cls.call_event(mark_as_dirty_func, func_arg)@classmethoddef get_instance(cls, id):diff --git a/saleor/graphql/discount/mutations/sale/sale_delete.py b/saleor/graphql/discount/mutations/sale/sale_delete.pyindex a387ea2392..2a5e0a4b9d 100644--- a/saleor/graphql/discount/mutations/sale/sale_delete.py+++ b/saleor/graphql/discount/mutations/sale/sale_delete.py@@ -1,4 +1,5 @@import graphene+from django.db.models import Exists, OuterReffrom .....core.tracing import traced_atomic_transactionfrom .....discount import models@@ -56,8 +57,16 @@ class SaleDelete(ModelDeleteMutation):promotion = cls.get_promotion_instance(id)old_sale_id = promotion.old_sale_idpromotion_id = promotion.id- rule = promotion.rules.first()- channels_ids = rule.channels.values_list("id", flat=True)++ rules = promotion.rules.all()+ rule = rules[0]++ PromotionRuleChannel = rules.model.channels.through+ channel_ids = list(+ PromotionRuleChannel.objects.filter(+ Exists(rules.filter(id=OuterRef("promotionrule_id")))+ ).values_list("channel_id", flat=True)+ )previous_catalogue = cls.get_catalogue_info(rule)product_ids = cls.get_product_ids(rule)with traced_atomic_transaction():@@ -71,9 +80,8 @@ class SaleDelete(ModelDeleteMutation):cls.call_event(manager.sale_deleted, promotion, previous_catalogue)cls.call_event(mark_products_in_channels_as_dirty,- {channel_id: product_ids for channel_id in channels_ids},+ {channel_id: product_ids for channel_id in channel_ids},)-return response@classmethoddiff --git a/saleor/graphql/discount/tests/mutations/test_sale_channel_listing_update.py b/saleor/graphql/discount/tests/mutations/test_sale_channel_listing_update.pyindex 4a92244054..c138c01ac7 100644--- a/saleor/graphql/discount/tests/mutations/test_sale_channel_listing_update.py+++ b/saleor/graphql/discount/tests/mutations/test_sale_channel_listing_update.py@@ -2,7 +2,9 @@ import graphenefrom .....discount import RewardValueTypefrom .....discount.error_codes import DiscountErrorCode+from .....product.models import ProductChannelListingfrom ....tests.utils import assert_negative_positive_decimal_value, get_graphql_content+from ...utils import get_products_for_ruleSALE_CHANNEL_LISTING_UPDATE_MUTATION = """mutation UpdateSaleChannelListing(@@ -193,6 +195,8 @@ def test_sale_channel_listing_remove_channels(sale_id = graphene.Node.to_global_id("Sale", promotion.old_sale_id)channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)+ rule = promotion.rules.filter(channels=channel_PLN).get()+ product_ids = list(get_products_for_rule(rule).values_list("id", flat=True))variables = {"id": sale_id,"input": {"removeChannels": [channel_id]},@@ -218,9 +222,11 @@ def test_sale_channel_listing_remove_channels(promotion.refresh_from_db()rules = promotion.rules.all()assert len(rules) == 1-- for rule in promotion.rules.all():- assert rule.variants_dirty is True+ assert not ProductChannelListing.objects.filter(+ product_id__in=product_ids,+ channel_id=channel_USD.id,+ discounted_price_dirty=False,+ )def test_sale_channel_listing_remove_all_channels(@@ -242,6 +248,9 @@ def test_sale_channel_listing_remove_all_channels(reward_value_type = rule.reward_value_typepredicate = rule.catalogue_predicate+ rule = promotion.rules.filter(channels=channel_PLN).get()+ product_ids = list(get_products_for_rule(rule).values_list("id", flat=True))+variables = {"id": sale_id,"input": {"removeChannels": channel_ids},@@ -269,8 +278,11 @@ def test_sale_channel_listing_remove_all_channels(assert rules[0].reward_value_type == reward_value_typeassert rules[0].catalogue_predicate == predicate- for rule in promotion.rules.all():- assert rule.variants_dirty is True+ assert not ProductChannelListing.objects.filter(+ product_id__in=product_ids,+ channel_id=channel_USD.id,+ discounted_price_dirty=False,+ )def test_sale_channel_listing_add_update_remove_channels(diff --git a/saleor/graphql/product/tests/test_product_discounted_price.py b/saleor/graphql/product/tests/test_product_discounted_price.pyindex 0857dfbce7..806d313b52 100644--- a/saleor/graphql/product/tests/test_product_discounted_price.py+++ b/saleor/graphql/product/tests/test_product_discounted_price.py@@ -267,10 +267,11 @@ def test_sale_update_updates_products_discounted_prices(promotionrule__in=promotion.rules.all()).values_list("channel_id", flat=True))- for listing in ProductChannelListing.objects.filter(- product__in=get_current_products_for_rules(rules), channel__in=channel_ids- ):- assert listing.discounted_price_dirty+ assert not ProductChannelListing.objects.filter(+ product__in=get_current_products_for_rules(rules),+ channel__in=channel_ids,+ discounted_price_dirty=False,+ )def test_sale_delete_updates_products_discounted_prices(@@ -295,6 +296,14 @@ def test_sale_delete_updates_products_discounted_prices("""promotion = promotion_converted_from_salevariables = {"id": to_global_id("Sale", promotion.old_sale_id)}+ rules = promotion.rules.all()+ PromotionRuleChannel = PromotionRule.channels.through+ channel_ids = set(+ PromotionRuleChannel.objects.filter(+ promotionrule__in=promotion.rules.all()+ ).values_list("channel_id", flat=True)+ )+ products = list(get_current_products_for_rules(rules))# whenresponse = staff_api_client.post_graphql(@@ -306,19 +315,10 @@ def test_sale_delete_updates_products_discounted_prices(content = get_graphql_content(response)assert content["data"]["saleDelete"]["errors"] == []- rules = promotion.rules.all()- PromotionRuleChannel = PromotionRule.channels.through- channel_ids = set(- PromotionRuleChannel.objects.filter(- promotionrule__in=promotion.rules.all()- ).values_list("channel_id", flat=True)- )- for listing in ProductChannelListing.objects.filter(- product__in=get_current_products_for_rules(rules),- channel__in=channel_ids,- ):- assert listing.discounted_price_dirty+ assert not ProductChannelListing.objects.filter(+ product__in=products, channel__in=channel_ids, discounted_price_dirty=False+ )def test_sale_add_catalogues_updates_products_discounted_prices(