Skip to content

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

New events related to page types changes (#9859)

SzymJ5a4a4e683e59
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 38b3173d72..dd33fccad1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,7 +4,10 @@ All notable, unreleased changes to this project will be documented in this file.
# 3.5.0 [Unreleased]
-...
+### Other changes
+
+#### Saleor Apps
+- Add webhooks `PAGE_TYPE_CREATED`, `PAGE_TYPE_UPDATED` and `PAGE_TYPE_DELETED` - #9859 by @SzymJ
# 3.4.0
diff --git a/saleor/graphql/core/descriptions.py b/saleor/graphql/core/descriptions.py
index 31b2676acd..2a5259714b 100644
--- a/saleor/graphql/core/descriptions.py
+++ b/saleor/graphql/core/descriptions.py
@@ -15,6 +15,7 @@ ADDED_IN_31 = "\n\nAdded in Saleor 3.1."
ADDED_IN_32 = "\n\nAdded in Saleor 3.2."
ADDED_IN_33 = "\n\nAdded in Saleor 3.3."
ADDED_IN_34 = "\n\nAdded in Saleor 3.4."
+ADDED_IN_35 = "\n\nAdded in Saleor 3.5."
PREVIEW_FEATURE = (
diff --git a/saleor/graphql/page/bulk_mutations.py b/saleor/graphql/page/bulk_mutations.py
index 239f02e355..669a8b2e0e 100644
--- a/saleor/graphql/page/bulk_mutations.py
+++ b/saleor/graphql/page/bulk_mutations.py
@@ -1,5 +1,6 @@
import graphene
from django.core.exceptions import ValidationError
+from django.db import transaction
from ...attribute import AttributeInputType
from ...attribute import models as attribute_models
@@ -91,6 +92,13 @@ class PageTypeBulkDelete(ModelBulkDeleteMutation):
cls.delete_assigned_attribute_values(pks)
return super().perform_mutation(_root, info, ids, **data)
+ @classmethod
+ def bulk_action(cls, info, queryset):
+ page_types = list(queryset)
+ queryset.delete()
+ for pt in page_types:
+ transaction.on_commit(lambda: info.context.plugins.page_type_deleted(pt))
+
@staticmethod
def delete_assigned_attribute_values(instance_pks):
attribute_models.AttributeValue.objects.filter(
diff --git a/saleor/graphql/page/mutations/pages.py b/saleor/graphql/page/mutations/pages.py
index 4c9be70a14..5619fa75c8 100644
--- a/saleor/graphql/page/mutations/pages.py
+++ b/saleor/graphql/page/mutations/pages.py
@@ -280,6 +280,10 @@ class PageTypeCreate(PageTypeMixin, ModelMutation):
if attributes is not None:
instance.page_attributes.add(*attributes)
+ @classmethod
+ def post_save_action(cls, info, instance, cleaned_input):
+ info.context.plugins.page_type_created(instance)
+
class PageTypeUpdate(PageTypeMixin, ModelMutation):
class Arguments:
@@ -336,6 +340,10 @@ class PageTypeUpdate(PageTypeMixin, ModelMutation):
if add_attributes is not None:
instance.page_attributes.add(*add_attributes)
+ @classmethod
+ def post_save_action(cls, info, instance, cleaned_input):
+ info.context.plugins.page_type_updated(instance)
+
class PageTypeDelete(ModelDeleteMutation):
class Arguments:
@@ -365,3 +373,7 @@ class PageTypeDelete(ModelDeleteMutation):
attribute__input_type__in=AttributeInputType.TYPES_WITH_UNIQUE_VALUES,
pageassignments__assignment__page_type_id=instance_pk,
).delete()
+
+ @classmethod
+ def post_save_action(cls, info, instance, cleaned_input):
+ transaction.on_commit(lambda: info.context.plugins.page_type_deleted(instance))
diff --git a/saleor/graphql/page/tests/mutations/test_page_type_create.py b/saleor/graphql/page/tests/mutations/test_page_type_create.py
index 578a31892b..e3ff69c187 100644
--- a/saleor/graphql/page/tests/mutations/test_page_type_create.py
+++ b/saleor/graphql/page/tests/mutations/test_page_type_create.py
@@ -1,7 +1,13 @@
+from unittest import mock
+
import graphene
+from django.utils.functional import SimpleLazyObject
+from freezegun import freeze_time
from .....page.error_codes import PageErrorCode
from .....page.models import PageType
+from .....webhook.event_types import WebhookEventAsyncType
+from .....webhook.payloads import generate_meta, generate_requestor
from ....tests.utils import assert_no_permission, get_graphql_content
PAGE_TYPE_CREATE_MUTATION = """
@@ -69,6 +75,67 @@ def test_page_type_create_as_staff(
}
+@freeze_time("2022-05-12 12:00:00")
[email protected]("saleor.plugins.webhook.plugin.get_webhooks_for_event")
[email protected]("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_page_type_create_trigger_webhook(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ staff_api_client,
+ tag_page_attribute,
+ author_page_attribute,
+ permission_manage_page_types_and_attributes,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ staff_user = staff_api_client.user
+ staff_user.user_permissions.add(permission_manage_page_types_and_attributes)
+
+ name = "Test page type"
+ slug = "test-page-type"
+
+ attributes = [author_page_attribute, tag_page_attribute]
+
+ variables = {
+ "name": name,
+ "slug": slug,
+ "addAttributes": [
+ graphene.Node.to_global_id("Attribute", attr.pk) for attr in attributes
+ ],
+ }
+
+ # when
+ response = staff_api_client.post_graphql(PAGE_TYPE_CREATE_MUTATION, variables)
+ page_type = PageType.objects.last()
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["pageTypeCreate"]
+
+ assert not data["errors"]
+ assert data["pageType"]
+ mocked_webhook_trigger.assert_called_once_with(
+ {
+ "id": graphene.Node.to_global_id("PageType", page_type.id),
+ "name": page_type.name,
+ "slug": page_type.slug,
+ "meta": generate_meta(
+ requestor_data=generate_requestor(
+ SimpleLazyObject(lambda: staff_api_client.user)
+ )
+ ),
+ },
+ WebhookEventAsyncType.PAGE_TYPE_CREATED,
+ [any_webhook],
+ page_type,
+ SimpleLazyObject(lambda: staff_api_client.user),
+ )
+
+
def test_page_type_create_as_staff_no_perm(
staff_api_client, tag_page_attribute, author_page_attribute
):
diff --git a/saleor/graphql/page/tests/mutations/test_page_type_delete.py b/saleor/graphql/page/tests/mutations/test_page_type_delete.py
index 0aa64cf6c6..1d57de0bfa 100644
--- a/saleor/graphql/page/tests/mutations/test_page_type_delete.py
+++ b/saleor/graphql/page/tests/mutations/test_page_type_delete.py
@@ -1,8 +1,14 @@
+from unittest import mock
+
import graphene
import pytest
+from django.utils.functional import SimpleLazyObject
+from freezegun import freeze_time
from .....attribute.utils import associate_attribute_values_to_instance
from .....page.models import Page
+from .....webhook.event_types import WebhookEventAsyncType
+from .....webhook.payloads import generate_meta, generate_requestor
from ....tests.utils import assert_no_permission, get_graphql_content
DELETE_PAGE_TYPE_MUTATION = """
@@ -56,6 +62,57 @@ def test_page_type_delete_by_staff(
assert not Page.objects.filter(pk__in=pages_pks)
+@freeze_time("2022-05-12 12:00:00")
[email protected]("saleor.plugins.webhook.plugin.get_webhooks_for_event")
[email protected]("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_page_type_delete_trigger_webhook(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ staff_api_client,
+ page_type,
+ page,
+ permission_manage_page_types_and_attributes,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ staff_api_client.user.user_permissions.add(
+ permission_manage_page_types_and_attributes
+ )
+ page_type_id = graphene.Node.to_global_id("PageType", page_type.pk)
+
+ # when
+ response = staff_api_client.post_graphql(
+ DELETE_PAGE_TYPE_MUTATION, {"id": page_type_id}
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["pageTypeDelete"]
+
+ assert not data["errors"]
+ assert data["pageType"]["id"] == page_type_id
+ mocked_webhook_trigger.assert_called_once_with(
+ {
+ "id": page_type_id,
+ "name": page_type.name,
+ "slug": page_type.slug,
+ "meta": generate_meta(
+ requestor_data=generate_requestor(
+ SimpleLazyObject(lambda: staff_api_client.user)
+ )
+ ),
+ },
+ WebhookEventAsyncType.PAGE_TYPE_DELETED,
+ [any_webhook],
+ page_type,
+ SimpleLazyObject(lambda: staff_api_client.user),
+ )
+
+
def test_page_type_delete_by_staff_no_perm(
staff_api_client, page_type, page, permission_manage_page_types_and_attributes
):
diff --git a/saleor/graphql/page/tests/mutations/test_page_type_update.py b/saleor/graphql/page/tests/mutations/test_page_type_update.py
index 3effc136c1..bde97def78 100644
--- a/saleor/graphql/page/tests/mutations/test_page_type_update.py
+++ b/saleor/graphql/page/tests/mutations/test_page_type_update.py
@@ -1,9 +1,13 @@
-from unittest.mock import ANY
+from unittest import mock
import graphene
+from django.utils.functional import SimpleLazyObject
+from freezegun import freeze_time
from .....page.error_codes import PageErrorCode
from .....page.models import PageType
+from .....webhook.event_types import WebhookEventAsyncType
+from .....webhook.payloads import generate_meta, generate_requestor
from ....tests.utils import assert_no_permission, get_graphql_content
PAGE_TYPE_UPDATE_MUTATION = """
@@ -78,6 +82,62 @@ def test_page_type_update_as_staff(
}
+@freeze_time("2022-05-12 12:00:00")
[email protected]("saleor.plugins.webhook.plugin.get_webhooks_for_event")
[email protected]("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_page_type_update_trigger_webhook(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ staff_api_client,
+ page_type,
+ permission_manage_page_types_and_attributes,
+ author_page_attribute,
+ size_page_attribute,
+ tag_page_attribute,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ staff_user = staff_api_client.user
+ staff_user.user_permissions.add(permission_manage_page_types_and_attributes)
+
+ slug = "new-slug"
+
+ variables = {
+ "id": graphene.Node.to_global_id("PageType", page_type.pk),
+ "input": {
+ "slug": slug,
+ },
+ }
+
+ # when
+ response = staff_api_client.post_graphql(PAGE_TYPE_UPDATE_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["pageTypeUpdate"]
+ page_type.refresh_from_db()
+
+ # then
+ assert not data["errors"]
+ assert data["pageType"]["slug"] == slug
+ mocked_webhook_trigger.assert_called_once_with(
+ {
+ "id": graphene.Node.to_global_id("PageType", page_type.id),
+ "name": page_type.name,
+ "slug": page_type.slug,
+ "meta": generate_meta(
+ requestor_data=generate_requestor(SimpleLazyObject(lambda: staff_user))
+ ),
+ },
+ WebhookEventAsyncType.PAGE_TYPE_UPDATED,
+ [any_webhook],
+ page_type,
+ SimpleLazyObject(lambda: staff_user),
+ )
+
+
def test_page_type_update_as_staff_no_perm(staff_api_client, page_type):
# given
variables = {
@@ -237,13 +297,13 @@ def test_page_type_update_not_valid_attributes(
{
"code": PageErrorCode.INVALID.name,
"field": "addAttributes",
- "message": ANY,
+ "message": mock.ANY,
"attributes": [graphene.Node.to_global_id("Attribute", color_attribute.pk)],
},
{
"code": PageErrorCode.INVALID.name,
"field": "removeAttributes",
- "message": ANY,
+ "message": mock.ANY,
"attributes": [
graphene.Node.to_global_id("Attribute", attr.pk)
for attr in [weight_attribute, size_attribute]
diff --git a/saleor/graphql/page/tests/mutations/test_page_types_bulk_delete.py b/saleor/graphql/page/tests/mutations/test_page_types_bulk_delete.py
index 7bfbe3134e..df38e31492 100644
--- a/saleor/graphql/page/tests/mutations/test_page_types_bulk_delete.py
+++ b/saleor/graphql/page/tests/mutations/test_page_types_bulk_delete.py
@@ -1,3 +1,5 @@
+from unittest import mock
+
import graphene
import pytest
@@ -53,6 +55,45 @@ def test_page_type_bulk_delete_by_staff(
assert not Page.objects.filter(pk__in=pages_pks)
[email protected]("saleor.plugins.webhook.plugin.get_webhooks_for_event")
[email protected]("saleor.plugins.webhook.plugin.trigger_webhooks_async")
+def test_page_type_bulk_delete_trigger_webhooks(
+ mocked_webhook_trigger,
+ mocked_get_webhooks_for_event,
+ any_webhook,
+ staff_api_client,
+ page_type_list,
+ permission_manage_page_types_and_attributes,
+ settings,
+):
+ # given
+ mocked_get_webhooks_for_event.return_value = [any_webhook]
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+
+ staff_api_client.user.user_permissions.add(
+ permission_manage_page_types_and_attributes
+ )
+
+ page_type_count = len(page_type_list)
+ variables = {
+ "ids": [
+ graphene.Node.to_global_id("PageType", page_type.pk)
+ for page_type in page_type_list
+ ]
+ }
+
+ # when
+ response = staff_api_client.post_graphql(PAGE_TYPE_BULK_DELETE_MUTATION, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["pageTypeBulkDelete"]
+
+ assert not data["errors"]
+ assert data["count"] == page_type_count
+ mocked_webhook_trigger.call_count == page_type_count
+
+
def test_page_type_bulk_delete_by_staff_no_perm(
staff_api_client, page_type_list, permission_manage_page_types_and_attributes
):
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 38f3a214d6..bf1d429232 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -1488,6 +1488,15 @@ enum WebhookEventTypeEnum {
"""A page is deleted."""
PAGE_DELETED
+ """A new page type is created."""
+ PAGE_TYPE_CREATED
+
+ """A page type is updated."""
+ PAGE_TYPE_UPDATED
+
+ """A page type is deleted."""
+ PAGE_TYPE_DELETED
+
"""A new shipping price is created."""
SHIPPING_PRICE_CREATED
@@ -1739,6 +1748,15 @@ enum WebhookEventTypeAsyncEnum {
"""A page is deleted."""
PAGE_DELETED
+ """A new page type is created."""
+ PAGE_TYPE_CREATED
+
+ """A page type is updated."""
+ PAGE_TYPE_UPDATED
+
+ """A page type is deleted."""
+ PAGE_TYPE_DELETED
+
"""A new shipping price is created."""
SHIPPING_PRICE_CREATED
@@ -2302,6 +2320,9 @@ enum WebhookSampleEventTypeEnum {
PAGE_CREATED
PAGE_UPDATED
PAGE_DELETED
+ PAGE_TYPE_CREATED
+ PAGE_TYPE_UPDATED
+ PAGE_TYPE_DELETED
SHIPPING_PRICE_CREATED
SHIPPING_PRICE_UPDATED
SHIPPING_PRICE_DELETED
@@ -22052,6 +22073,75 @@ type PageDeleted implements Event {
page: Page
}
+type PageTypeCreated 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 page type 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.
+ """
+ pageType: PageType
+}
+
+type PageTypeUpdated 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 page type 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.
+ """
+ pageType: PageType
+}
+
+type PageTypeDeleted 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 page type 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.
+ """
+ pageType: PageType
+}
+
type ShippingPriceCreated 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 45ccdee570..daa60209d4 100644
--- a/saleor/graphql/tests/queries/fragments.py
+++ b/saleor/graphql/tests/queries/fragments.py
@@ -298,6 +298,19 @@ fragment PageDetails on Page{
"""
+
+PAGE_TYPE_DETAILS = """
+fragment PageTypeDetails on PageType{
+ id
+ name
+ slug
+ attributes {
+ slug
+ }
+}
+"""
+
+
SALE_DETAILS = """
fragment SaleDetails on Sale {
id
diff --git a/saleor/graphql/webhook/enums.py b/saleor/graphql/webhook/enums.py
index 58d9453c5b..82256badbb 100644
--- a/saleor/graphql/webhook/enums.py
+++ b/saleor/graphql/webhook/enums.py
@@ -66,6 +66,9 @@ WEBHOOK_EVENT_DESCRIPTION = {
WebhookEventAsyncType.PAGE_CREATED: "A new page is created.",
WebhookEventAsyncType.PAGE_UPDATED: "A page is updated.",
WebhookEventAsyncType.PAGE_DELETED: "A page is deleted.",
+ WebhookEventAsyncType.PAGE_TYPE_CREATED: "A new page type is created.",
+ WebhookEventAsyncType.PAGE_TYPE_UPDATED: "A page type is updated.",
+ WebhookEventAsyncType.PAGE_TYPE_DELETED: "A page type is deleted.",
WebhookEventAsyncType.PRODUCT_CREATED: "A new product is created.",
WebhookEventAsyncType.PRODUCT_UPDATED: "A product is updated.",
WebhookEventAsyncType.PRODUCT_DELETED: "A product is deleted.",
diff --git a/saleor/graphql/webhook/subscription_types.py b/saleor/graphql/webhook/subscription_types.py
index 575be403b5..fa851c60e1 100644
--- a/saleor/graphql/webhook/subscription_types.py
+++ b/saleor/graphql/webhook/subscription_types.py
@@ -23,7 +23,7 @@ from ...webhook.event_types import WebhookEventAsyncType
from ..account.types import User as UserType
from ..app.types import App as AppType
from ..channel import ChannelContext
-from ..core.descriptions import ADDED_IN_32, ADDED_IN_34, PREVIEW_FEATURE
+from ..core.descriptions import ADDED_IN_32, ADDED_IN_34, ADDED_IN_35, PREVIEW_FEATURE
from ..core.scalars import PositiveDecimal
from ..payment.enums import TransactionActionEnum
from ..payment.types import TransactionItem
@@ -130,6 +130,9 @@ class Event(graphene.Interface):
WebhookEventAsyncType.PAGE_CREATED: PageCreated,
WebhookEventAsyncType.PAGE_UPDATED: PageUpdated,
WebhookEventAsyncType.PAGE_DELETED: PageDeleted,
+ WebhookEventAsyncType.PAGE_TYPE_CREATED: PageTypeCreated,
+ WebhookEventAsyncType.PAGE_TYPE_UPDATED: PageTypeUpdated,
+ WebhookEventAsyncType.PAGE_TYPE_DELETED: PageTypeDeleted,
WebhookEventAsyncType.SHIPPING_PRICE_CREATED: ShippingPriceCreated,
WebhookEventAsyncType.SHIPPING_PRICE_UPDATED: ShippingPriceUpdated,
WebhookEventAsyncType.SHIPPING_PRICE_DELETED: ShippingPriceDeleted,
@@ -730,6 +733,35 @@ class PageDeleted(ObjectType, PageBase):
interfaces = (Event,)
+class PageTypeBase(AbstractType):
+ page_type = graphene.Field(
+ "saleor.graphql.page.types.PageType",
+ description="The page type the event relates to."
+ + ADDED_IN_35
+ + PREVIEW_FEATURE,
+ )
+
+ @staticmethod
+ def resolve_page_type(root, _info):
+ _, page_type = root
+ return page_type
+
+
+class PageTypeCreated(ObjectType, PageTypeBase):
+ class Meta:
+ interfaces = (Event,)
+
+
+class PageTypeUpdated(ObjectType, PageTypeBase):
+ class Meta:
+ interfaces = (Event,)
+
+
+class PageTypeDeleted(ObjectType, PageTypeBase):
+ class Meta:
+ interfaces = (Event,)
+
+
class ShippingPriceBase(AbstractType):
shipping_method = graphene.Field(
"saleor.graphql.shipping.types.ShippingMethodType",
@@ -1019,6 +1051,9 @@ SUBSCRIPTION_EVENTS_TYPES = [
PageCreated,
PageUpdated,
PageDeleted,
+ PageTypeCreated,
+ PageTypeUpdated,
+ PageTypeDeleted,
ShippingPriceCreated,
ShippingPriceUpdated,
ShippingPriceDeleted,
diff --git a/saleor/plugins/base_plugin.py b/saleor/plugins/base_plugin.py
index 64e2a416f7..65dbe70cb0 100644
--- a/saleor/plugins/base_plugin.py
+++ b/saleor/plugins/base_plugin.py
@@ -42,7 +42,7 @@ if TYPE_CHECKING:
from ..invoice.models import Invoice
from ..menu.models import Menu, MenuItem
from ..order.models import Fulfillment, Order, OrderLine
- from ..page.models import Page
+ from ..page.models import Page, PageType
from ..product.models import (
Category,
Collection,
@@ -602,6 +602,24 @@ class BasePlugin:
# updated.
page_updated: Callable[["Page", Any], Any]
+ # Trigger when page type is created.
+ #
+ # Overwrite this method if you need to trigger specific logic when a page type is
+ # created.
+ page_type_created: Callable[["PageType", Any], Any]
+
+ # Trigger when page type is deleted.
+ #
+ # Overwrite this method if you need to trigger specific logic when a page type is
+ # deleted.
+ page_type_deleted: Callable[["PageType", Any], Any]
+
+ # Trigger when page type is updated.
+ #
+ # Overwrite this method if you need to trigger specific logic when a page type is
+ # updated.
+ page_type_updated: Callable[["PageType", Any], Any]
+
# Trigger directly before order creation.
#
# Overwrite this method if you need to trigger specific logic before an order is
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index ae6f43acc8..44130c7af6 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -48,7 +48,7 @@ if TYPE_CHECKING:
from ..invoice.models import Invoice
from ..menu.models import Menu, MenuItem
from ..order.models import Fulfillment, Order, OrderLine
- from ..page.models import Page
+ from ..page.models import Page, PageType
from ..payment.interface import (
CustomerSource,
GatewayResponse,
@@ -797,6 +797,24 @@ class PluginsManager(PaymentInterface):
default_value = None
return self.__run_method_on_plugins("page_deleted", default_value, page)
+ def page_type_created(self, page_type: "PageType"):
+ default_value = None
+ return self.__run_method_on_plugins(
+ "page_type_created", default_value, page_type
+ )
+
+ def page_type_updated(self, page_type: "PageType"):
+ default_value = None
+ return self.__run_method_on_plugins(
+ "page_type_updated", default_value, page_type
+ )
+
+ def page_type_deleted(self, page_type: "PageType"):
+ default_value = None
+ return self.__run_method_on_plugins(
+ "page_type_deleted", default_value, page_type
+ )
+
def transaction_action_request(
self, payment_data: "TransactionActionData", channel_slug: str
):
diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.py
index 3033e8c503..fc24573f87 100644
--- a/saleor/plugins/webhook/plugin.py
+++ b/saleor/plugins/webhook/plugin.py
@@ -59,7 +59,7 @@ if TYPE_CHECKING:
from ...invoice.models import Invoice
from ...menu.models import Menu, MenuItem
from ...order.models import Fulfillment, Order
- from ...page.models import Page
+ from ...page.models import Page, PageType
from ...payment.interface import (
GatewayResponse,
PaymentData,
@@ -711,6 +711,39 @@ class WebhookPlugin(BasePlugin):
page_data, event_type, webhooks, page, self.requestor
)
+ def _trigger_page_type_event(self, event_type, page_type):
+ if webhooks := get_webhooks_for_event(event_type):
+ payload = {
+ "id": graphene.Node.to_global_id("PageType", page_type.id),
+ "name": page_type.name,
+ "slug": page_type.slug,
+ "meta": self._generate_meta(),
+ }
+ trigger_webhooks_async(
+ payload, event_type, webhooks, page_type, self.requestor
+ )
+
+ def page_type_created(self, page_type: "PageType", previous_value: Any) -> Any:
+ if not self.active:
+ return previous_value
+ self._trigger_page_type_event(
+ WebhookEventAsyncType.PAGE_TYPE_CREATED, page_type
+ )
+
+ def page_type_updated(self, page_type: "PageType", previous_value: Any) -> Any:
+ if not self.active:
+ return previous_value
+ self._trigger_page_type_event(
+ WebhookEventAsyncType.PAGE_TYPE_UPDATED, page_type
+ )
+
+ def page_type_deleted(self, page_type: "PageType", previous_value: Any) -> Any:
+ if not self.active:
+ return previous_value
+ self._trigger_page_type_event(
+ WebhookEventAsyncType.PAGE_TYPE_DELETED, page_type
+ )
+
def _trigger_shipping_price_event(self, event_type, shipping_method):
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 e5142b3ffa..1647f28021 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py
@@ -478,6 +478,27 @@ def subscription_page_deleted_webhook(subscription_webhook):
)
+def subscription_page_type_created_webhook(subscription_webhook):
+ return subscription_webhook(
+ subscription_queries.PAGE_TYPE_CREATED, WebhookEventAsyncType.PAGE_TYPE_CREATED
+ )
+
+
+def subscription_page_type_updated_webhook(subscription_webhook):
+ return subscription_webhook(
+ subscription_queries.PAGE_TYPE_UPDATED, WebhookEventAsyncType.PAGE_TYPE_UPDATED
+ )
+
+
+def subscription_page_type_deleted_webhook(subscription_webhook):
+ return subscription_webhook(
+ subscription_queries.PAGE_TYPE_DELETED, WebhookEventAsyncType.PAGE_TYPE_DELETED
+ )
+
+
@pytest.fixture
def subscription_product_created_multiple_events_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 76dd5f77f9..7c7adc0f5b 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/payloads.py
@@ -168,6 +168,20 @@ def generate_page_payload(page):
}
+def generate_page_type_payload(page_type):
+ page_type_id = graphene.Node.to_global_id("PageType", page_type.pk)
+ return {
+ "pageType": {
+ "id": page_type_id,
+ "name": page_type.name,
+ "slug": page_type.slug,
+ "attributes": [
+ {"slug": ap.attribute.slug} for ap in page_type.attributepage.all()
+ ],
+ }
+ }
+
+
def generate_invoice_payload(invoice):
return {
"invoice": {
diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
index ec41a9569b..432378ce2f 100644
--- a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
+++ b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py
@@ -878,6 +878,53 @@ PAGE_DELETED = (
"""
)
+
+PAGE_TYPE_CREATED = (
+ fragments.PAGE_TYPE_DETAILS
+ + """
+ subscription{
+ event{
+ ...on PageTypeCreated{
+ pageType{
+ ...PageTypeDetails
+ }
+ }
+ }
+ }
+"""
+)
+
+PAGE_TYPE_UPDATED = (
+ fragments.PAGE_TYPE_DETAILS
+ + """
+ subscription{
+ event{
+ ...on PageTypeUpdated{
+ pageType{
+ ...PageTypeDetails
+ }
+ }
+ }
+ }
+"""
+)
+
+PAGE_TYPE_DELETED = (
+ fragments.PAGE_TYPE_DETAILS
+ + """
+ subscription{
+ event{
+ ...on PageTypeDeleted{
+ pageType{
+ ...PageTypeDetails
+ }
+ }
+ }
+ }
+"""
+)
+
+
MULTIPLE_EVENTS = """
subscription{
event{
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 42147257ce..d28b38ec92 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
@@ -26,6 +26,7 @@ from .payloads import (
generate_menu_item_payload,
generate_menu_payload,
generate_page_payload,
+ generate_page_type_payload,
generate_sale_payload,
generate_shipping_method_payload,
generate_voucher_created_payload_with_meta,
@@ -1127,6 +1128,55 @@ def test_page_deleted(page, subscription_page_deleted_webhook):
assert deliveries[0].webhook == webhooks[0]
+def test_page_type_created(page_type, subscription_page_type_created_webhook):
+ # given
+ webhooks = [subscription_page_type_created_webhook]
+ event_type = WebhookEventAsyncType.PAGE_TYPE_CREATED
+ expected_payload = json.dumps(generate_page_type_payload(page_type))
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(event_type, page_type, webhooks)
+
+ # then
+ assert deliveries[0].payload.payload == expected_payload
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+
+
+def test_page_type_updated(page_type, subscription_page_type_updated_webhook):
+ # given
+ webhooks = [subscription_page_type_updated_webhook]
+ event_type = WebhookEventAsyncType.PAGE_TYPE_UPDATED
+ expected_payload = json.dumps(generate_page_type_payload(page_type))
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(event_type, page_type, webhooks)
+
+ # then
+ assert deliveries[0].payload.payload == expected_payload
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+
+
+def test_page_type_deleted(page_type, subscription_page_type_deleted_webhook):
+ # given
+ page_type_id = page_type.id
+ page_type.delete()
+ page_type.id = page_type_id
+
+ webhooks = [subscription_page_type_deleted_webhook]
+ event_type = WebhookEventAsyncType.PAGE_TYPE_DELETED
+ expected_payload = json.dumps(generate_page_type_payload(page_type))
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(event_type, page_type, webhooks)
+
+ # then
+ assert deliveries[0].payload.payload == expected_payload
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+
+
def test_product_created_multiple_events_in_subscription(
product, subscription_product_created_multiple_events_webhook
):
diff --git a/saleor/webhook/event_types.py b/saleor/webhook/event_types.py
index 103ffbdb52..f59369b28a 100644
--- a/saleor/webhook/event_types.py
+++ b/saleor/webhook/event_types.py
@@ -8,6 +8,7 @@ from ..core.permissions import (
MenuPermissions,
OrderPermissions,
PagePermissions,
+ PageTypePermissions,
PaymentPermissions,
ProductPermissions,
ShippingPermissions,
@@ -93,6 +94,10 @@ class WebhookEventAsyncType:
PAGE_UPDATED = "page_updated"
PAGE_DELETED = "page_deleted"
+ PAGE_TYPE_CREATED = "page_type_created"
+ PAGE_TYPE_UPDATED = "page_type_updated"
+ PAGE_TYPE_DELETED = "page_type_deleted"
+
SHIPPING_PRICE_CREATED = "shipping_price_created"
SHIPPING_PRICE_UPDATED = "shipping_price_updated"
SHIPPING_PRICE_DELETED = "shipping_price_deleted"
@@ -175,6 +180,9 @@ class WebhookEventAsyncType:
PAGE_CREATED: "Page Created",
PAGE_UPDATED: "Page Updated",
PAGE_DELETED: "Page Deleted",
+ PAGE_TYPE_CREATED: "Page type created",
+ PAGE_TYPE_UPDATED: "Page type updated",
+ PAGE_TYPE_DELETED: "Page type deleted",
SHIPPING_PRICE_CREATED: "Shipping price created",
SHIPPING_PRICE_UPDATED: "Shipping price updated",
SHIPPING_PRICE_DELETED: "Shipping price deleted",
@@ -252,6 +260,9 @@ class WebhookEventAsyncType:
(PAGE_CREATED, DISPLAY_LABELS[PAGE_CREATED]),
(PAGE_UPDATED, DISPLAY_LABELS[PAGE_UPDATED]),
(PAGE_DELETED, DISPLAY_LABELS[PAGE_DELETED]),
+ (PAGE_TYPE_CREATED, DISPLAY_LABELS[PAGE_TYPE_CREATED]),
+ (PAGE_TYPE_UPDATED, DISPLAY_LABELS[PAGE_TYPE_UPDATED]),
+ (PAGE_TYPE_DELETED, DISPLAY_LABELS[PAGE_TYPE_DELETED]),
(SHIPPING_PRICE_CREATED, DISPLAY_LABELS[SHIPPING_PRICE_CREATED]),
(SHIPPING_PRICE_UPDATED, DISPLAY_LABELS[SHIPPING_PRICE_UPDATED]),
(SHIPPING_PRICE_DELETED, DISPLAY_LABELS[SHIPPING_PRICE_DELETED]),
@@ -330,6 +341,9 @@ class WebhookEventAsyncType:
PAGE_CREATED: PagePermissions.MANAGE_PAGES,
PAGE_UPDATED: PagePermissions.MANAGE_PAGES,
PAGE_DELETED: PagePermissions.MANAGE_PAGES,
+ PAGE_TYPE_CREATED: PageTypePermissions.MANAGE_PAGE_TYPES_AND_ATTRIBUTES,
+ PAGE_TYPE_UPDATED: PageTypePermissions.MANAGE_PAGE_TYPES_AND_ATTRIBUTES,
+ PAGE_TYPE_DELETED: PageTypePermissions.MANAGE_PAGE_TYPES_AND_ATTRIBUTES,
SHIPPING_PRICE_CREATED: ShippingPermissions.MANAGE_SHIPPING,
SHIPPING_PRICE_UPDATED: ShippingPermissions.MANAGE_SHIPPING,
SHIPPING_PRICE_DELETED: ShippingPermissions.MANAGE_SHIPPING,
@@ -477,6 +491,9 @@ SUBSCRIBABLE_EVENTS = [
WebhookEventAsyncType.PAGE_CREATED,
WebhookEventAsyncType.PAGE_UPDATED,
WebhookEventAsyncType.PAGE_DELETED,
+ WebhookEventAsyncType.PAGE_TYPE_CREATED,
+ WebhookEventAsyncType.PAGE_TYPE_UPDATED,
+ WebhookEventAsyncType.PAGE_TYPE_DELETED,
WebhookEventAsyncType.SHIPPING_PRICE_CREATED,
WebhookEventAsyncType.SHIPPING_PRICE_UPDATED,
WebhookEventAsyncType.SHIPPING_PRICE_DELETED,