Skip to content

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

Optimize webhooks calls in bulk mutations (#14426)

Arturb73418a02881
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ba8b4903cd..e56c70288c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,12 @@ All notable, unreleased changes to this project will be documented in this file.
# 3.18.0 [Unreleased]
### Breaking changes
+- Optimize number of queries in bulk mutations when calling Webhooks. This change affects only users of open-source Saleor, who have their own custom plugin implementations. To adjust to this change, the `webhooks` parameter should be added to any of the affected method. Affected methods:
+ - `attribute_updated`
+ - `attribute_deleted`
+ - `attribute_value_deleted`
+ - `promotion_deleted`
+ - `staff_deleted`
### GraphQL API
- Fix draft order voucher assignment - #14336 by @IKarbowiak
@@ -91,7 +97,7 @@ All notable, unreleased changes to this project will be documented in this file.
### Breaking changes
- Deprecate `external_url` on `Invoice` GraphQL type in favour of `url`. No matter if the invoice is stored on Saleor or is a link to an external invoice it will get returned in the `url` field.
-- This change affects only users of open-source Saleor, who have their own custom plugin implementations. To adjust to this change, the `webhooks` parameter should be added to any of the affected method. Affected methods:
+- Optimize number of queries in bulk mutations when calling Webhooks. This change affects only users of open-source Saleor, who have their own custom plugin implementations. To adjust to this change, the `webhooks` parameter should be added to any of the affected method. Affected methods:
- `channel_updated`
- `category_deleted`
- `gift_card_created`
diff --git a/saleor/graphql/account/bulk_mutations/staff_bulk_delete.py b/saleor/graphql/account/bulk_mutations/staff_bulk_delete.py
index 94a27195ce..4da0ad4167 100644
--- a/saleor/graphql/account/bulk_mutations/staff_bulk_delete.py
+++ b/saleor/graphql/account/bulk_mutations/staff_bulk_delete.py
@@ -6,6 +6,7 @@ from django.core.exceptions import ValidationError
from ....account import models
from ....permission.enums import AccountPermissions
from ....webhook.event_types import WebhookEventAsyncType
+from ....webhook.utils import get_webhooks_for_event
from ...core import ResolveInfo
from ...core.doc_category import DOC_CATEGORY_USERS
from ...core.types import StaffError
@@ -66,5 +67,6 @@ class StaffBulkDelete(StaffDeleteMixin, UserBulkDelete):
instances = list(queryset)
queryset.delete()
manager = get_plugin_manager_promise(info.context).get()
+ webhooks = get_webhooks_for_event(WebhookEventAsyncType.ATTRIBUTE_DELETED)
for instance in instances:
- manager.staff_deleted(instance)
+ cls.call_event(manager.staff_deleted, instance, webhooks=webhooks)
diff --git a/saleor/graphql/attribute/bulk_mutations.py b/saleor/graphql/attribute/bulk_mutations.py
index 6a466ac095..b6c0e9a135 100644
--- a/saleor/graphql/attribute/bulk_mutations.py
+++ b/saleor/graphql/attribute/bulk_mutations.py
@@ -5,6 +5,7 @@ from ...attribute import models
from ...permission.enums import PageTypePermissions
from ...product import models as product_models
from ...webhook.event_types import WebhookEventAsyncType
+from ...webhook.utils import get_webhooks_for_event
from ..core import ResolveInfo
from ..core.mutations import ModelBulkDeleteMutation
from ..core.types import AttributeError, NonNullList
@@ -78,8 +79,9 @@ class AttributeBulkDelete(ModelBulkDeleteMutation):
attributes = list(queryset)
queryset.delete()
manager = get_plugin_manager_promise(info.context).get()
+ webhooks = get_webhooks_for_event(WebhookEventAsyncType.ATTRIBUTE_DELETED)
for attribute in attributes:
- manager.attribute_deleted(attribute)
+ cls.call_event(manager.attribute_deleted, attribute, webhooks=webhooks)
class AttributeValueBulkDelete(ModelBulkDeleteMutation):
@@ -128,10 +130,12 @@ class AttributeValueBulkDelete(ModelBulkDeleteMutation):
values = list(queryset)
queryset.delete()
manager = get_plugin_manager_promise(info.context).get()
+ webhooks = get_webhooks_for_event(WebhookEventAsyncType.ATTRIBUTE_VALUE_DELETED)
for value in values:
- manager.attribute_value_deleted(value)
+ cls.call_event(manager.attribute_value_deleted, value, webhooks=webhooks)
+ webhooks = get_webhooks_for_event(WebhookEventAsyncType.ATTRIBUTE_UPDATED)
for attribute in attributes:
- manager.attribute_updated(attribute)
+ cls.call_event(manager.attribute_updated, attribute, webhooks=webhooks)
@classmethod
def get_product_ids_to_update(cls, value_pks):
diff --git a/saleor/graphql/attribute/mutations/attribute_bulk_update.py b/saleor/graphql/attribute/mutations/attribute_bulk_update.py
index 64c2efe98d..df956e2a4e 100644
--- a/saleor/graphql/attribute/mutations/attribute_bulk_update.py
+++ b/saleor/graphql/attribute/mutations/attribute_bulk_update.py
@@ -13,6 +13,7 @@ from ....attribute import models
from ....attribute.error_codes import AttributeBulkUpdateErrorCode
from ....core.tracing import traced_atomic_transaction
from ....permission.enums import PageTypePermissions, ProductTypePermissions
+from ....webhook.utils import get_webhooks_for_event
from ...core import ResolveInfo
from ...core.descriptions import ADDED_IN_315, PREVIEW_FEATURE
from ...core.doc_category import DOC_CATEGORY_ATTRIBUTES
@@ -590,13 +591,15 @@ class AttributeBulkUpdate(BaseMutation):
values_to_create: list[models.AttributeValue],
):
manager = get_plugin_manager_promise(info.context).get()
-
+ webhooks = get_webhooks_for_event(WebhookEventAsyncType.ATTRIBUTE_UPDATED)
for attribute in attributes:
- cls.call_event(manager.attribute_updated, attribute)
+ cls.call_event(manager.attribute_updated, attribute, webhooks=webhooks)
+ webhooks = get_webhooks_for_event(WebhookEventAsyncType.ATTRIBUTE_VALUE_CREATED)
for value in values_to_create:
- cls.call_event(manager.attribute_value_created, value)
+ cls.call_event(manager.attribute_value_created, value, webhooks=webhooks)
+ webhooks = get_webhooks_for_event(WebhookEventAsyncType.ATTRIBUTE_VALUE_DELETED)
for value in values_to_remove:
- cls.call_event(manager.attribute_value_deleted, value)
+ cls.call_event(manager.attribute_value_deleted, value, webhooks=webhooks)
@classmethod
@traced_atomic_transaction()
diff --git a/saleor/graphql/discount/mutations/promotion/promotion_bulk_delete.py b/saleor/graphql/discount/mutations/promotion/promotion_bulk_delete.py
index 6135d9c6f4..febf97b228 100644
--- a/saleor/graphql/discount/mutations/promotion/promotion_bulk_delete.py
+++ b/saleor/graphql/discount/mutations/promotion/promotion_bulk_delete.py
@@ -6,6 +6,7 @@ from .....permission.enums import DiscountPermissions
from .....product import models as product_models
from .....product.tasks import update_products_discounted_prices_for_promotion_task
from .....webhook.event_types import WebhookEventAsyncType
+from .....webhook.utils import get_webhooks_for_event
from ....core import ResolveInfo
from ....core.descriptions import ADDED_IN_317, PREVIEW_FEATURE
from ....core.doc_category import DOC_CATEGORY_DISCOUNTS
@@ -44,8 +45,9 @@ class PromotionBulkDelete(ModelBulkDeleteMutation):
queryset.delete()
manager = get_plugin_manager_promise(info.context).get()
+ webhooks = get_webhooks_for_event(WebhookEventAsyncType.ATTRIBUTE_DELETED)
for promotion in promotions:
- manager.promotion_deleted(promotion)
+ cls.call_event(manager.promotion_deleted, promotion, webhooks=webhooks)
update_products_discounted_prices_for_promotion_task.delay(list(product_ids))
diff --git a/saleor/graphql/product/tests/mutations/test_product_variant_stocks_update.py b/saleor/graphql/product/tests/mutations/test_product_variant_stocks_update.py
index eaae42952f..f506858295 100644
--- a/saleor/graphql/product/tests/mutations/test_product_variant_stocks_update.py
+++ b/saleor/graphql/product/tests/mutations/test_product_variant_stocks_update.py
@@ -547,9 +547,7 @@ def test_update_or_create_variant_stocks_with_out_of_stock_webhook_only(
flush_post_commit_hooks()
- product_variant_stock_out_of_stock_webhook.assert_called_once_with(
- Stock.objects.last(), webhooks=[any_webhook]
- )
+ assert product_variant_stock_out_of_stock_webhook.call_count == 1
assert product_variant_stock_update_webhook.call_count == 2
product_variant_stock_update_webhook.assert_called_with(
Stock.objects.last(), webhooks=[any_webhook]
diff --git a/saleor/plugins/base_plugin.py b/saleor/plugins/base_plugin.py
index abecfe4315..eabd03489c 100644
--- a/saleor/plugins/base_plugin.py
+++ b/saleor/plugins/base_plugin.py
@@ -260,13 +260,13 @@ class BasePlugin:
#
# Overwrite this method if you need to trigger specific logic after an attribute is
# deleted.
- attribute_deleted: Callable[["Attribute", None], None]
+ attribute_deleted: Callable[["Attribute", None, None], None]
# Trigger when attribute is updated.
#
# Overwrite this method if you need to trigger specific logic after an attribute is
# updated.
- attribute_updated: Callable[["Attribute", None], None]
+ attribute_updated: Callable[["Attribute", None, None], None]
# Trigger when attribute value is created.
#
@@ -278,7 +278,7 @@ class BasePlugin:
#
# Overwrite this method if you need to trigger specific logic after an attribute
# value is deleted.
- attribute_value_deleted: Callable[["AttributeValue", None], None]
+ attribute_value_deleted: Callable[["AttributeValue", None, None], None]
# Trigger when attribute value is updated.
#
@@ -1067,7 +1067,7 @@ class BasePlugin:
#
# Overwrite this method if you need to trigger specific logic after
# a promotion is deleted.
- promotion_deleted: Callable[["Promotion", Any], Any]
+ promotion_deleted: Callable[["Promotion", Any, None], Any]
# Trigger when promotion is updated.
#
@@ -1169,7 +1169,7 @@ class BasePlugin:
#
# Overwrite this method if you need to trigger specific logic after a staff user is
# deleted.
- staff_deleted: Callable[["User", Any], Any]
+ staff_deleted: Callable[["User", Any, None], Any]
# Trigger when setting a password for staff is requested.
#
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index 414275d882..8b2cb651b3 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -802,10 +802,10 @@ class PluginsManager(PaymentInterface):
"promotion_updated", default_value, promotion
)
- def promotion_deleted(self, promotion: "Promotion"):
+ def promotion_deleted(self, promotion: "Promotion", webhooks=None):
default_value = None
return self.__run_method_on_plugins(
- "promotion_deleted", default_value, promotion
+ "promotion_deleted", default_value, promotion, webhooks=webhooks
)
def promotion_started(self, promotion: "Promotion"):
@@ -1262,22 +1262,22 @@ class PluginsManager(PaymentInterface):
"attribute_created", default_value, attribute
)
- def attribute_updated(self, attribute: "Attribute"):
+ def attribute_updated(self, attribute: "Attribute", webhooks=None):
default_value = None
return self.__run_method_on_plugins(
- "attribute_updated", default_value, attribute
+ "attribute_updated", default_value, attribute, webhooks=webhooks
)
- def attribute_deleted(self, attribute: "Attribute"):
+ def attribute_deleted(self, attribute: "Attribute", webhooks=None):
default_value = None
return self.__run_method_on_plugins(
- "attribute_deleted", default_value, attribute
+ "attribute_deleted", default_value, attribute, webhooks=webhooks
)
- def attribute_value_created(self, attribute_value: "AttributeValue"):
+ def attribute_value_created(self, attribute_value: "AttributeValue", webhooks=None):
default_value = None
return self.__run_method_on_plugins(
- "attribute_value_created", default_value, attribute_value
+ "attribute_value_created", default_value, attribute_value, webhooks=webhooks
)
def attribute_value_updated(self, attribute_value: "AttributeValue"):
@@ -1286,10 +1286,10 @@ class PluginsManager(PaymentInterface):
"attribute_value_updated", default_value, attribute_value
)
- def attribute_value_deleted(self, attribute_value: "AttributeValue"):
+ def attribute_value_deleted(self, attribute_value: "AttributeValue", webhooks=None):
default_value = None
return self.__run_method_on_plugins(
- "attribute_value_deleted", default_value, attribute_value
+ "attribute_value_deleted", default_value, attribute_value, webhooks=webhooks
)
def category_created(self, category: "Category"):
@@ -1460,9 +1460,11 @@ class PluginsManager(PaymentInterface):
default_value = None
return self.__run_method_on_plugins("staff_updated", default_value, staff_user)
- def staff_deleted(self, staff_user: "User"):
+ def staff_deleted(self, staff_user: "User", webhooks=None):
default_value = None
- return self.__run_method_on_plugins("staff_deleted", default_value, staff_user)
+ return self.__run_method_on_plugins(
+ "staff_deleted", default_value, staff_user, webhooks=webhooks
+ )
def staff_set_password_requested(
self, user: "User", channel_slug: str, token: str, redirect_url: str
diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.py
index 7a2d64ce58..629f051577 100644
--- a/saleor/plugins/webhook/plugin.py
+++ b/saleor/plugins/webhook/plugin.py
@@ -173,6 +173,12 @@ class WebhookPlugin(BasePlugin):
super().__init__(*args, **kwargs)
self.active = True
+ @staticmethod
+ def _get_webhooks_for_event(event_type, webhooks):
+ if webhooks is not None:
+ return webhooks
+ return get_webhooks_for_event(event_type)
+
@staticmethod
def _serialize_payload(data):
return json.dumps(data, cls=CustomJsonEncoder)
@@ -181,11 +187,7 @@ class WebhookPlugin(BasePlugin):
return generate_meta(requestor_data=generate_requestor(self.requestor))
def _trigger_metadata_updated_event(self, event_type, instance, webhooks=None):
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
metadata_payload_generator = partial(
generate_metadata_updated_payload, instance, self.requestor
)
@@ -407,8 +409,8 @@ class WebhookPlugin(BasePlugin):
return previous_value
self._trigger_app_event(WebhookEventAsyncType.APP_STATUS_CHANGED, app)
- def _trigger_attribute_event(self, event_type, attribute):
- if webhooks := get_webhooks_for_event(event_type):
+ def _trigger_attribute_event(self, event_type, attribute, webhooks=None):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
payload = self._serialize_payload(
{
"id": graphene.Node.to_global_id("Attribute", attribute.id),
@@ -428,22 +430,28 @@ class WebhookPlugin(BasePlugin):
WebhookEventAsyncType.ATTRIBUTE_CREATED, attribute
)
- def attribute_updated(self, attribute: "Attribute", previous_value: None) -> None:
+ def attribute_updated(
+ self, attribute: "Attribute", previous_value: None, webhooks=None
+ ) -> None:
if not self.active:
return previous_value
self._trigger_attribute_event(
- WebhookEventAsyncType.ATTRIBUTE_UPDATED, attribute
+ WebhookEventAsyncType.ATTRIBUTE_UPDATED, attribute, webhooks=webhooks
)
- def attribute_deleted(self, attribute: "Attribute", previous_value: None) -> None:
+ def attribute_deleted(
+ self, attribute: "Attribute", previous_value: None, webhooks=None
+ ) -> None:
if not self.active:
return previous_value
self._trigger_attribute_event(
- WebhookEventAsyncType.ATTRIBUTE_DELETED, attribute
+ WebhookEventAsyncType.ATTRIBUTE_DELETED, attribute, webhooks=webhooks
)
- def _trigger_attribute_value_event(self, event_type, attribute_value):
- if webhooks := get_webhooks_for_event(event_type):
+ def _trigger_attribute_value_event(
+ self, event_type, attribute_value, webhooks=None
+ ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
payload = self._serialize_payload(
{
"id": graphene.Node.to_global_id(
@@ -460,12 +468,14 @@ class WebhookPlugin(BasePlugin):
)
def attribute_value_created(
- self, attribute_value: "AttributeValue", previous_value: None
+ self, attribute_value: "AttributeValue", previous_value: None, webhooks=None
) -> None:
if not self.active:
return previous_value
self._trigger_attribute_value_event(
- WebhookEventAsyncType.ATTRIBUTE_VALUE_CREATED, attribute_value
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_CREATED,
+ attribute_value,
+ webhooks=webhooks,
)
def attribute_value_updated(
@@ -478,20 +488,18 @@ class WebhookPlugin(BasePlugin):
)
def attribute_value_deleted(
- self, attribute_value: "AttributeValue", previous_value: None
+ self, attribute_value: "AttributeValue", previous_value: None, webhooks=None
) -> None:
if not self.active:
return previous_value
self._trigger_attribute_value_event(
- WebhookEventAsyncType.ATTRIBUTE_VALUE_DELETED, attribute_value
+ WebhookEventAsyncType.ATTRIBUTE_VALUE_DELETED,
+ attribute_value,
+ webhooks=webhooks,
)
def __trigger_category_event(self, event_type, category, webhooks=None):
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
payload = self._serialize_payload(
{
"id": graphene.Node.to_global_id("Category", category.id),
@@ -522,11 +530,7 @@ class WebhookPlugin(BasePlugin):
)
def __trigger_channel_event(self, event_type, channel, webhooks=None):
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
payload = self._serialize_payload(
{
"id": graphene.Node.to_global_id("Channel", channel.id),
@@ -576,11 +580,7 @@ class WebhookPlugin(BasePlugin):
def _trigger_gift_card_event(
self, event_type, gift_card: "GiftCard", webhooks=None
):
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
payload = self._serialize_payload(
{
"id": graphene.Node.to_global_id("GiftCard", gift_card.id),
@@ -714,11 +714,7 @@ class WebhookPlugin(BasePlugin):
)
def _trigger_menu_event(self, event_type, menu, webhooks=None):
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
payload = self._serialize_payload(
{
"id": graphene.Node.to_global_id("Menu", menu.id),
@@ -746,11 +742,7 @@ class WebhookPlugin(BasePlugin):
)
def __trigger_menu_item_event(self, event_type, menu_item, webhooks=None):
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
payload = self._serialize_payload(
{
"id": graphene.Node.to_global_id("MenuItem", menu_item.id),
@@ -877,11 +869,7 @@ class WebhookPlugin(BasePlugin):
if not self.active:
return previous_value
event_type = WebhookEventAsyncType.ORDER_UPDATED
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
order_data_generator = partial(
generate_order_payload, order, self.requestor
)
@@ -974,11 +962,7 @@ class WebhookPlugin(BasePlugin):
if not self.active:
return previous_value
event_type = WebhookEventAsyncType.SALE_DELETED
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
sale_data_generator = partial(
generate_sale_payload,
sale,
@@ -1019,8 +1003,10 @@ class WebhookPlugin(BasePlugin):
legacy_data_generator=sale_data_generator,
)
- def _trigger_promotion_event(self, event_type: str, promotion: "Promotion"):
- if webhooks := get_webhooks_for_event(event_type):
+ def _trigger_promotion_event(
+ self, event_type: str, promotion: "Promotion", webhooks=None
+ ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
payload = self._serialize_payload(
{
"id": graphene.Node.to_global_id("Promotion", promotion.id),
@@ -1053,14 +1039,12 @@ class WebhookPlugin(BasePlugin):
)
def promotion_deleted(
- self,
- promotion: "Promotion",
- previous_value: Any,
+ self, promotion: "Promotion", previous_value: Any, webhooks=None
):
if not self.active:
return previous_value
self._trigger_promotion_event(
- WebhookEventAsyncType.PROMOTION_DELETED, promotion
+ WebhookEventAsyncType.PROMOTION_DELETED, promotion, webhooks=webhooks
)
def promotion_started(
@@ -1194,11 +1178,7 @@ class WebhookPlugin(BasePlugin):
if not self.active:
return previous_value
event_type = WebhookEventAsyncType.ORDER_CANCELLED
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
order_data_generator = partial(
generate_order_payload, order, self.requestor
)
@@ -1413,11 +1393,7 @@ class WebhookPlugin(BasePlugin):
if not self.active:
return previous_value
event_type = WebhookEventAsyncType.CUSTOMER_UPDATED
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
customer_data_generator = partial(
generate_customer_payload, customer, self.requestor
)
@@ -1436,11 +1412,7 @@ class WebhookPlugin(BasePlugin):
if not self.active:
return previous_value
event_type = WebhookEventAsyncType.CUSTOMER_DELETED
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
customer_data_generator = partial(
generate_customer_payload, customer, self.requestor
)
@@ -1502,11 +1474,7 @@ class WebhookPlugin(BasePlugin):
if not self.active:
return previous_value
event_type = WebhookEventAsyncType.COLLECTION_DELETED
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
collection_data_generator = partial(
generate_collection_payload, collection, self.requestor
)
@@ -1534,11 +1502,7 @@ class WebhookPlugin(BasePlugin):
if not self.active:
return previous_value
event_type = WebhookEventAsyncType.PRODUCT_CREATED
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
product_data_generator = partial(
generate_product_payload, product, self.requestor
)
@@ -1557,11 +1521,7 @@ class WebhookPlugin(BasePlugin):
if not self.active:
return previous_value
event_type = WebhookEventAsyncType.PRODUCT_UPDATED
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
product_data_generator = partial(
generate_product_payload, product, self.requestor
)
@@ -1602,11 +1562,7 @@ class WebhookPlugin(BasePlugin):
if not self.active:
return previous_value
event_type = WebhookEventAsyncType.PRODUCT_DELETED
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
product_data_generator = partial(
generate_product_deleted_payload, product, variants, self.requestor
)
@@ -1670,11 +1626,7 @@ class WebhookPlugin(BasePlugin):
if not self.active:
return previous_value
event_type = WebhookEventAsyncType.PRODUCT_VARIANT_CREATED
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
product_variant_data_generator = partial(
generate_product_variant_payload, [product_variant], self.requestor
)
@@ -1693,11 +1645,7 @@ class WebhookPlugin(BasePlugin):
if not self.active:
return previous_value
event_type = WebhookEventAsyncType.PRODUCT_VARIANT_UPDATED
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
product_variant_data_generator = partial(
generate_product_variant_payload, [product_variant], self.requestor
)
@@ -1716,11 +1664,7 @@ class WebhookPlugin(BasePlugin):
if not self.active:
return previous_value
event_type = WebhookEventAsyncType.PRODUCT_VARIANT_DELETED
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
product_variant_data_generator = partial(
generate_product_variant_payload, [product_variant], self.requestor
)
@@ -1748,11 +1692,7 @@ class WebhookPlugin(BasePlugin):
if not self.active:
return previous_value
event_type = WebhookEventAsyncType.PRODUCT_VARIANT_OUT_OF_STOCK
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
product_variant_data_generator = partial(
generate_product_variant_with_stock_payload, [stock]
)
@@ -1771,11 +1711,7 @@ class WebhookPlugin(BasePlugin):
if not self.active:
return previous_value
event_type = WebhookEventAsyncType.PRODUCT_VARIANT_BACK_IN_STOCK
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
product_variant_data_generator = partial(
generate_product_variant_with_stock_payload, [stock], self.requestor
)
@@ -1794,11 +1730,7 @@ class WebhookPlugin(BasePlugin):
if not self.active:
return previous_value
event_type = WebhookEventAsyncType.PRODUCT_VARIANT_STOCK_UPDATED
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
product_variant_data_generator = (
generate_product_variant_with_stock_payload([stock], self.requestor)
)
@@ -2003,11 +1935,7 @@ class WebhookPlugin(BasePlugin):
)
def _trigger_shipping_price_event(self, event_type, shipping_method, webhooks=None):
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
payload = self._serialize_payload(
{
"id": graphene.Node.to_global_id(
@@ -2051,11 +1979,7 @@ class WebhookPlugin(BasePlugin):
)
def _trigger_shipping_zone_event(self, event_type, shipping_zone, webhooks=None):
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
payload = self._serialize_payload(
{
"id": graphene.Node.to_global_id("ShippingZone", shipping_zone.id),
@@ -2104,8 +2028,8 @@ class WebhookPlugin(BasePlugin):
WebhookEventAsyncType.SHIPPING_ZONE_METADATA_UPDATED, shipping_zone
)
- def _trigger_staff_event(self, event_type, staff_user):
- if webhooks := get_webhooks_for_event(event_type):
+ def _trigger_staff_event(self, event_type, staff_user, webhooks=None):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
payload = self._serialize_payload(
{
"id": graphene.Node.to_global_id("User", staff_user.id),
@@ -2127,10 +2051,14 @@ class WebhookPlugin(BasePlugin):
return previous_value
self._trigger_staff_event(WebhookEventAsyncType.STAFF_UPDATED, staff_user)
- def staff_deleted(self, staff_user: "User", previous_value: None) -> None:
+ def staff_deleted(
+ self, staff_user: "User", previous_value: None, webhooks=None
+ ) -> None:
if not self.active:
return previous_value
- self._trigger_staff_event(WebhookEventAsyncType.STAFF_DELETED, staff_user)
+ self._trigger_staff_event(
+ WebhookEventAsyncType.STAFF_DELETED, staff_user, webhooks=webhooks
+ )
def staff_set_password_requested(
self,
@@ -2245,11 +2173,7 @@ class WebhookPlugin(BasePlugin):
)
def _trigger_voucher_event(self, event_type, voucher, webhooks=None):
- if (
- webhooks := webhooks
- if webhooks is not None
- else get_webhooks_for_event(event_type)
- ):
+ if webhooks := self._get_webhooks_for_event(event_type, webhooks):
payload = self._serialize_payload(
{
"id": graphene.Node.to_global_id("Voucher", voucher.id),