Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add MoneyScalar
IKarbowiakb600dab9cc8b
diff --git a/saleor/graphql/core/scalars.py b/saleor/graphql/core/scalars.pyindex df91269b00..6b9df69184 100644--- a/saleor/graphql/core/scalars.py+++ b/saleor/graphql/core/scalars.py@@ -1,6 +1,8 @@import decimalimport graphene+from django.conf import settings+from django_prices.utils.formatting import get_currency_fractionfrom graphql.error import GraphQLErrorfrom graphql.language import astfrom measurement.measures import Weight@@ -36,6 +38,32 @@ class Decimal(graphene.Float):return None+class MoneyScalar(Decimal):+ """Money scalar implementation.++ Should be used in every price input field.+ Contains validations that check if the money value is not lower than 0 and+ if the value does not contain too many decimal values.+ """++ @staticmethod+ def parse_value(value):+ value = super(MoneyScalar, MoneyScalar).parse_value(value)+ if not value:+ return value+ if value < 0:+ raise GraphQLError("Money value cannot be lower than 0.")++ currency_fraction = get_currency_fraction(settings.DEFAULT_CURRENCY)+ value = value.normalize()+ if abs(value.as_tuple().exponent) > currency_fraction:+ raise GraphQLError(+ "Ensure the provided money field value has no more than "+ f"{currency_fraction} decimal places."+ )+ return value++class WeightScalar(graphene.Scalar):@staticmethoddef parse_value(value):