Skip to content

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

Return types weights in default weight unit

IKarbowiakfeb8da6d7361
diff --git a/saleor/graphql/core/scalars.py b/saleor/graphql/core/scalars.py
index fa0420c261..6fd4c338d2 100644
--- a/saleor/graphql/core/scalars.py
+++ b/saleor/graphql/core/scalars.py
@@ -5,7 +5,7 @@ from graphql.error import GraphQLError
from graphql.language import ast
from measurement.measures import Weight
-from ...core.weight import convert_weight, get_default_weight_unit
+from ...core.weight import covert_weight_to_default_weight_unit, get_default_weight_unit
class Decimal(graphene.Float):
@@ -48,9 +48,7 @@ class WeightScalar(graphene.Scalar):
@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)
+ weight = covert_weight_to_default_weight_unit(weight)
return str(weight)
return None
diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.py
index 70544cb082..07e66affe9 100644
--- a/saleor/graphql/product/tests/test_product.py
+++ b/saleor/graphql/product/tests/test_product.py
@@ -29,7 +29,7 @@ from ....product.tasks import update_variants_names
from ....product.tests.utils import create_image, create_pdf_file_with_image_ext
from ....product.utils.attributes import associate_attribute_values_to_instance
from ....warehouse.models import Allocation, Stock, Warehouse
-from ...core.enums import ReportingPeriod, WeightUnitsEnum
+from ...core.enums import ReportingPeriod
from ...tests.utils import (
assert_no_permission,
get_graphql_content,
@@ -4279,26 +4279,26 @@ mutation createProduct(
@pytest.mark.parametrize(
- "weight, expected_weight_value, expected_weight_unit",
+ "weight, expected_weight_value",
(
- ("0", 0, WeightUnitsEnum.KG.name),
- (0, 0, WeightUnitsEnum.KG.name),
- (11.11, 11.11, WeightUnitsEnum.KG.name),
- (11, 11.0, WeightUnitsEnum.KG.name),
- ("11.11", 11.11, WeightUnitsEnum.KG.name),
- ({"value": 11.11, "unit": "kg"}, 11.11, WeightUnitsEnum.KG.name,),
- ({"value": 11, "unit": "g"}, 11.0, WeightUnitsEnum.G.name,),
- ({"value": "11.11", "unit": "ounce"}, 11.11, WeightUnitsEnum.OZ.name,),
+ ("0", 0),
+ (0, 0),
+ (11.11, 11.11),
+ (11, 11.0),
+ ("11.11", 11.11),
+ ({"value": 11.11, "unit": "kg"}, 11.11),
+ ({"value": 11, "unit": "g"}, 0.011),
+ ({"value": "1", "unit": "ounce"}, 0.028),
),
)
def test_create_product_with_weight_variable(
weight,
expected_weight_value,
- expected_weight_unit,
staff_api_client,
category,
permission_manage_products,
product_type_without_variant,
+ site_settings,
):
category_id = graphene.Node.to_global_id("Category", category.pk)
product_type_id = graphene.Node.to_global_id(
@@ -4320,30 +4320,30 @@ def test_create_product_with_weight_variable(
content = get_graphql_content(response)
result_weight = content["data"]["productCreate"]["product"]["weight"]
assert result_weight["value"] == expected_weight_value
- assert result_weight["unit"] == expected_weight_unit
+ assert result_weight["unit"] == site_settings.default_weight_unit.upper()
@pytest.mark.parametrize(
- "weight, expected_weight_value, expected_weight_unit",
+ "weight, expected_weight_value",
(
- ("0", 0, WeightUnitsEnum.KG.name),
- (0, 0, WeightUnitsEnum.KG.name),
- ("11.11", 11.11, WeightUnitsEnum.KG.name),
- ("11", 11.0, WeightUnitsEnum.KG.name),
- ('"11.11"', 11.11, WeightUnitsEnum.KG.name),
- ('{value: 11.11, unit: "kg"}', 11.11, WeightUnitsEnum.KG.name,),
- ('{value: 11, unit: "g"}', 11.0, WeightUnitsEnum.G.name,),
- ('{value: "11.11", unit: "ounce"}', 11.11, WeightUnitsEnum.OZ.name,),
+ ("0", 0),
+ (0, 0),
+ ("11.11", 11.11),
+ ("11", 11.0),
+ ('"11.11"', 11.11),
+ ('{value: 11.11, unit: "kg"}', 11.11),
+ ('{value: 11, unit: "g"}', 0.011),
+ ('{value: "1", unit: "ounce"}', 0.028),
),
)
def test_create_product_with_weight_input(
weight,
expected_weight_value,
- expected_weight_unit,
staff_api_client,
category,
permission_manage_products,
product_type_without_variant,
+ site_settings,
):
# Because we use Scalars for Weight this test query tests only a scenario when
# weight value is passed by directly in input
@@ -4397,4 +4397,4 @@ def test_create_product_with_weight_input(
content = get_graphql_content(response)
result_weight = content["data"]["productCreate"]["product"]["weight"]
assert result_weight["value"] == expected_weight_value
- assert result_weight["unit"] == expected_weight_unit
+ assert result_weight["unit"] == site_settings.default_weight_unit.upper()
diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.py
index 430c12e3ba..05db0221ea 100644
--- a/saleor/graphql/product/types/products.py
+++ b/saleor/graphql/product/types/products.py
@@ -8,6 +8,7 @@ from graphene_federation import key
from graphql.error import GraphQLError
from ....core.permissions import ProductPermissions
+from ....core.weight import covert_weight_to_default_weight_unit
from ....product import models
from ....product.templatetags.product_images import (
get_product_image_thumbnail,
@@ -423,6 +424,10 @@ class ProductVariant(CountableDjangoObjectType):
def __resolve_reference(root, _info, **_kwargs):
return graphene.Node.get_node_from_global_id(_info, root.id)
+ @staticmethod
+ def resolve_weight(root: models.ProductVariant, _info, **_kwargs):
+ return covert_weight_to_default_weight_unit(root.weight)
+
@key(fields="id")
class Product(CountableDjangoObjectType):
@@ -621,6 +626,10 @@ class Product(CountableDjangoObjectType):
def __resolve_reference(root, _info, **_kwargs):
return graphene.Node.get_node_from_global_id(_info, root.id)
+ @staticmethod
+ def resolve_weight(root: models.Product, _info, **_kwargs):
+ return covert_weight_to_default_weight_unit(root.weight)
+
@key(fields="id")
class ProductType(CountableDjangoObjectType):
@@ -702,6 +711,10 @@ class ProductType(CountableDjangoObjectType):
def __resolve_reference(root, _info, **_kwargs):
return graphene.Node.get_node_from_global_id(_info, root.id)
+ @staticmethod
+ def resolve_weight(root: models.ProductType, _info, **_kwargs):
+ return covert_weight_to_default_weight_unit(root.weight)
+
@key(fields="id")
class Collection(CountableDjangoObjectType):
diff --git a/saleor/graphql/shipping/types.py b/saleor/graphql/shipping/types.py
index 3de50472aa..04a8037309 100644
--- a/saleor/graphql/shipping/types.py
+++ b/saleor/graphql/shipping/types.py
@@ -1,6 +1,7 @@
import graphene
from graphene import relay
+from ...core.weight import covert_weight_to_default_weight_unit
from ...shipping import models
from ..core.connection import CountableDjangoObjectType
from ..core.types import CountryDisplay, MoneyRange
@@ -33,6 +34,12 @@ class ShippingMethod(CountableDjangoObjectType):
"price",
]
+ def resolve_maximum_order_weight(root: models.ShippingMethod, *_args):
+ return covert_weight_to_default_weight_unit(root.maximum_order_weight)
+
+ def resolve_minimum_order_weight(root: models.ShippingMethod, *_args):
+ return covert_weight_to_default_weight_unit(root.minimum_order_weight)
+
class ShippingZone(CountableDjangoObjectType):
price_range = graphene.Field(