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:
parent
6955e6bec6
commit
31d9ccb82a
4 changed files with 17 additions and 16 deletions
|
|
@ -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(
|
||||
return self.products.filter(is_active=True, stocks__is_active=True).aggregate(
|
||||
Min("stocks__price")
|
||||
)["stocks__price__min"]
|
||||
or Decimal("0.00")
|
||||
)
|
||||
)["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(
|
||||
return self.products.filter(is_active=True, stocks__is_active=True).aggregate(
|
||||
Max("stocks__price")
|
||||
)["stocks__price__max"]
|
||||
or Decimal("0.00")
|
||||
)
|
||||
)["stocks__price__max"] or Decimal("0.00")
|
||||
|
||||
@cached_property
|
||||
def seo_description(self) -> str:
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -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"):
|
||||
|
|
|
|||
Loading…
Reference in a new issue