Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add signal for category and collection deleting
IKarbowiak06ef1cb2c710
diff --git a/saleor/graphql/product/tests/test_category.py b/saleor/graphql/product/tests/test_category.pyindex a477665243..77250b960c 100644--- a/saleor/graphql/product/tests/test_category.py+++ b/saleor/graphql/product/tests/test_category.py@@ -768,9 +768,36 @@ MUTATION_CATEGORY_DELETE = """"""+@patch("saleor.product.signals.delete_from_storage")def test_category_delete_mutation(- staff_api_client, category, permission_manage_products+ delete_from_storage_mock,+ staff_api_client,+ category,+ permission_manage_products,+):+ variables = {"id": graphene.Node.to_global_id("Category", category.id)}+ response = staff_api_client.post_graphql(+ MUTATION_CATEGORY_DELETE, variables, permissions=[permission_manage_products]+ )+ content = get_graphql_content(response)+ data = content["data"]["categoryDelete"]+ assert data["category"]["name"] == category.name+ with pytest.raises(category._meta.model.DoesNotExist):+ category.refresh_from_db()++ delete_from_storage_mock.assert_not_called()+++@patch("saleor.product.signals.delete_from_storage")+def test_delete_category_with_background_image(+ delete_from_storage_mock,+ staff_api_client,+ category_with_image,+ permission_manage_products,+ media_root,):+ """Ensure deleting category deletes background image from storage."""+ category = category_with_imagevariables = {"id": graphene.Node.to_global_id("Category", category.id)}response = staff_api_client.post_graphql(MUTATION_CATEGORY_DELETE, variables, permissions=[permission_manage_products]@@ -780,6 +807,7 @@ def test_category_delete_mutation(assert data["category"]["name"] == category.namewith pytest.raises(category._meta.model.DoesNotExist):category.refresh_from_db()+ assert delete_from_storage_mock.called_once_with(category.background_image.name)@patch("saleor.product.utils.update_products_discounted_prices_task")diff --git a/saleor/graphql/product/tests/test_collection.py b/saleor/graphql/product/tests/test_collection.pyindex 68abbe7285..099d478071 100644--- a/saleor/graphql/product/tests/test_collection.py+++ b/saleor/graphql/product/tests/test_collection.py@@ -841,16 +841,47 @@ def test_update_collection_slug_and_name(assert errors[0]["code"] == ProductErrorCode.REQUIRED.name-def test_delete_collection(staff_api_client, collection, permission_manage_products):- query = """- mutation deleteCollection($id: ID!) {- collectionDelete(id: $id) {- collection {- name- }+DELETE_COLLECTION_MUTATION = """+ mutation deleteCollection($id: ID!) {+ collectionDelete(id: $id) {+ collection {+ name}}- """+ }+"""+++@patch("saleor.product.signals.delete_from_storage")+def test_delete_collection(+ delete_from_storage_mock,+ staff_api_client,+ collection,+ permission_manage_products,+):+ query = DELETE_COLLECTION_MUTATION+ collection_id = to_global_id("Collection", collection.id)+ variables = {"id": collection_id}+ response = staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_products]+ )+ content = get_graphql_content(response)+ data = content["data"]["collectionDelete"]["collection"]+ assert data["name"] == collection.name+ with pytest.raises(collection._meta.model.DoesNotExist):+ collection.refresh_from_db()+ delete_from_storage_mock.assert_not_called()+++@patch("saleor.product.signals.delete_from_storage")+def test_delete_collection_with_background_image(+ delete_from_storage_mock,+ staff_api_client,+ collection_with_image,+ permission_manage_products,+):+ query = DELETE_COLLECTION_MUTATION+ collection = collection_with_imagecollection_id = to_global_id("Collection", collection.id)variables = {"id": collection_id}response = staff_api_client.post_graphql(@@ -861,6 +892,7 @@ def test_delete_collection(staff_api_client, collection, permission_manage_produassert data["name"] == collection.namewith pytest.raises(collection._meta.model.DoesNotExist):collection.refresh_from_db()+ assert delete_from_storage_mock.called_once_with(collection.background_image.name)@patch("saleor.plugins.manager.PluginsManager.product_updated")diff --git a/saleor/product/__init__.py b/saleor/product/__init__.pyindex 340776a514..602a59722b 100644--- a/saleor/product/__init__.py+++ b/saleor/product/__init__.py@@ -1,3 +1,6 @@+default_app_config = "saleor.product.app.ProductAppConfig"++class ProductMediaTypes:IMAGE = "IMAGE"VIDEO = "VIDEO"diff --git a/saleor/product/app.py b/saleor/product/app.pynew file mode 100644index 0000000000..751fc5ac8b--- /dev/null+++ b/saleor/product/app.py@@ -0,0 +1,21 @@+from django.apps import AppConfig+from django.db.models.signals import post_delete+++class ProductAppConfig(AppConfig):+ name = "saleor.product"++ def ready(self):+ from .models import Category, Collection+ from .signals import delete_background_image++ post_delete.connect(+ delete_background_image,+ sender=Category,+ dispatch_uid="delete_category_background",+ )+ post_delete.connect(+ delete_background_image,+ sender=Collection,+ dispatch_uid="delete_collection_background",+ )diff --git a/saleor/product/signals.py b/saleor/product/signals.pynew file mode 100644index 0000000000..3c1e1a685a--- /dev/null+++ b/saleor/product/signals.py@@ -0,0 +1,6 @@+from ..core.tasks import delete_from_storage+++def delete_background_image(sender, instance, **kwargs):+ if img := instance.background_image:+ delete_from_storage.delay(img.name)