Skip to content

Commit

Permalink
chore: add top_bid field to both seller and buyer auction list views …
Browse files Browse the repository at this point in the history
…as well as dashboard list views
  • Loading branch information
sandronadiradze committed Nov 2, 2024
1 parent 40d4fd0 commit f7196b5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
1 change: 1 addition & 0 deletions auction/openapi/buyer_dashboard_list_openapi_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
17 changes: 17 additions & 0 deletions auction/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -48,6 +49,7 @@ class Meta:
"start_date",
"end_date",
"bookmarked",
"top_bid",
]

def get_tags(self, obj):
Expand All @@ -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)

Expand All @@ -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

Expand Down
34 changes: 26 additions & 8 deletions auction/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit f7196b5

Please sign in to comment.