Skip to content

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

Move validation outside of mutation

Mira Szewczyk224264183554
diff --git a/saleor/graphql/warehouse/mutations.py b/saleor/graphql/warehouse/mutations.py
index 440a648ee4..7b4dc58c86 100644
--- a/saleor/graphql/warehouse/mutations.py
+++ b/saleor/graphql/warehouse/mutations.py
@@ -1,9 +1,9 @@
import graphene
from django.core.exceptions import ValidationError
-from ...shipping.models import ShippingZone
from ...warehouse import models
from ...warehouse.error_codes import WarehouseErrorCode
+from ...warehouse.validation import validate_warehouse_count
from ..account.i18n import I18nMixin
from ..core.mutations import ModelDeleteMutation, ModelMutation
from ..core.types.common import WarehouseError
@@ -22,33 +22,11 @@ ADDRESS_FIELDS = [
class WarehouseMixin:
- @classmethod
- def validate_warehouse_count(cls, shipping_zones, instance):
- """Every ShippingZone can be assigned to only one warehouse.
-
- If not there would be issue with automatically selecting stock for operation.
- """
-
- warehouses = set(
- ShippingZone.objects.filter(
- id__in=[shipping_zone.id for shipping_zone in shipping_zones]
- )
- .filter(warehouse__isnull=False)
- .values_list("warehouse", flat=True)
- )
- if not bool(warehouses):
- return True
- if len(warehouses) > 1:
- return False
- if instance.id is None:
- return False
- return warehouses == {instance.id}
-
@classmethod
def clean_input(cls, info, instance, data):
cleaned_input = super().clean_input(info, instance, data)
shipping_zones = cleaned_input.get("shipping_zones", [])
- if not cls.validate_warehouse_count(shipping_zones, instance):
+ if not validate_warehouse_count(shipping_zones, instance):
msg = "Shipping zone can be assigned only to one warehouse."
raise ValidationError(
{"shipping_zones": msg}, code=WarehouseErrorCode.INVALID
diff --git a/saleor/warehouse/validation.py b/saleor/warehouse/validation.py
new file mode 100644
index 0000000000..d26d856ed0
--- /dev/null
+++ b/saleor/warehouse/validation.py
@@ -0,0 +1,23 @@
+from ..shipping.models import ShippingZone
+
+
+def validate_warehouse_count(shipping_zones, instance) -> bool:
+ """Every ShippingZone can be assigned to only one warehouse.
+
+ If not there would be issue with automatically selecting stock for operation.
+ """
+
+ warehouses = set(
+ ShippingZone.objects.filter(
+ id__in=[shipping_zone.id for shipping_zone in shipping_zones]
+ )
+ .filter(warehouse__isnull=False)
+ .values_list("warehouse", flat=True)
+ )
+ if not bool(warehouses):
+ return True
+ if len(warehouses) > 1:
+ return False
+ if instance.id is None:
+ return False
+ return warehouses == {instance.id}