diff --git a/auction/openapi/buyer_dashboard_list_openapi_examples.py b/auction/openapi/buyer_dashboard_list_openapi_examples.py index 7ab968c..323a4bb 100644 --- a/auction/openapi/buyer_dashboard_list_openapi_examples.py +++ b/auction/openapi/buyer_dashboard_list_openapi_examples.py @@ -31,6 +31,7 @@ def examples(): "quantity": 1000, "start_date": "2024-10-08T12:34:56.789012Z", "end_date": "2024-10-08T12:34:57.789012Z", + "top_bid": "₾2900.00", }, response_only=True, status_codes=[200], diff --git a/auction/serializers.py b/auction/serializers.py index b5f2a92..c193a77 100644 --- a/auction/serializers.py +++ b/auction/serializers.py @@ -29,6 +29,7 @@ class AuctionListSerializer(serializers.ModelSerializer): product = serializers.CharField(source="auction_name") tags = serializers.SerializerMethodField() bookmarked = serializers.BooleanField(default=False) + top_bid = serializers.SerializerMethodField() class Meta: model = Auction @@ -48,6 +49,7 @@ class Meta: "start_date", "end_date", "bookmarked", + "top_bid", ] def get_tags(self, obj): @@ -62,6 +64,17 @@ def get_category(self, obj): category = obj.category return category.name if category else None + def get_top_bid(self, obj): + """ + Returns the formatted top bid if it exists, otherwise returns None + """ + try: + if hasattr(obj, "statistics") and obj.statistics.top_bid: + return obj.statistics.top_bid + return None + except AuctionStatistics.DoesNotExist: + return None + def to_representation(self, instance): representation = super().to_representation(instance) @@ -73,7 +86,11 @@ def to_representation(self, instance): # Attach currency symbol to max_price field currency = representation["currency"] max_price = representation["max_price"] + top_bid = representation["top_bid"] representation["max_price"] = f"{get_currency_symbol(currency)}{max_price}" + representation["top_bid"] = ( + f"{get_currency_symbol(currency)}{top_bid}" if top_bid is not None else None + ) return representation diff --git a/auction/views.py b/auction/views.py index a9361d1..e38f67b 100755 --- a/auction/views.py +++ b/auction/views.py @@ -131,7 +131,11 @@ class BuyerAuctionListView(ListAPIView): def get_queryset(self): user = self.request.user.id - queryset = Auction.objects.filter(author=user) + queryset = ( + Auction.objects.select_related("statistics", "category").prefetch_related( + "tags" + ) + ).filter(author=user) # Override ordering if 'category' is in the query params ordering = self.request.query_params.get("ordering", None) @@ -176,8 +180,12 @@ class BuyerDashboardListView(ListAPIView): serializer_class = AuctionListSerializer def get_queryset(self): - queryset = Auction.objects.filter( - status="Live", start_date__lte=timezone.now(), author=self.request.user.id + queryset = ( + Auction.objects.select_related("statistics", "category") + .prefetch_related("tags") + .filter( + status="Live", start_date__lte=timezone.now(), author=self.request.user.id + ) ) return queryset @@ -263,10 +271,17 @@ class SellerAuctionListView(ListAPIView): def get_queryset(self): status = self.request.query_params.get("status") - queryset = ( - Auction.objects.all() if status else Auction.objects.filter(status="Live") + base_queryset = ( + Auction.objects.select_related("statistics", "category") + .prefetch_related("tags") + .exclude(status=StatusChoices.DRAFT) ) + if status == "Upcoming": + queryset = base_queryset.filter(start_date__gt=timezone.now()) + else: + queryset = base_queryset.filter(status="Live") + ordering = self.request.query_params.get("ordering", None) bookmarked_subquery = Bookmark.objects.filter( @@ -320,9 +335,12 @@ def get_queryset(self): user = self.request.user # Get auctions where the user has placed bids - queryset = Auction.objects.filter( - bids__author=user.id, start_date__lte=timezone.now() - ).distinct() + queryset = ( + Auction.objects.select_related("statistics", "category") + .prefetch_related("tags") + .filter(bids__author=user.id, start_date__lte=timezone.now()) + .distinct() + ) # Add bookmarked status bookmarked_subquery = Bookmark.objects.filter(