Skip to content

Commit

Permalink
chore: add permission to declare winner view so that if it has a winn…
Browse files Browse the repository at this point in the history
…er winner declaration is prohibited.
  • Loading branch information
sandronadiradze committed Nov 5, 2024
1 parent 76a0e45 commit d1a87fa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
15 changes: 15 additions & 0 deletions auction/permissions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from rest_framework.permissions import BasePermission

from auction.models.auction import AuctionStatistics


class IsOwner(BasePermission):
"""
Expand Down Expand Up @@ -71,3 +73,16 @@ def has_permission(self, request, view):

def has_object_permission(self, request, view, obj):
return str(obj.auction.author) == str(request.user.id)


class AlreadyHasAWinner(BasePermission):
def has_permission(self, request, view):
return True

def has_object_permission(self, request, view, obj):
auction_statistics = AuctionStatistics.objects.filter(auction=obj).first()

if auction_statistics:
return auction_statistics.winner_bid_object is not None

return False
9 changes: 8 additions & 1 deletion auction/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
seller_dashboard_list_openapi_examples,
)
from auction.permissions import (
AlreadyHasAWinner,
HasCountryInProfile,
IsAuctionOwner,
IsBookmarkOwner,
Expand Down Expand Up @@ -1095,7 +1096,13 @@ class DeclareWinnerView(generics.GenericAPIView):
and the bid status will be set to `APPROVED`.
"""

permission_classes = [IsAuthenticated, IsBuyer, IsAuctionOwner, HasCountryInProfile]
permission_classes = [
IsAuthenticated,
IsBuyer,
IsAuctionOwner,
HasCountryInProfile,
AlreadyHasAWinner,
]

def get_bid(self, auction_id, bid_id):
bid = get_object_or_404(
Expand Down

0 comments on commit d1a87fa

Please sign in to comment.