Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move validation outside of mutation
Mira Szewczyk224264183554
diff --git a/saleor/graphql/warehouse/mutations.py b/saleor/graphql/warehouse/mutations.pyindex 440a648ee4..7b4dc58c86 100644--- a/saleor/graphql/warehouse/mutations.py+++ b/saleor/graphql/warehouse/mutations.py@@ -1,9 +1,9 @@import graphenefrom django.core.exceptions import ValidationError-from ...shipping.models import ShippingZonefrom ...warehouse import modelsfrom ...warehouse.error_codes import WarehouseErrorCode+from ...warehouse.validation import validate_warehouse_countfrom ..account.i18n import I18nMixinfrom ..core.mutations import ModelDeleteMutation, ModelMutationfrom ..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}-@classmethoddef 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.INVALIDdiff --git a/saleor/warehouse/validation.py b/saleor/warehouse/validation.pynew file mode 100644index 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}