Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Extract enums for shippings
Marcin Gębalaab68001bb6c2
diff --git a/saleor/graphql/core/scalars.py b/saleor/graphql/core/scalars.pyindex d89d4b626e..6b73a209ff 100644--- a/saleor/graphql/core/scalars.py+++ b/saleor/graphql/core/scalars.py@@ -1,5 +1,8 @@import decimalimport graphene+from measurement.measures import Weight++from ...core.weight import convert_weight, get_default_weight_unitclass Decimal(graphene.Float):@@ -25,3 +28,29 @@ class Decimal(graphene.Float):return decimal.Decimal(value)except decimal.DecimalException:return None+++class WeightScalar(graphene.Scalar):+ @staticmethod+ def parse_value(value):+ # Expects value to be a string "amount unit" separated by a single+ # space.+ try:+ value = decimal.Decimal(value)+ except decimal.DecimalException:+ return None+ default_unit = get_default_weight_unit()+ return Weight(**{default_unit: value})++ @staticmethod+ def serialize(weight):+ if isinstance(weight, Weight):+ default_unit = get_default_weight_unit()+ if weight.unit != default_unit:+ weight = convert_weight(weight, default_unit)+ return str(weight)+ return None++ @staticmethod+ def parse_literal(node):+ return nodediff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.pyindex 427005657c..3b671efac9 100644--- a/saleor/graphql/product/mutations/products.py+++ b/saleor/graphql/product/mutations/products.py@@ -13,10 +13,9 @@ from ....product.thumbnails import (from ....product.utils.attributes import get_name_from_attributesfrom ...core.enums import TaxRateTypefrom ...core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation-from ...core.scalars import Decimal+from ...core.scalars import Decimal, WeightScalarfrom ...core.types import SeoInput, Uploadfrom ...core.utils import clean_seo_fields-from ...shipping.types import WeightScalarfrom ..types import Category, Collection, Product, ProductImage, ProductVariantfrom ..utils import attributes_to_hstorediff --git a/saleor/graphql/shipping/enums.py b/saleor/graphql/shipping/enums.pynew file mode 100644index 0000000000..3eac84056d--- /dev/null+++ b/saleor/graphql/shipping/enums.py@@ -0,0 +1,8 @@+import graphene++from ...shipping import ShippingMethodType+++ShippingMethodTypeEnum = graphene.Enum(+ 'ShippingMethodTypeEnum',+ [(code.upper(), code) for code, name in ShippingMethodType.CHOICES])diff --git a/saleor/graphql/shipping/mutations.py b/saleor/graphql/shipping/mutations.pyindex d3e07db88a..688776a743 100644--- a/saleor/graphql/shipping/mutations.py+++ b/saleor/graphql/shipping/mutations.py@@ -5,8 +5,9 @@ import graphenefrom ...dashboard.shipping.forms import default_shipping_zone_existsfrom ...shipping import modelsfrom ..core.mutations import ModelDeleteMutation, ModelMutation-from ..core.scalars import Decimal-from .types import ShippingMethodTypeEnum, ShippingZone, WeightScalar+from ..core.scalars import Decimal, WeightScalar+from .enums import ShippingMethodTypeEnum+from .types import ShippingZoneclass ShippingPriceInput(graphene.InputObjectType):diff --git a/saleor/graphql/shipping/types.py b/saleor/graphql/shipping/types.pyindex 9fd1a7d6e2..34224ee3f1 100644--- a/saleor/graphql/shipping/types.py+++ b/saleor/graphql/shipping/types.py@@ -1,20 +1,13 @@-import decimalfrom textwrap import dedentimport grapheneimport graphene_django_optimizer as gql_optimizerfrom graphene import relay-from graphene.types import Scalar-from measurement.measures import Weight-from ...core.weight import convert_weight, get_default_weight_unit-from ...shipping import ShippingMethodType, models+from ...shipping import modelsfrom ..core.connection import CountableDjangoObjectTypefrom ..core.types import CountryDisplay, MoneyRange--ShippingMethodTypeEnum = graphene.Enum(- 'ShippingMethodTypeEnum',- [(code.upper(), code) for code, name in ShippingMethodType.CHOICES])+from .enums import ShippingMethodTypeEnumclass ShippingMethod(CountableDjangoObjectType):@@ -62,30 +55,3 @@ class ShippingZone(CountableDjangoObjectType):def resolve_shipping_methods(self, info):return self.shipping_methods.all()---class WeightScalar(Scalar):- @staticmethod- def parse_value(value):- """Excepts value to be a string "amount unit"- separated by a single space.- """- try:- value = decimal.Decimal(value)- except decimal.DecimalException:- return None- default_unit = get_default_weight_unit()- return Weight(**{default_unit: value})-- @staticmethod- def serialize(weight):- if isinstance(weight, Weight):- default_unit = get_default_weight_unit()- if weight.unit != default_unit:- weight = convert_weight(weight, default_unit)- return str(weight)- return None-- @staticmethod- def parse_literal(node):- return node