Skip to content

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

Get rid of django form in mutation

Mira Szewczyk96bb6abe8e6d
diff --git a/saleor/graphql/warehouse/forms.py b/saleor/graphql/warehouse/forms.py
deleted file mode 100644
index 3824b0445b..0000000000
--- a/saleor/graphql/warehouse/forms.py
+++ /dev/null
@@ -1,37 +0,0 @@
-from django import forms
-
-from ...shipping.models import ShippingZone
-from ...warehouse.models import Warehouse
-
-
-def get_available_shipping_zones():
- return ShippingZone.objects.filter(warehouse__isnull=True)
-
-
-class WarehouseForm(forms.ModelForm):
- class Meta:
- model = Warehouse
- fields = ["name", "company_name", "shipping_zones", "email"]
-
- def validate_warehouse_count(self, shipping_zones) -> bool:
- warehouses = set(
- shipping_zones.filter(warehouse__isnull=False).values_list(
- "warehouse", flat=True
- )
- )
- if not bool(warehouses):
- return True
- if len(warehouses) > 1:
- return False
- instance_id = self.instance.id
- if instance_id is None:
- return False
- return warehouses == {instance_id}
-
- def clean_shipping_zones(self):
- shipping_zones = self.cleaned_data["shipping_zones"]
- if not self.validate_warehouse_count(shipping_zones):
- raise forms.ValidationError(
- "Shipping zone can be assigned only to one warehouse."
- )
- return shipping_zones
diff --git a/saleor/graphql/warehouse/mutations.py b/saleor/graphql/warehouse/mutations.py
index 621a63a46b..f7fbfe2d88 100644
--- a/saleor/graphql/warehouse/mutations.py
+++ b/saleor/graphql/warehouse/mutations.py
@@ -1,11 +1,11 @@
import graphene
from django.core.exceptions import ValidationError
+from ...shipping.models import ShippingZone
from ...warehouse import models
from ..account.i18n import I18nMixin
from ..core.mutations import ModelDeleteMutation, ModelMutation
from ..core.types.common import WarehouseError
-from .forms import WarehouseForm
from .types import WarehouseCreateInput, WarehouseUpdateInput
ADDRESS_FIELDS = [
@@ -21,12 +21,30 @@ ADDRESS_FIELDS = [
class WarehouseMixin:
+ @classmethod
+ def validate_warehouse_count(cls, shipping_zones, instance):
+ 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)
- warehouse_form = WarehouseForm(cleaned_input, instance=instance)
- if not warehouse_form.is_valid():
- raise ValidationError(warehouse_form.errors.as_data())
+ shipping_zones = cleaned_input.get("shipping_zones", [])
+ if not cls.validate_warehouse_count(shipping_zones, instance):
+ msg = "Shipping zone can be assigned only to one warehouse."
+ raise ValidationError({"shipping_zones": msg})
return cleaned_input
@classmethod