From 31d9ccb82a9111756360deabfa4aeb76a8e6ff8a Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Thu, 5 Mar 2026 11:03:45 +0300 Subject: [PATCH] refactor(core): simplify code for price aggregation and formatting Improve readability by removing unnecessary parentheses in `min_price` and `max_price` methods in `models.py`. Enhance JSON serialization error formatting in `views.py` and adjustment of field formatting in serializers. No functional changes, purely stylistic updates for code maintainability. --- engine/core/models.py | 18 ++++++------------ engine/core/serializers/detail.py | 6 ++++-- engine/core/serializers/simple.py | 5 ++++- engine/core/views.py | 4 +++- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/engine/core/models.py b/engine/core/models.py index 7cba3f71..06f15426 100644 --- a/engine/core/models.py +++ b/engine/core/models.py @@ -470,21 +470,15 @@ class Category(NiceModel, MPTTModel): @cached_property def min_price(self) -> Decimal: - return ( - self.products.filter(is_active=True, stocks__is_active=True).aggregate( - Min("stocks__price") - )["stocks__price__min"] - or Decimal("0.00") - ) + return self.products.filter(is_active=True, stocks__is_active=True).aggregate( + Min("stocks__price") + )["stocks__price__min"] or Decimal("0.00") @cached_property def max_price(self) -> Decimal: - return ( - self.products.filter(is_active=True, stocks__is_active=True).aggregate( - Max("stocks__price") - )["stocks__price__max"] - or Decimal("0.00") - ) + return self.products.filter(is_active=True, stocks__is_active=True).aggregate( + Max("stocks__price") + )["stocks__price__max"] or Decimal("0.00") @cached_property def seo_description(self) -> str: diff --git a/engine/core/serializers/detail.py b/engine/core/serializers/detail.py index 1f229fe8..34f73440 100644 --- a/engine/core/serializers/detail.py +++ b/engine/core/serializers/detail.py @@ -112,7 +112,6 @@ class CategoryDetailSerializer(ModelSerializer): return [] - class BrandDetailSerializer(ModelSerializer): categories = CategorySimpleSerializer(many=True) description = SerializerMethodField() @@ -286,7 +285,10 @@ class ProductDetailSerializer(ModelSerializer): quantity = SerializerMethodField() feedbacks_count = SerializerMethodField() discount_price = DecimalField( - max_digits=12, decimal_places=2, read_only=True, allow_null=True, + max_digits=12, + decimal_places=2, + read_only=True, + allow_null=True, coerce_to_string=False, ) personal_orders_only = SerializerMethodField() diff --git a/engine/core/serializers/simple.py b/engine/core/serializers/simple.py index 945b4717..81ae61d6 100644 --- a/engine/core/serializers/simple.py +++ b/engine/core/serializers/simple.py @@ -147,7 +147,10 @@ class ProductSimpleSerializer(ModelSerializer): feedbacks_count = SerializerMethodField() personal_orders_only = SerializerMethodField() discount_price = DecimalField( - max_digits=12, decimal_places=2, read_only=True, allow_null=True, + max_digits=12, + decimal_places=2, + read_only=True, + allow_null=True, coerce_to_string=False, ) diff --git a/engine/core/views.py b/engine/core/views.py index 6eba249b..85782849 100644 --- a/engine/core/views.py +++ b/engine/core/views.py @@ -144,7 +144,9 @@ class CustomGraphQLView(FileUploadGraphQLView): def _default(obj): if isinstance(obj, Decimal): return float(obj) - raise TypeError(f"Object of type {type(obj).__name__} is not JSON serializable") + raise TypeError( + f"Object of type {type(obj).__name__} is not JSON serializable" + ) opts = orjson.OPT_NON_STR_KEYS if pretty or request.GET.get("pretty"):