Features: 1) Update product property to reflect the highest price from stock instead of the lowest; 2) Update filter option to use price_order for consistent sorting;

Fixes: 1) Fix incorrect queryset filtering logic in the `feedbacks` endpoint; 2) Replace redundant product lookup in `feedbacks` with `get_object` for improved clarity and performance;

Extra: 1) Rename annotated field `price` to `price_order` in queryset filters for better semantics; 2) Cleanup and streamline `feedbacks` endpoint logic.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-08-20 06:45:46 +03:00
parent a2f9ae8693
commit 016b9a575a
3 changed files with 8 additions and 16 deletions

View file

@ -87,7 +87,7 @@ class ProductFilter(FilterSet):
("slug", "slug"),
("created", "created"),
("modified", "modified"),
("price", "price"),
("price_order", "price"),
("?", "random"),
),
initial="uuid",
@ -134,7 +134,7 @@ class ProductFilter(FilterSet):
)
if "price" in order_fields:
self.queryset = self.queryset.annotate(
price=Coalesce(
price_order=Coalesce(
Max("stocks__price"),
Value(0.0),
output_field=FloatField(),

View file

@ -609,7 +609,7 @@ class Product(ExportModelOperationsMixin("product"), NiceModel): # type: ignore
Properties:
rating (float): The average rating of the product, rounded to 2 decimal places.
feedbacks_count (int): The total number of feedback entries associated with the product.
price (float): The lowest price of the product based on its stock, rounded to 2 decimal
price (float): The highest price of the product based on its stock, rounded to 2 decimal
places.
quantity (int): The total available quantity of the product across all its stocks.
total_orders (int): Counts the total orders made for the product in relevant statuses.

View file

@ -571,19 +571,11 @@ class ProductViewSet(EvibesViewSet):
@action(detail=True, methods=["get"], url_path="feedbacks")
def feedbacks(self, request, **kwargs):
lookup_val = kwargs.get(self.lookup_field)
try:
product = Product.objects.get(uuid=lookup_val)
feedbacks = (
Feedback.objects.filter(order_product__product=product)
if request.user.has_perm("core.view_feedback")
else Feedback.objects.filter(order_product__product=product, is_active=True)
)
# noinspection PyTypeChecker
return Response(data=FeedbackDetailSerializer(feedbacks, many=True).data)
except Product.DoesNotExist:
name = "Product"
return Response(status=status.HTTP_404_NOT_FOUND, data={"detail": _(f"{name} does not exist: {uuid}")})
product = self.get_object()
qs = Feedback.objects.filter(order_product__product=product)
if not request.user.has_perm("core.view_feedback"):
qs = qs.filter(is_active=True)
return Response(data=FeedbackDetailSerializer(qs, many=True).data)
@action(
detail=True,