From 28d308105dc51a18f9d5479f96ac838295db5211 Mon Sep 17 00:00:00 2001 From: kadewu Date: Wed, 4 Jan 2023 10:22:04 +0100 Subject: [PATCH] Prepare for moving orderSettings to Channel --- .../migrations/0006_order_settings_fields.py | 23 ++++++++ .../0007_order_settings_per_channel.py | 29 +++++++++++ saleor/channel/models.py | 5 ++ saleor/graphql/shop/mutations.py | 8 +-- saleor/graphql/shop/tests/test_shop.py | 52 +++++++++++++++++-- 5 files changed, 110 insertions(+), 7 deletions(-) create mode 100644 saleor/channel/migrations/0006_order_settings_fields.py create mode 100644 saleor/channel/migrations/0007_order_settings_per_channel.py diff --git a/saleor/channel/migrations/0006_order_settings_fields.py b/saleor/channel/migrations/0006_order_settings_fields.py new file mode 100644 index 000000000000..4f1de06ff6eb --- /dev/null +++ b/saleor/channel/migrations/0006_order_settings_fields.py @@ -0,0 +1,23 @@ +# Generated by Django 3.2.16 on 2022-12-08 09:00 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("channel", "0005_channel_allocation_strategy"), + ] + + operations = [ + migrations.AddField( + model_name="channel", + name="automatically_confirm_all_new_orders", + field=models.BooleanField(default=True, null=True), + ), + migrations.AddField( + model_name="channel", + name="automatically_fulfill_non_shippable_gift_card", + field=models.BooleanField(default=True, null=True), + ), + ] diff --git a/saleor/channel/migrations/0007_order_settings_per_channel.py b/saleor/channel/migrations/0007_order_settings_per_channel.py new file mode 100644 index 000000000000..73cd5bbc9280 --- /dev/null +++ b/saleor/channel/migrations/0007_order_settings_per_channel.py @@ -0,0 +1,29 @@ +from django.db import migrations + + +def set_order_settings(apps, schema_editor): + SiteSettings = apps.get_model("site", "SiteSettings") + Channel = apps.get_model("channel", "Channel") + + site_settings = SiteSettings.objects.first() + + Channel.objects.update( + automatically_confirm_all_new_orders=( + site_settings.automatically_confirm_all_new_orders + ), + automatically_fulfill_non_shippable_gift_card=( + site_settings.automatically_fulfill_non_shippable_gift_card + ), + ) + + +class Migration(migrations.Migration): + + dependencies = [ + ("channel", "0006_order_settings_fields"), + ("site", "0032_gift_card_settings"), + ] + + operations = [ + migrations.RunPython(set_order_settings, migrations.RunPython.noop), + ] diff --git a/saleor/channel/models.py b/saleor/channel/models.py index f6f99f276ce9..e3c07d971cf5 100644 --- a/saleor/channel/models.py +++ b/saleor/channel/models.py @@ -17,6 +17,11 @@ class Channel(models.Model): choices=AllocationStrategy.CHOICES, default=AllocationStrategy.PRIORITIZE_SORTING_ORDER, ) + automatically_confirm_all_new_orders = models.BooleanField(default=True, null=True) + automatically_fulfill_non_shippable_gift_card = models.BooleanField( + default=True, + null=True, + ) class Meta: ordering = ("slug",) diff --git a/saleor/graphql/shop/mutations.py b/saleor/graphql/shop/mutations.py index e4e2b953e4a9..0730c5958b0e 100644 --- a/saleor/graphql/shop/mutations.py +++ b/saleor/graphql/shop/mutations.py @@ -2,6 +2,7 @@ from django.core.exceptions import ValidationError from ...account import models as account_models +from ...channel import models as channel_models from ...core.error_codes import ShopErrorCode from ...core.permissions import GiftcardPermissions, OrderPermissions, SitePermissions from ...core.utils.url import validate_storefront_url @@ -403,15 +404,16 @@ def perform_mutation(cls, _root, info: ResolveInfo, /, **data): ] site = get_site_promise(info.context).get() instance = site.settings - update_fields = [] + update_fields = {} for field in FIELDS: value = data["input"].get(field) if value is not None: setattr(instance, field, value) - update_fields.append(field) + update_fields[field] = value if update_fields: - instance.save(update_fields=update_fields) + instance.save(update_fields=update_fields.keys()) + channel_models.Channel.objects.update(**update_fields) return OrderSettingsUpdate(order_settings=instance) diff --git a/saleor/graphql/shop/tests/test_shop.py b/saleor/graphql/shop/tests/test_shop.py index f9eb46343431..989864a46bc4 100644 --- a/saleor/graphql/shop/tests/test_shop.py +++ b/saleor/graphql/shop/tests/test_shop.py @@ -1339,71 +1339,115 @@ def test_staff_notification_update_mutation_with_empty_email( def test_order_settings_update_by_staff( - staff_api_client, permission_manage_orders, site_settings + staff_api_client, permission_manage_orders, site_settings, channel_USD, channel_PLN ): + # given assert site_settings.automatically_confirm_all_new_orders is True staff_api_client.user.user_permissions.add(permission_manage_orders) + + # when response = staff_api_client.post_graphql( ORDER_SETTINGS_UPDATE_MUTATION, {"confirmOrders": False, "fulfillGiftCards": False}, ) content = get_graphql_content(response) + + # then response_settings = content["data"]["orderSettingsUpdate"]["orderSettings"] assert response_settings["automaticallyConfirmAllNewOrders"] is False assert response_settings["automaticallyFulfillNonShippableGiftCard"] is False site_settings.refresh_from_db() assert site_settings.automatically_confirm_all_new_orders is False assert site_settings.automatically_fulfill_non_shippable_gift_card is False + channel_PLN.refresh_from_db() + channel_USD.refresh_from_db() + assert channel_PLN.automatically_confirm_all_new_orders is False + assert channel_PLN.automatically_fulfill_non_shippable_gift_card is False + assert channel_USD.automatically_confirm_all_new_orders is False + assert channel_USD.automatically_fulfill_non_shippable_gift_card is False def test_order_settings_update_by_staff_nothing_changed( - staff_api_client, permission_manage_orders, site_settings + staff_api_client, permission_manage_orders, site_settings, channel_USD, channel_PLN ): + # given assert site_settings.automatically_confirm_all_new_orders is True staff_api_client.user.user_permissions.add(permission_manage_orders) + + # when response = staff_api_client.post_graphql( ORDER_SETTINGS_UPDATE_MUTATION, {}, ) content = get_graphql_content(response) + + # then response_settings = content["data"]["orderSettingsUpdate"]["orderSettings"] assert response_settings["automaticallyConfirmAllNewOrders"] is True assert response_settings["automaticallyFulfillNonShippableGiftCard"] is True site_settings.refresh_from_db() assert site_settings.automatically_confirm_all_new_orders is True assert site_settings.automatically_fulfill_non_shippable_gift_card is True + channel_PLN.refresh_from_db() + channel_USD.refresh_from_db() + assert channel_PLN.automatically_confirm_all_new_orders is True + assert channel_PLN.automatically_fulfill_non_shippable_gift_card is True + assert channel_USD.automatically_confirm_all_new_orders is True + assert channel_USD.automatically_fulfill_non_shippable_gift_card is True def test_order_settings_update_by_app( - app_api_client, permission_manage_orders, site_settings + app_api_client, permission_manage_orders, site_settings, channel_USD, channel_PLN ): + # given assert site_settings.automatically_confirm_all_new_orders is True app_api_client.app.permissions.set([permission_manage_orders]) + + # when response = app_api_client.post_graphql( ORDER_SETTINGS_UPDATE_MUTATION, {"confirmOrders": False, "fulfillGiftCards": False}, ) content = get_graphql_content(response) + + # then response_settings = content["data"]["orderSettingsUpdate"]["orderSettings"] assert response_settings["automaticallyConfirmAllNewOrders"] is False assert response_settings["automaticallyFulfillNonShippableGiftCard"] is False site_settings.refresh_from_db() assert site_settings.automatically_confirm_all_new_orders is False assert site_settings.automatically_fulfill_non_shippable_gift_card is False + channel_PLN.refresh_from_db() + channel_USD.refresh_from_db() + assert channel_PLN.automatically_confirm_all_new_orders is False + assert channel_PLN.automatically_fulfill_non_shippable_gift_card is False + assert channel_USD.automatically_confirm_all_new_orders is False + assert channel_USD.automatically_fulfill_non_shippable_gift_card is False def test_order_settings_update_by_user_without_permissions( - user_api_client, permission_manage_orders, site_settings + user_api_client, permission_manage_orders, site_settings, channel_USD, channel_PLN ): + # given assert site_settings.automatically_confirm_all_new_orders is True + + # when response = user_api_client.post_graphql( ORDER_SETTINGS_UPDATE_MUTATION, {"confirmOrders": False, "fulfillGiftCards": False}, ) + + # then assert_no_permission(response) site_settings.refresh_from_db() assert site_settings.automatically_confirm_all_new_orders is True assert site_settings.automatically_fulfill_non_shippable_gift_card is True + channel_PLN.refresh_from_db() + channel_USD.refresh_from_db() + assert channel_PLN.automatically_confirm_all_new_orders is True + assert channel_PLN.automatically_fulfill_non_shippable_gift_card is True + assert channel_USD.automatically_confirm_all_new_orders is True + assert channel_USD.automatically_fulfill_non_shippable_gift_card is True ORDER_SETTINGS_QUERY = """