Skip to content

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

New events related to attribute values changes (#10035)

SzymJbe244877ac2e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a5e47583f4..bc33e1451c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,6 +17,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Add webhooks `ADDRESS_CREATED`, `ADDRESS_UPDATED` and `ADDRESS_DELETED` - #9860 by @SzymJ
- Add webhooks `STAFF_CREATED`, `STAFF_UPDATED` and `STAFF_DELETED` - #9949 by @SzymJ
- Add webhooks `ATTRIBUTE_CREATED`, `ATTRIBUTE_UPDATED` and `ATTRIBUTE_DELETED` - #9991 by @SzymJ
+- Add webhooks `ATTRIBUTE_VALUE_CREATED`, `ATTRIBUTE_VALUE_UPDATED` and `ATTRIBUTE_VALUE_DELETED` - #10035 by @SzymJ
- Fix returning errors in subscription webhooks payloads - #9905 by @SzymJ
# 3.4.0
diff --git a/saleor/graphql/attribute/bulk_mutations.py b/saleor/graphql/attribute/bulk_mutations.py
index df2da2c14f..51d734f5be 100644
--- a/saleor/graphql/attribute/bulk_mutations.py
+++ b/saleor/graphql/attribute/bulk_mutations.py
@@ -101,7 +101,10 @@ class AttributeValueBulkDelete(ModelBulkDeleteMutation):
@classmethod
def bulk_action(cls, info, queryset):
attributes = {value.attribute for value in queryset}
+ values = list(queryset)
queryset.delete()
+ for value in values:
+ info.context.plugins.attribute_value_deleted(value)
for attribute in attributes:
info.context.plugins.attribute_updated(attribute)
diff --git a/saleor/graphql/attribute/mutations.py b/saleor/graphql/attribute/mutations.py
index 471647eac3..326e5113c9 100644
--- a/saleor/graphql/attribute/mutations.py
+++ b/saleor/graphql/attribute/mutations.py
@@ -701,6 +701,7 @@ class AttributeValueCreate(AttributeMixin, ModelMutation):
@classmethod
def post_save_action(cls, info, instance, cleaned_input):
+ info.context.plugins.attribute_value_created(instance)
info.context.plugins.attribute_updated(instance.attribute)
@@ -753,6 +754,7 @@ class AttributeValueUpdate(AttributeValueCreate):
| Q(Exists(variants.filter(product_id=OuterRef("id"))))
)
update_products_search_vector(products)
+ info.context.plugins.attribute_value_updated(instance)
info.context.plugins.attribute_updated(instance.attribute)
@@ -779,6 +781,7 @@ class AttributeValueDelete(ModelDeleteMutation):
update_products_search_vector(
product_models.Product.objects.filter(id__in=product_ids)
)
+ info.context.plugins.attribute_value_deleted(instance)
info.context.plugins.attribute_updated(instance.attribute)
return response
@@ -839,7 +842,7 @@ class AttributeReorderValues(BaseMutation):
}
)
- values_m2m = attribute.values
+ values_m2m = attribute.values.all()
operations = {}
# Resolve the values
@@ -864,5 +867,9 @@ class AttributeReorderValues(BaseMutation):
with traced_atomic_transaction():
perform_reordering(values_m2m, operations)
attribute.refresh_from_db(fields=["values"])
+
+ for value in [v for v in values_m2m if v.id in operations.keys()]:
+ info.context.plugins.attribute_value_updated(value)
info.context.plugins.attribute_updated(attribute)
+
return AttributeReorderValues(attribute=attribute)
diff --git a/saleor/graphql/attribute/tests/mutations/test_attribute_reorder_values.py b/saleor/graphql/attribute/tests/mutations/test_attribute_reorder_values.py
index 5e18847be8..f0901828c6 100644
--- a/saleor/graphql/attribute/tests/mutations/test_attribute_reorder_values.py
+++ b/saleor/graphql/attribute/tests/mutations/test_attribute_reorder_values.py
@@ -198,22 +198,47 @@ def test_sort_values_trigger_webhook(
staff_api_client.post_graphql(ATTRIBUTE_VALUES_REORDER_MUTATION, variables)
)["data"]["attributeReorderValues"]
- # then
- assert not content["errors"]
- assert content["attribute"]["id"] == attribute_id
- mocked_webhook_trigger.assert_called_once_with(
+ meta = generate_meta(
+ requestor_data=generate_requestor(
+ SimpleLazyObject(lambda: staff_api_client.user)
+ )
+ )
+
+ attribute_updated_call = mock.call(
{
"id": graphene.Node.to_global_id("Attribute", color_attribute.id),
"name": color_attribute.name,
"slug": color_attribute.slug,
- "meta": generate_meta(
- requestor_data=generate_requestor(
- SimpleLazyObject(lambda: staff_api_client.user)
- )
- ),
+ "meta": meta,
},
WebhookEventAsyncType.ATTRIBUTE_UPDATED,
[any_webhook],
color_attribute,
SimpleLazyObject(lambda: staff_api_client.user),
)
+
+ def generate_attribute_value_update_call(value):
+ return mock.call(
+ {
+ "id": graphene.Node.to_global_id("AttributeValue", value.id),
+ "name": value.name,
+ "slug": value.slug,
+ "value": value.value,
+ "meta": meta,
+ },
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_UPDATED,
+ [any_webhook],
+ value,
+ SimpleLazyObject(lambda: staff_api_client.user),
+ )
+
+ attribute_value_1_updated_call = generate_attribute_value_update_call(values[0])
+ attribute_value_2_updated_call = generate_attribute_value_update_call(values[2])
+
+ # then
+ assert not content["errors"]
+ assert content["attribute"]["id"] == attribute_id
+ assert len(mocked_webhook_trigger.call_args_list) == 3
+ assert attribute_updated_call in mocked_webhook_trigger.call_args_list
+ assert attribute_value_1_updated_call in mocked_webhook_trigger.call_args_list
+ assert attribute_value_2_updated_call in mocked_webhook_trigger.call_args_list
diff --git a/saleor/graphql/attribute/tests/mutations/test_attribute_value_create.py b/saleor/graphql/attribute/tests/mutations/test_attribute_value_create.py
index f8e0d0848b..4c5a2d10da 100644
--- a/saleor/graphql/attribute/tests/mutations/test_attribute_value_create.py
+++ b/saleor/graphql/attribute/tests/mutations/test_attribute_value_create.py
@@ -124,20 +124,19 @@ def test_create_attribute_value_trigger_webhooks(
)
content = get_graphql_content(response)
data = content["data"]["attributeValueCreate"]
+ attribute_value = AttributeValue.objects.get(attribute=color_attribute, name=name)
+ meta = generate_meta(
+ requestor_data=generate_requestor(
+ SimpleLazyObject(lambda: staff_api_client.user)
+ )
+ )
- # then
- assert not data["errors"]
- assert data["attributeValue"]
- mocked_webhook_trigger.assert_called_once_with(
+ attribute_updated_call = mock.call(
{
"id": graphene.Node.to_global_id("Attribute", color_attribute.id),
"name": color_attribute.name,
"slug": color_attribute.slug,
- "meta": generate_meta(
- requestor_data=generate_requestor(
- SimpleLazyObject(lambda: staff_api_client.user)
- )
- ),
+ "meta": meta,
},
WebhookEventAsyncType.ATTRIBUTE_UPDATED,
[any_webhook],
@@ -145,6 +144,27 @@ def test_create_attribute_value_trigger_webhooks(
SimpleLazyObject(lambda: staff_api_client.user),
)
+ attribute_value_created_call = mock.call(
+ {
+ "id": graphene.Node.to_global_id("AttributeValue", attribute_value.id),
+ "name": attribute_value.name,
+ "slug": attribute_value.slug,
+ "value": attribute_value.value,
+ "meta": meta,
+ },
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_CREATED,
+ [any_webhook],
+ attribute_value,
+ SimpleLazyObject(lambda: staff_api_client.user),
+ )
+
+ # then
+ assert not data["errors"]
+ assert data["attributeValue"]
+ assert len(mocked_webhook_trigger.call_args_list) == 2
+ assert attribute_updated_call in mocked_webhook_trigger.call_args_list
+ assert attribute_value_created_call in mocked_webhook_trigger.call_args_list
+
def test_create_swatch_attribute_value_with_value(
staff_api_client, swatch_attribute, permission_manage_products
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 7f365d60c1..e842526ba5 100644
--- a/saleor/graphql/attribute/tests/mutations/test_attribute_value_delete.py
+++ b/saleor/graphql/attribute/tests/mutations/test_attribute_value_delete.py
@@ -71,17 +71,18 @@ def test_delete_attribute_value_trigger_webhooks(
permissions=[permission_manage_product_types_and_attributes],
)
- # then
- mocked_webhook_trigger.assert_called_once_with(
+ meta = generate_meta(
+ requestor_data=generate_requestor(
+ SimpleLazyObject(lambda: staff_api_client.user)
+ )
+ )
+
+ attribute_updated_call = mock.call(
{
"id": graphene.Node.to_global_id("Attribute", color_attribute.id),
"name": color_attribute.name,
"slug": color_attribute.slug,
- "meta": generate_meta(
- requestor_data=generate_requestor(
- SimpleLazyObject(lambda: staff_api_client.user)
- )
- ),
+ "meta": meta,
},
WebhookEventAsyncType.ATTRIBUTE_UPDATED,
[any_webhook],
@@ -89,6 +90,25 @@ def test_delete_attribute_value_trigger_webhooks(
SimpleLazyObject(lambda: staff_api_client.user),
)
+ attribute_value_created_call = mock.call(
+ {
+ "id": graphene.Node.to_global_id("AttributeValue", value.id),
+ "name": value.name,
+ "slug": value.slug,
+ "value": value.value,
+ "meta": meta,
+ },
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_DELETED,
+ [any_webhook],
+ value,
+ SimpleLazyObject(lambda: staff_api_client.user),
+ )
+
+ # then
+ assert len(mocked_webhook_trigger.call_args_list) == 2
+ assert attribute_updated_call in mocked_webhook_trigger.call_args_list
+ assert attribute_value_created_call in mocked_webhook_trigger.call_args_list
+
def test_delete_file_attribute_value(
staff_api_client,
diff --git a/saleor/graphql/attribute/tests/mutations/test_attribute_value_update.py b/saleor/graphql/attribute/tests/mutations/test_attribute_value_update.py
index bbcecfcb53..70eb16bfe7 100644
--- a/saleor/graphql/attribute/tests/mutations/test_attribute_value_update.py
+++ b/saleor/graphql/attribute/tests/mutations/test_attribute_value_update.py
@@ -104,20 +104,18 @@ def test_update_attribute_value_trigger_webhooks(
data = content["data"]["attributeValueUpdate"]
value.refresh_from_db()
attribute = value.attribute
+ meta = generate_meta(
+ requestor_data=generate_requestor(
+ SimpleLazyObject(lambda: staff_api_client.user)
+ )
+ )
- # then
- assert not data["errors"]
- assert data["attributeValue"]["name"] == name == value.name
- mocked_webhook_trigger.assert_called_once_with(
+ attribute_updated_call = mock.call(
{
"id": graphene.Node.to_global_id("Attribute", attribute.id),
"name": attribute.name,
"slug": attribute.slug,
- "meta": generate_meta(
- requestor_data=generate_requestor(
- SimpleLazyObject(lambda: staff_api_client.user)
- )
- ),
+ "meta": meta,
},
WebhookEventAsyncType.ATTRIBUTE_UPDATED,
[any_webhook],
@@ -125,6 +123,27 @@ def test_update_attribute_value_trigger_webhooks(
SimpleLazyObject(lambda: staff_api_client.user),
)
+ attribute_value_created_call = mock.call(
+ {
+ "id": graphene.Node.to_global_id("AttributeValue", value.id),
+ "name": value.name,
+ "slug": value.slug,
+ "value": value.value,
+ "meta": meta,
+ },
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_UPDATED,
+ [any_webhook],
+ value,
+ SimpleLazyObject(lambda: staff_api_client.user),
+ )
+
+ # then
+ assert not data["errors"]
+ assert data["attributeValue"]["name"] == name == value.name
+ assert len(mocked_webhook_trigger.call_args_list) == 2
+ assert attribute_updated_call in mocked_webhook_trigger.call_args_list
+ assert attribute_value_created_call in mocked_webhook_trigger.call_args_list
+
def test_update_attribute_value_name_not_unique(
staff_api_client,
diff --git a/saleor/graphql/attribute/tests/mutations/test_bulk_delete.py b/saleor/graphql/attribute/tests/mutations/test_bulk_delete.py
index 7349d8de5a..6440e25168 100644
--- a/saleor/graphql/attribute/tests/mutations/test_bulk_delete.py
+++ b/saleor/graphql/attribute/tests/mutations/test_bulk_delete.py
@@ -202,10 +202,11 @@ def test_delete_attribute_values_trigger_webhook(
permissions=[permission_manage_page_types_and_attributes],
)
content = get_graphql_content(response)
+ expected_call_count = len(attributes) + len(attribute_value_list)
# then
assert content["data"]["attributeValueBulkDelete"]["count"] == 3
- assert mocked_webhook_trigger.call_count == len(attributes)
+ assert mocked_webhook_trigger.call_count == expected_call_count
def test_delete_attribute_values_search_document_updated(
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index fef393f147..93478739d8 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1361,6 +1361,15 @@ enum WebhookEventTypeEnum {
"""An attribute is deleted."""
ATTRIBUTE_DELETED
+ """A new attribute value is created."""
+ ATTRIBUTE_VALUE_CREATED
+
+ """An attribute value is updated."""
+ ATTRIBUTE_VALUE_UPDATED
+
+ """An attribute value is deleted."""
+ ATTRIBUTE_VALUE_DELETED
+
"""A new category created."""
CATEGORY_CREATED
@@ -1644,6 +1653,15 @@ enum WebhookEventTypeAsyncEnum {
"""An attribute is deleted."""
ATTRIBUTE_DELETED
+ """A new attribute value is created."""
+ ATTRIBUTE_VALUE_CREATED
+
+ """An attribute value is updated."""
+ ATTRIBUTE_VALUE_UPDATED
+
+ """An attribute value is deleted."""
+ ATTRIBUTE_VALUE_DELETED
+
"""A new category created."""
CATEGORY_CREATED
@@ -2323,6 +2341,9 @@ enum WebhookSampleEventTypeEnum {
ATTRIBUTE_CREATED
ATTRIBUTE_UPDATED
ATTRIBUTE_DELETED
+ ATTRIBUTE_VALUE_CREATED
+ ATTRIBUTE_VALUE_UPDATED
+ ATTRIBUTE_VALUE_DELETED
CATEGORY_CREATED
CATEGORY_UPDATED
CATEGORY_DELETED
@@ -21081,6 +21102,75 @@ type AttributeDeleted implements Event {
attribute: Attribute
}
+type AttributeValueCreated implements Event {
+ """Time of the event."""
+ issuedAt: DateTime
+
+ """Saleor version that triggered the event."""
+ version: String
+
+ """The user or application that triggered the event."""
+ issuingPrincipal: IssuingPrincipal
+
+ """The application receiving the webhook."""
+ recipient: App
+
+ """
+ The attribute value the event relates to.
+
+ Added in Saleor 3.5.
+
+ Note: this API is currently in Feature Preview and can be subject to changes at later point.
+ """
+ attributeValue: AttributeValue
+}
+
+type AttributeValueUpdated implements Event {
+ """Time of the event."""
+ issuedAt: DateTime
+
+ """Saleor version that triggered the event."""
+ version: String
+
+ """The user or application that triggered the event."""
+ issuingPrincipal: IssuingPrincipal
+
+ """The application receiving the webhook."""
+ recipient: App
+
+ """
+ The attribute value the event relates to.
+
+ Added in Saleor 3.5.
+
+ Note: this API is currently in Feature Preview and can be subject to changes at later point.
+ """
+ attributeValue: AttributeValue
+}
+
+type AttributeValueDeleted implements Event {
+ """Time of the event."""
+ issuedAt: DateTime
+
+ """Saleor version that triggered the event."""
+ version: String
+
+ """The user or application that triggered the event."""
+ issuingPrincipal: IssuingPrincipal
+
+ """The application receiving the webhook."""
+ recipient: App
+
+ """
+ The attribute value the event relates to.
+
+ Added in Saleor 3.5.
+
+ Note: this API is currently in Feature Preview and can be subject to changes at later point.
+ """
+ attributeValue: AttributeValue
+}
+
type CategoryCreated implements Event {
"""Time of the event."""
issuedAt: DateTime
diff --git a/saleor/graphql/tests/queries/fragments.py b/saleor/graphql/tests/queries/fragments.py
index 19a617d344..e4ea41356b 100644
--- a/saleor/graphql/tests/queries/fragments.py
+++ b/saleor/graphql/tests/queries/fragments.py
@@ -393,3 +393,11 @@ fragment AttributeDetails on Attribute {
inputType
}
"""
+
+ATTRIBUTE_VALUE_DETAILS = """
+fragment AttributeValueDetails on AttributeValue {
+ name
+ slug
+ value
+}
+"""
diff --git a/saleor/graphql/webhook/enums.py b/saleor/graphql/webhook/enums.py
index 2528d9039d..920c6d7c58 100644
--- a/saleor/graphql/webhook/enums.py
+++ b/saleor/graphql/webhook/enums.py
@@ -33,6 +33,9 @@ WEBHOOK_EVENT_DESCRIPTION = {
WebhookEventAsyncType.ATTRIBUTE_CREATED: "A new attribute is created.",
WebhookEventAsyncType.ATTRIBUTE_UPDATED: "An attribute is updated.",
WebhookEventAsyncType.ATTRIBUTE_DELETED: "An attribute is deleted.",
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_CREATED: "A new attribute value is created.",
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_UPDATED: "An attribute value is updated.",
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_DELETED: "An attribute value is deleted.",
WebhookEventAsyncType.CATEGORY_CREATED: "A new category created.",
WebhookEventAsyncType.CATEGORY_UPDATED: "A category is updated.",
WebhookEventAsyncType.CATEGORY_DELETED: "A category is deleted.",
diff --git a/saleor/graphql/webhook/subscription_types.py b/saleor/graphql/webhook/subscription_types.py
index 9badb26b77..edc7f042bb 100644
--- a/saleor/graphql/webhook/subscription_types.py
+++ b/saleor/graphql/webhook/subscription_types.py
@@ -80,6 +80,9 @@ class Event(graphene.Interface):
WebhookEventAsyncType.ATTRIBUTE_CREATED: AttributeCreated,
WebhookEventAsyncType.ATTRIBUTE_UPDATED: AttributeUpdated,
WebhookEventAsyncType.ATTRIBUTE_DELETED: AttributeDeleted,
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_CREATED: AttributeValueCreated,
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_UPDATED: AttributeValueUpdated,
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_DELETED: AttributeValueDeleted,
WebhookEventAsyncType.CATEGORY_CREATED: CategoryCreated,
WebhookEventAsyncType.CATEGORY_UPDATED: CategoryUpdated,
WebhookEventAsyncType.CATEGORY_DELETED: CategoryDeleted,
@@ -274,6 +277,35 @@ class AttributeDeleted(ObjectType, AttributeBase):
interfaces = (Event,)
+class AttributeValueBase(AbstractType):
+ attribute_value = graphene.Field(
+ "saleor.graphql.attribute.types.AttributeValue",
+ description="The attribute value the event relates to."
+ + ADDED_IN_35
+ + PREVIEW_FEATURE,
+ )
+
+ @staticmethod
+ def resolve_attribute_value(root, _info):
+ _, attribute = root
+ return attribute
+
+
+class AttributeValueCreated(ObjectType, AttributeValueBase):
+ class Meta:
+ interfaces = (Event,)
+
+
+class AttributeValueUpdated(ObjectType, AttributeValueBase):
+ class Meta:
+ interfaces = (Event,)
+
+
+class AttributeValueDeleted(ObjectType, AttributeValueBase):
+ class Meta:
+ interfaces = (Event,)
+
+
class CategoryBase(AbstractType):
category = graphene.Field(
"saleor.graphql.product.types.Category",
@@ -1085,6 +1117,9 @@ SUBSCRIPTION_EVENTS_TYPES = [
AttributeCreated,
AttributeUpdated,
AttributeDeleted,
+ AttributeValueCreated,
+ AttributeValueUpdated,
+ AttributeValueDeleted,
CategoryCreated,
CategoryUpdated,
CategoryDeleted,
diff --git a/saleor/plugins/base_plugin.py b/saleor/plugins/base_plugin.py
index 8b459aae72..005a2e86af 100644
--- a/saleor/plugins/base_plugin.py
+++ b/saleor/plugins/base_plugin.py
@@ -40,7 +40,7 @@ if TYPE_CHECKING:
# flake8: noqa
from ..account.models import Address, User
from ..app.models import App
- from ..attribute.models import Attribute
+ from ..attribute.models import Attribute, AttributeValue
from ..channel.models import Channel
from ..checkout.fetch import CheckoutInfo, CheckoutLineInfo
from ..checkout.models import Checkout
@@ -217,6 +217,24 @@ class BasePlugin:
# updated.
attribute_updated: Callable[["Attribute", None], None]
+ # Trigger when attribute value is created.
+ #
+ # Overwrite this method if you need to trigger specific logic after an attribute
+ # value is installed.
+ attribute_value_created: Callable[["AttributeValue", None], None]
+
+ # Trigger when attribute value is deleted.
+ #
+ # Overwrite this method if you need to trigger specific logic after an attribute
+ # value is deleted.
+ attribute_value_deleted: Callable[["AttributeValue", None], None]
+
+ # Trigger when attribute value is updated.
+ #
+ # Overwrite this method if you need to trigger specific logic after an attribute
+ # value is updated.
+ attribute_value_updated: Callable[["AttributeValue", None], None]
+
# Authenticate user which should be assigned to the request.
#
# Overwrite this method if the plugin handles authentication flow.
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index 94931c2eaa..9736e96289 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -40,7 +40,7 @@ if TYPE_CHECKING:
# flake8: noqa
from ..account.models import Address, User
from ..app.models import App
- from ..attribute.models import Attribute
+ from ..attribute.models import Attribute, AttributeValue
from ..checkout.fetch import CheckoutInfo, CheckoutLineInfo
from ..checkout.models import Checkout
from ..core.middleware import Requestor
@@ -882,6 +882,24 @@ class PluginsManager(PaymentInterface):
"attribute_deleted", default_value, attribute
)
+ def attribute_value_created(self, attribute_value: "AttributeValue"):
+ default_value = None
+ return self.__run_method_on_plugins(
+ "attribute_value_created", default_value, attribute_value
+ )
+
+ def attribute_value_updated(self, attribute_value: "AttributeValue"):
+ default_value = None
+ return self.__run_method_on_plugins(
+ "attribute_value_updated", default_value, attribute_value
+ )
+
+ def attribute_value_deleted(self, attribute_value: "AttributeValue"):
+ default_value = None
+ return self.__run_method_on_plugins(
+ "attribute_value_deleted", default_value, attribute_value
+ )
+
def category_created(self, category: "Category"):
default_value = None
return self.__run_method_on_plugins("category_created", default_value, category)
diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.py
index db0b2ad96b..c6ed1af92e 100644
--- a/saleor/plugins/webhook/plugin.py
+++ b/saleor/plugins/webhook/plugin.py
@@ -51,7 +51,7 @@ from .utils import (
if TYPE_CHECKING:
from ...account.models import Address, User
- from ...attribute.models import Attribute
+ from ...attribute.models import Attribute, AttributeValue
from ...channel.models import Channel
from ...checkout.models import Checkout
from ...discount.models import Sale, Voucher
@@ -187,6 +187,46 @@ class WebhookPlugin(BasePlugin):
WebhookEventAsyncType.ATTRIBUTE_DELETED, attribute
)
+ def _trigger_attribute_value_event(self, event_type, attribute_value):
+ if webhooks := get_webhooks_for_event(event_type):
+ payload = {
+ "id": graphene.Node.to_global_id("AttributeValue", attribute_value.id),
+ "name": attribute_value.name,
+ "slug": attribute_value.slug,
+ "value": attribute_value.value,
+ "meta": self._generate_meta(),
+ }
+ trigger_webhooks_async(
+ payload, event_type, webhooks, attribute_value, self.requestor
+ )
+
+ def attribute_value_created(
+ self, attribute_value: "AttributeValue", previous_value: None
+ ) -> None:
+ if not self.active:
+ return previous_value
+ self._trigger_attribute_value_event(
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_CREATED, attribute_value
+ )
+
+ def attribute_value_updated(
+ self, attribute_value: "AttributeValue", previous_value: None
+ ) -> None:
+ if not self.active:
+ return previous_value
+ self._trigger_attribute_value_event(
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_UPDATED, attribute_value
+ )
+
+ def attribute_value_deleted(
+ self, attribute_value: "AttributeValue", previous_value: None
+ ) -> None:
+ if not self.active:
+ return previous_value
+ self._trigger_attribute_value_event(
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_DELETED, attribute_value
+ )
+
def __trigger_category_event(self, event_type, category):
if webhooks := get_webhooks_for_event(event_type):
payload = {
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py b/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
index 1840a3d9f7..070487cbf7 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
@@ -91,6 +91,30 @@ def subscription_attribute_deleted_webhook(subscription_webhook):
)
+def subscription_attribute_value_created_webhook(subscription_webhook):
+ return subscription_webhook(
+ subscription_queries.ATTRIBUTE_VALUE_CREATED,
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_CREATED,
+ )
+
+
+def subscription_attribute_value_updated_webhook(subscription_webhook):
+ return subscription_webhook(
+ subscription_queries.ATTRIBUTE_VALUE_UPDATED,
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_UPDATED,
+ )
+
+
+def subscription_attribute_value_deleted_webhook(subscription_webhook):
+ return subscription_webhook(
+ subscription_queries.ATTRIBUTE_VALUE_DELETED,
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_DELETED,
+ )
+
+
@pytest.fixture
def subscription_category_created_webhook(subscription_webhook):
return subscription_webhook(
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py b/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py
index 6022157958..0c4fe370e1 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py
@@ -34,6 +34,18 @@ def generate_attribute_payload(attribute):
)
+def generate_attribute_value_payload(attribute_value):
+ return json.dumps(
+ {
+ "attributeValue": {
+ "name": attribute_value.name,
+ "slug": attribute_value.slug,
+ "value": attribute_value.value,
+ }
+ }
+ )
+
+
def generate_taxed_money_payload(taxed_money):
return {
"currency": taxed_money.currency,
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
index 06a23ebd0a..346083d863 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
@@ -159,6 +159,54 @@ ATTRIBUTE_DELETED = (
)
+ATTRIBUTE_VALUE_CREATED = (
+ fragments.ATTRIBUTE_VALUE_DETAILS
+ + """
+ subscription{
+ event{
+ ...on AttributeValueCreated{
+ attributeValue{
+ ...AttributeValueDetails
+ }
+ }
+ }
+ }
+"""
+)
+
+
+ATTRIBUTE_VALUE_UPDATED = (
+ fragments.ATTRIBUTE_VALUE_DETAILS
+ + """
+ subscription{
+ event{
+ ...on AttributeValueUpdated{
+ attributeValue{
+ ...AttributeValueDetails
+ }
+ }
+ }
+ }
+"""
+)
+
+
+ATTRIBUTE_VALUE_DELETED = (
+ fragments.ATTRIBUTE_VALUE_DETAILS
+ + """
+ subscription{
+ event{
+ ...on AttributeValueDeleted{
+ attributeValue{
+ ...AttributeValueDetails
+ }
+ }
+ }
+ }
+"""
+)
+
+
GIFT_CARD_CREATED = (
fragments.GIFT_CARD_DETAILS
+ """
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py b/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py
index 03befa4229..bf27f17e3a 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py
@@ -19,6 +19,7 @@ from .payloads import (
generate_address_payload,
generate_app_payload,
generate_attribute_payload,
+ generate_attribute_value_payload,
generate_category_payload,
generate_collection_payload,
generate_customer_payload,
@@ -255,6 +256,68 @@ def test_attribute_deleted(color_attribute, subscription_attribute_deleted_webho
assert deliveries[0].webhook == webhooks[0]
+def test_attribute_value_created(
+ pink_attribute_value, subscription_attribute_value_created_webhook
+):
+ # given
+ webhooks = [subscription_attribute_value_created_webhook]
+ event_type = WebhookEventAsyncType.ATTRIBUTE_VALUE_CREATED
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(
+ event_type, pink_attribute_value, webhooks
+ )
+
+ # then
+ expected_payload = generate_attribute_value_payload(pink_attribute_value)
+ assert deliveries[0].payload.payload == expected_payload
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+
+
+def test_attribute_value_updated(
+ pink_attribute_value, subscription_attribute_value_updated_webhook
+):
+ # given
+ webhooks = [subscription_attribute_value_updated_webhook]
+ event_type = WebhookEventAsyncType.ATTRIBUTE_VALUE_UPDATED
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(
+ event_type, pink_attribute_value, webhooks
+ )
+
+ # then
+ expected_payload = generate_attribute_value_payload(pink_attribute_value)
+ assert deliveries[0].payload.payload == expected_payload
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+
+
+def test_attribute_value_deleted(
+ pink_attribute_value, subscription_attribute_value_deleted_webhook
+):
+ # given
+ webhooks = [subscription_attribute_value_deleted_webhook]
+
+ id = pink_attribute_value.id
+ pink_attribute_value.delete()
+ pink_attribute_value.id = id
+
+ event_type = WebhookEventAsyncType.ATTRIBUTE_VALUE_DELETED
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(
+ event_type, pink_attribute_value, webhooks
+ )
+
+ # then
+ expected_payload = generate_attribute_value_payload(pink_attribute_value)
+ assert deliveries[0].payload.payload == expected_payload
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+
+
def test_category_created(
categories_tree_with_published_products,
subscription_category_created_webhook,
diff --git a/saleor/webhook/event_types.py b/saleor/webhook/event_types.py
index faffd18f08..d5445229b1 100644
--- a/saleor/webhook/event_types.py
+++ b/saleor/webhook/event_types.py
@@ -32,6 +32,10 @@ class WebhookEventAsyncType:
ATTRIBUTE_UPDATED = "attribute_updated"
ATTRIBUTE_DELETED = "attribute_deleted"
+ ATTRIBUTE_VALUE_CREATED = "attribute_value_created"
+ ATTRIBUTE_VALUE_UPDATED = "attribute_value_updated"
+ ATTRIBUTE_VALUE_DELETED = "attribute_value_deleted"
+
CATEGORY_CREATED = "category_created"
CATEGORY_UPDATED = "category_updated"
CATEGORY_DELETED = "category_deleted"
@@ -145,6 +149,9 @@ class WebhookEventAsyncType:
ATTRIBUTE_CREATED: "Attribute created",
ATTRIBUTE_UPDATED: "Attribute updated",
ATTRIBUTE_DELETED: "Attribute deleted",
+ ATTRIBUTE_VALUE_CREATED: "Attribute value created",
+ ATTRIBUTE_VALUE_UPDATED: "Attribute value updated",
+ ATTRIBUTE_VALUE_DELETED: "Attribute value deleted",
CATEGORY_CREATED: "Category created",
CATEGORY_UPDATED: "Category updated",
CATEGORY_DELETED: "Category deleted",
@@ -234,6 +241,9 @@ class WebhookEventAsyncType:
(ATTRIBUTE_CREATED, DISPLAY_LABELS[ATTRIBUTE_CREATED]),
(ATTRIBUTE_UPDATED, DISPLAY_LABELS[ATTRIBUTE_UPDATED]),
(ATTRIBUTE_DELETED, DISPLAY_LABELS[ATTRIBUTE_DELETED]),
+ (ATTRIBUTE_VALUE_CREATED, DISPLAY_LABELS[ATTRIBUTE_VALUE_CREATED]),
+ (ATTRIBUTE_VALUE_UPDATED, DISPLAY_LABELS[ATTRIBUTE_VALUE_UPDATED]),
+ (ATTRIBUTE_VALUE_DELETED, DISPLAY_LABELS[ATTRIBUTE_VALUE_DELETED]),
(CATEGORY_CREATED, DISPLAY_LABELS[CATEGORY_CREATED]),
(CATEGORY_UPDATED, DISPLAY_LABELS[CATEGORY_UPDATED]),
(CATEGORY_DELETED, DISPLAY_LABELS[CATEGORY_DELETED]),
@@ -324,6 +334,9 @@ class WebhookEventAsyncType:
ATTRIBUTE_CREATED: None,
ATTRIBUTE_UPDATED: None,
ATTRIBUTE_DELETED: None,
+ ATTRIBUTE_VALUE_CREATED: None,
+ ATTRIBUTE_VALUE_UPDATED: None,
+ ATTRIBUTE_VALUE_DELETED: None,
CATEGORY_CREATED: ProductPermissions.MANAGE_PRODUCTS,
CATEGORY_UPDATED: ProductPermissions.MANAGE_PRODUCTS,
CATEGORY_DELETED: ProductPermissions.MANAGE_PRODUCTS,
@@ -484,6 +497,9 @@ SUBSCRIBABLE_EVENTS = [
WebhookEventAsyncType.ATTRIBUTE_CREATED,
WebhookEventAsyncType.ATTRIBUTE_UPDATED,
WebhookEventAsyncType.ATTRIBUTE_DELETED,
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_CREATED,
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_UPDATED,
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_DELETED,
WebhookEventAsyncType.CATEGORY_CREATED,
WebhookEventAsyncType.CATEGORY_UPDATED,
WebhookEventAsyncType.CATEGORY_DELETED,