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.
This commit is contained in:
Egor Pavlovich Gorbunov 2026-03-05 11:03:45 +03:00
parent 6955e6bec6
commit 31d9ccb82a
4 changed files with 17 additions and 16 deletions

View file

@ -470,21 +470,15 @@ class Category(NiceModel, MPTTModel):
@cached_property @cached_property
def min_price(self) -> Decimal: def min_price(self) -> Decimal:
return ( return self.products.filter(is_active=True, stocks__is_active=True).aggregate(
self.products.filter(is_active=True, stocks__is_active=True).aggregate(
Min("stocks__price") Min("stocks__price")
)["stocks__price__min"] )["stocks__price__min"] or Decimal("0.00")
or Decimal("0.00")
)
@cached_property @cached_property
def max_price(self) -> Decimal: def max_price(self) -> Decimal:
return ( return self.products.filter(is_active=True, stocks__is_active=True).aggregate(
self.products.filter(is_active=True, stocks__is_active=True).aggregate(
Max("stocks__price") Max("stocks__price")
)["stocks__price__max"] )["stocks__price__max"] or Decimal("0.00")
or Decimal("0.00")
)
@cached_property @cached_property
def seo_description(self) -> str: def seo_description(self) -> str:

View file

@ -112,7 +112,6 @@ class CategoryDetailSerializer(ModelSerializer):
return [] return []
class BrandDetailSerializer(ModelSerializer): class BrandDetailSerializer(ModelSerializer):
categories = CategorySimpleSerializer(many=True) categories = CategorySimpleSerializer(many=True)
description = SerializerMethodField() description = SerializerMethodField()
@ -286,7 +285,10 @@ class ProductDetailSerializer(ModelSerializer):
quantity = SerializerMethodField() quantity = SerializerMethodField()
feedbacks_count = SerializerMethodField() feedbacks_count = SerializerMethodField()
discount_price = DecimalField( 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, coerce_to_string=False,
) )
personal_orders_only = SerializerMethodField() personal_orders_only = SerializerMethodField()

View file

@ -147,7 +147,10 @@ class ProductSimpleSerializer(ModelSerializer):
feedbacks_count = SerializerMethodField() feedbacks_count = SerializerMethodField()
personal_orders_only = SerializerMethodField() personal_orders_only = SerializerMethodField()
discount_price = DecimalField( 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, coerce_to_string=False,
) )

View file

@ -144,7 +144,9 @@ class CustomGraphQLView(FileUploadGraphQLView):
def _default(obj): def _default(obj):
if isinstance(obj, Decimal): if isinstance(obj, Decimal):
return float(obj) 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 opts = orjson.OPT_NON_STR_KEYS
if pretty or request.GET.get("pretty"): if pretty or request.GET.get("pretty"):