Skip to content

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

Add attribute signals for deleting AttributeValue

IKarbowiak7288275f0e88
diff --git a/saleor/attribute/__init__.py b/saleor/attribute/__init__.py
index f2d6665fda..d37d14f6ad 100644
--- a/saleor/attribute/__init__.py
+++ b/saleor/attribute/__init__.py
@@ -1,3 +1,6 @@
+default_app_config = "saleor.attribute.app.AttributeAppConfig"
+
+
class AttributeInputType:
"""The type that we expect to render the attribute's values as."""
diff --git a/saleor/attribute/app.py b/saleor/attribute/app.py
new file mode 100644
index 0000000000..c7c5f1eb77
--- /dev/null
+++ b/saleor/attribute/app.py
@@ -0,0 +1,16 @@
+from django.apps import AppConfig
+from django.db.models.signals import post_delete
+
+
+class AttributeAppConfig(AppConfig):
+ name = "saleor.attribute"
+
+ def ready(self):
+ from .models import AttributeValue
+ from .signals import delete_attribute_value_file
+
+ post_delete.connect(
+ delete_attribute_value_file,
+ sender=AttributeValue,
+ dispatch_uid="delete_attribute_value_file",
+ )
diff --git a/saleor/attribute/signals.py b/saleor/attribute/signals.py
new file mode 100644
index 0000000000..55a5f8f56c
--- /dev/null
+++ b/saleor/attribute/signals.py
@@ -0,0 +1,6 @@
+from ..core.tasks import delete_from_storage
+
+
+def delete_attribute_value_file(sender, instance, **kwargs):
+ if file_url := instance.file_url:
+ delete_from_storage.delay(file_url)
diff --git a/saleor/graphql/attribute/tests/mutations/test_attribute_delete.py b/saleor/graphql/attribute/tests/mutations/test_attribute_delete.py
index 983c49b8ca..080d588044 100644
--- a/saleor/graphql/attribute/tests/mutations/test_attribute_delete.py
+++ b/saleor/graphql/attribute/tests/mutations/test_attribute_delete.py
@@ -1,18 +1,11 @@
+from unittest.mock import patch
+
import graphene
import pytest
from ....tests.utils import get_graphql_content
-
-def test_delete_attribute(
- staff_api_client,
- color_attribute,
- permission_manage_product_types_and_attributes,
- product_type,
-):
- # given
- attribute = color_attribute
- query = """
+ATTRIBUTE_DELETE_MUTATION = """
mutation deleteAttribute($id: ID!) {
attributeDelete(id: $id) {
errors {
@@ -24,7 +17,49 @@ def test_delete_attribute(
}
}
}
- """
+"""
+
+
+@patch("saleor.attribute.signals.delete_from_storage.delay")
+def test_delete_attribute(
+ delete_from_storage_mock,
+ staff_api_client,
+ color_attribute,
+ permission_manage_product_types_and_attributes,
+ product_type,
+):
+ # given
+ attribute = color_attribute
+ query = ATTRIBUTE_DELETE_MUTATION
+ node_id = graphene.Node.to_global_id("Attribute", attribute.id)
+ variables = {"id": node_id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_product_types_and_attributes]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["attributeDelete"]
+ assert data["attribute"]["id"] == variables["id"]
+ with pytest.raises(attribute._meta.model.DoesNotExist):
+ attribute.refresh_from_db()
+ delete_from_storage_mock.assert_not_called()
+
+
+@patch("saleor.attribute.signals.delete_from_storage.delay")
+def test_delete_file_attribute(
+ delete_from_storage_mock,
+ staff_api_client,
+ file_attribute,
+ permission_manage_product_types_and_attributes,
+ product_type,
+):
+ # given
+ attribute = file_attribute
+ paths = {value.file_url for value in attribute.values.all()}
+ query = ATTRIBUTE_DELETE_MUTATION
node_id = graphene.Node.to_global_id("Attribute", attribute.id)
variables = {"id": node_id}
@@ -39,3 +74,5 @@ def test_delete_attribute(
assert data["attribute"]["id"] == variables["id"]
with pytest.raises(attribute._meta.model.DoesNotExist):
attribute.refresh_from_db()
+ assert delete_from_storage_mock.call_count == len(paths)
+ assert {call.args[0] for call in delete_from_storage_mock.call_args_list} == paths
diff --git a/saleor/graphql/attribute/tests/mutations/test_attribute_value_delete.py b/saleor/graphql/attribute/tests/mutations/test_attribute_value_delete.py
index 2cdd7b672f..233fc5eab4 100644
--- a/saleor/graphql/attribute/tests/mutations/test_attribute_value_delete.py
+++ b/saleor/graphql/attribute/tests/mutations/test_attribute_value_delete.py
@@ -1,8 +1,23 @@
+from unittest.mock import patch
+
import graphene
import pytest
+ATTRIBUTE_VALUE_DELETE_MUTATION = """
+ mutation updateChoice($id: ID!) {
+ attributeValueDelete(id: $id) {
+ attributeValue {
+ name
+ slug
+ }
+ }
+ }
+"""
+
+@patch("saleor.attribute.signals.delete_from_storage.delay")
def test_delete_attribute_value(
+ delete_from_storage_mock,
staff_api_client,
color_attribute,
pink_attribute_value,
@@ -10,16 +25,32 @@ def test_delete_attribute_value(
):
# given
value = color_attribute.values.get(name="Red")
- query = """
- mutation updateChoice($id: ID!) {
- attributeValueDelete(id: $id) {
- attributeValue {
- name
- slug
- }
- }
- }
- """
+ query = ATTRIBUTE_VALUE_DELETE_MUTATION
+ node_id = graphene.Node.to_global_id("AttributeValue", value.id)
+ variables = {"id": node_id}
+
+ # when
+ staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_product_types_and_attributes]
+ )
+
+ # then
+ with pytest.raises(value._meta.model.DoesNotExist):
+ value.refresh_from_db()
+ delete_from_storage_mock.assert_not_called()
+
+
+@patch("saleor.attribute.signals.delete_from_storage.delay")
+def test_delete_file_attribute_value(
+ delete_from_storage_mock,
+ staff_api_client,
+ file_attribute,
+ permission_manage_product_types_and_attributes,
+):
+ # given
+ value = file_attribute.values.first()
+ file_url = value.file_url
+ query = ATTRIBUTE_VALUE_DELETE_MUTATION
node_id = graphene.Node.to_global_id("AttributeValue", value.id)
variables = {"id": node_id}
@@ -31,3 +62,4 @@ def test_delete_attribute_value(
# then
with pytest.raises(value._meta.model.DoesNotExist):
value.refresh_from_db()
+ delete_from_storage_mock.assert_called_once_with(file_url)