Skip to content

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

Change validate_price_amount to validate_price_precision and update PositiveDecimal scalar

IKarbowiakbd49c11e5d43
diff --git a/saleor/graphql/core/scalars.py b/saleor/graphql/core/scalars.py
index 02e198d7a9..1cc3521477 100644
--- a/saleor/graphql/core/scalars.py
+++ b/saleor/graphql/core/scalars.py
@@ -45,9 +45,7 @@ class PositiveDecimal(Decimal):
@staticmethod
def parse_value(value):
value = super(PositiveDecimal, PositiveDecimal).parse_value(value)
- if not value:
- return value
- if value < 0:
+ if value and value < 0:
raise GraphQLError(
f"Value cannot be lower than 0. Unsupported value: {value}"
)
diff --git a/saleor/graphql/core/tests/test_validators.py b/saleor/graphql/core/tests/test_validators.py
index 23c9567b79..b058e99178 100644
--- a/saleor/graphql/core/tests/test_validators.py
+++ b/saleor/graphql/core/tests/test_validators.py
@@ -3,7 +3,7 @@ from decimal import Decimal
import pytest
from django.core.exceptions import ValidationError
-from ..validators import validate_price_amount
+from ..validators import validate_price_precision
@pytest.mark.parametrize(
@@ -17,9 +17,9 @@ from ..validators import validate_price_amount
(Decimal("5.12"), None),
],
)
-def test_validate_price_amount(value, currency):
+def test_validate_price_precision(value, currency):
# when
- result = validate_price_amount(value, currency)
+ result = validate_price_precision(value, currency)
# then
assert result is None
@@ -35,6 +35,6 @@ def test_validate_price_amount(value, currency):
(Decimal("5.123"), None),
],
)
-def test_validate_price_amount_raise_error(value, currency):
+def test_validate_price_precision_raise_error(value, currency):
with pytest.raises(ValidationError):
- validate_price_amount(value, currency)
+ validate_price_precision(value, currency)
diff --git a/saleor/graphql/core/validators.py b/saleor/graphql/core/validators.py
index 4a4ab62650..a14ae23232 100644
--- a/saleor/graphql/core/validators.py
+++ b/saleor/graphql/core/validators.py
@@ -20,7 +20,7 @@ def validate_one_of_args_is_in_query(arg1_name, arg1, arg2_name, arg2):
)
-def validate_price_amount(value: Optional["Decimal"], currency: str = None):
+def validate_price_precision(value: Optional["Decimal"], currency: str = None):
"""Validate if price amount does not have too many decimal places.
Price amount can't have more decimal places than currency allow to.
diff --git a/saleor/graphql/discount/mutations.py b/saleor/graphql/discount/mutations.py
index 0739cae235..c6c79f7f1b 100644
--- a/saleor/graphql/discount/mutations.py
+++ b/saleor/graphql/discount/mutations.py
@@ -12,7 +12,7 @@ from ...product.tasks import (
from ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
from ..core.scalars import PositiveDecimal
from ..core.types.common import DiscountError
-from ..core.validators import validate_price_amount
+from ..core.validators import validate_price_precision
from ..product.types import Category, Collection, Product
from .enums import DiscountValueTypeEnum, VoucherTypeEnum
from .types import Sale, Voucher
@@ -163,7 +163,7 @@ class VoucherCreate(ModelMutation):
min_spent_amount = cleaned_input.pop("min_amount_spent", None)
if min_spent_amount is not None:
try:
- validate_price_amount(min_spent_amount, instance.currency)
+ validate_price_precision(min_spent_amount, instance.currency)
except ValidationError as error:
error.code = DiscountErrorCode.INVALID.value
raise ValidationError({"min_spent_amount": error})
diff --git a/saleor/graphql/giftcard/mutations.py b/saleor/graphql/giftcard/mutations.py
index fbba4b5d3b..add65f8b8b 100644
--- a/saleor/graphql/giftcard/mutations.py
+++ b/saleor/graphql/giftcard/mutations.py
@@ -14,7 +14,7 @@ from ...giftcard.utils import activate_gift_card, deactivate_gift_card
from ..core.mutations import BaseMutation, ModelMutation
from ..core.scalars import PositiveDecimal
from ..core.types.common import GiftCardError
-from ..core.validators import validate_price_amount
+from ..core.validators import validate_price_precision
from .types import GiftCard
@@ -60,7 +60,7 @@ class GiftCardCreate(ModelMutation):
balance = cleaned_input.get("balance", None)
if balance:
try:
- validate_price_amount(balance, instance.currency)
+ validate_price_precision(balance, instance.currency)
except ValidationError as error:
error.code = GiftCardErrorCode.INVALID.value
raise ValidationError({"balance": error})
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 6decd124cb..57a2326473 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -41,7 +41,7 @@ from ...core.utils import (
validate_slug_and_generate_if_needed,
)
from ...core.utils.reordering import perform_reordering
-from ...core.validators import validate_price_amount
+from ...core.validators import validate_price_precision
from ...meta.deprecated.mutations import ClearMetaBaseMutation, UpdateMetaBaseMutation
from ...warehouse.types import Warehouse
from ..types import Category, Collection, Product, ProductImage, ProductVariant
@@ -833,7 +833,7 @@ class ProductCreate(ModelMutation):
base_price = cleaned_input.get("base_price")
try:
- validate_price_amount(base_price, instance.currency)
+ validate_price_precision(base_price, instance.currency)
except ValidationError as error:
error.code = ProductErrorCode.INVALID.value
raise ValidationError({"base_price": error})
@@ -1200,7 +1200,7 @@ class ProductVariantCreate(ModelMutation):
if "cost_price" in cleaned_input:
cost_price = cleaned_input.pop("cost_price")
try:
- validate_price_amount(cost_price, instance.currency)
+ validate_price_precision(cost_price, instance.currency)
except ValidationError as error:
error.code = ProductErrorCode.INVALID.value
raise ValidationError({"cost_price": error})
@@ -1219,7 +1219,7 @@ class ProductVariantCreate(ModelMutation):
if "price" in cleaned_input:
try:
- validate_price_amount(price, instance.currency)
+ validate_price_precision(price, instance.currency)
except ValidationError as error:
error.code = ProductErrorCode.INVALID.value
raise ValidationError({"price": error})
diff --git a/saleor/graphql/shipping/mutations.py b/saleor/graphql/shipping/mutations.py
index 257e4b7535..47c31568e2 100644
--- a/saleor/graphql/shipping/mutations.py
+++ b/saleor/graphql/shipping/mutations.py
@@ -13,7 +13,7 @@ from ..core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
from ..core.scalars import PositiveDecimal, WeightScalar
from ..core.types.common import ShippingError
from ..core.utils import get_duplicates_ids
-from ..core.validators import validate_price_amount
+from ..core.validators import validate_price_precision
from .enums import ShippingMethodTypeEnum
from .types import ShippingMethod, ShippingZone
@@ -172,7 +172,7 @@ class ShippingPriceMixin:
price_amount = cleaned_input.pop("price", None)
if price_amount is not None:
try:
- validate_price_amount(price_amount, instance.currency)
+ validate_price_precision(price_amount, instance.currency)
except ValidationError as error:
error.code = ShippingErrorCode.INVALID.value
raise ValidationError({"price": error})
@@ -186,7 +186,7 @@ class ShippingPriceMixin:
if min_price is not None:
try:
- validate_price_amount(min_price, instance.currency)
+ validate_price_precision(min_price, instance.currency)
except ValidationError as error:
error.code = ShippingErrorCode.INVALID.value
raise ValidationError({"minimum_order_price": error})
@@ -194,7 +194,7 @@ class ShippingPriceMixin:
if max_price is not None:
try:
- validate_price_amount(max_price, instance.currency)
+ validate_price_precision(max_price, instance.currency)
except ValidationError as error:
error.code = ShippingErrorCode.INVALID.value
raise ValidationError({"maximum_order_price": error})