Skip to content

Commit

Permalink
chore: add permission that will prohibit non-premium users from uploa…
Browse files Browse the repository at this point in the history
…ding more than five images per bid
  • Loading branch information
sandronadiradze committed Nov 2, 2024
1 parent 799a258 commit 40d4fd0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
24 changes: 22 additions & 2 deletions bid/openapi/bid_create_openapi_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ def create_bid_examples():
status_codes=[400],
),
OpenApiExample(
"Error: No permission (GET)",
summary="No permission to perform the requested operation.",
"Error: No permission to create more than 5 bids. (GET)",
summary="No permission to create more than 5 bids.",
description="This example shows an error response when a user tries "
"to create new bid, but the user has already created five bids "
"and does not have a premium account.",
Expand All @@ -191,4 +191,24 @@ def create_bid_examples():
response_only=True,
status_codes=[403],
),
OpenApiExample(
"Error: No permission to upload more than 5 images for a bid. (GET)",
summary="No permission to upload more than 5 images for a bid.",
description="This example shows an error response when a user tries "
"to create new bid with more than 5 images. Only users "
"with premium accounts should be able to upload more than "
"five images.",
value={
"type": "validation_error",
"errors": [
{
"code": "invalid",
"message": "As a non-premium user you can not upload more than 5 images per bid.",
"field_name": "images",
}
],
},
response_only=True,
status_codes=[403],
),
]
17 changes: 17 additions & 0 deletions bid/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class OnlyFiveUniqueBidsPerUser(BasePermission):

def has_permission(self, request, view):
auction_id = view.kwargs.get("auction_id")
print(request.data)

if auction_id is None:
return False
Expand All @@ -53,3 +54,19 @@ def has_permission(self, request, view):
)

return True


class OnlyFiveImagesPerBid(BasePermission):
"""
Custom permission to only allow users to place five images per bid on an auction.
"""

def has_permission(self, request, view):
images = request.data.get("images")

if images and len(images) > 5:
raise PermissionDenied(
_("As a non-premium user you can not upload more than 5 images per bid.")
)

return True
4 changes: 4 additions & 0 deletions bid/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from bid.permissions import (
IsBidAuthorOrAuctionAuthor,
IsBidOwner,
OnlyFiveImagesPerBid,
OnlyFiveUniqueBidsPerUser,
)
from bid.serializers import (
Expand Down Expand Up @@ -62,6 +63,8 @@ class CreateBidView(generics.CreateAPIView):
- 201 (Created): Bid created successfully. The response body contains the serialized bid data.
- 400 (Bad Request): Invalid request data. The response body contains a list of validation errors.
- 403 (Forbidden): Non-premium user tries to create more than `five` bids, or tries to upload more
than `five` images per bid.
- 404 (Not Found): Auction not found.
**Examples:**
Expand All @@ -82,6 +85,7 @@ class CreateBidView(generics.CreateAPIView):
IsSeller,
HasCountryInProfile,
OnlyFiveUniqueBidsPerUser,
OnlyFiveImagesPerBid,
)

def get_auction(self):
Expand Down
23 changes: 15 additions & 8 deletions locale/ka/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-11-02 01:52+0000\n"
"POT-Creation-Date: 2024-11-02 02:19+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -91,13 +91,20 @@ msgstr ""
"წაიშალა {deleted} აუქციონი და {marked_deleted} აუქციონი მონიშნულია, როგორც "
"წაშლილი."

#: bid/permissions.py:45
#: bid/permissions.py:50
msgid ""
"As a non-premium user you can not place more than five unique bids on this "
"auction. But you can change the offer of a bid as many times as you want."
msgstr "მომხმარებლებს, რომლებსაც არ აქვთ პრემიუმ ტიპის ანგარიში, არ შეუძლიათ "
"ერთ აუქციონზე დადონ 5-ზე მეტი სხვადასხვა ბიდი, მაგრამ თქვენ შეგიძლიათ, რომ"
"უკვე არსებული ბიდის შეთავაზება განაახლოთ იმდენჯერ, რამდენჯერაც მოგესურვებათ."
msgstr ""
"მომხმარებლებს, რომლებსაც არ აქვთ პრემიუმ ტიპის ანგარიში, არ შეუძლიათ ერთ "
"აუქციონზე დადონ 5-ზე მეტი სხვადასხვა ბიდი, მაგრამ თქვენ შეგიძლიათ, რომუკვე "
"არსებული ბიდის შეთავაზება განაახლოთ იმდენჯერ, რამდენჯერაც მოგესურვებათ."

#: bid/permissions.py:69
msgid "As a non-premium user you can not upload more than 5 images per bid."
msgstr ""
"მომხმარებლებს, რომლებსაც არ აქვთ პრემიუმ ტიპის ანგარიში, შეუძლიათ, რომ თითო "
"ბიდისთვის ატვირთონ მხოლოდ ხუთი სურათი."

#: bid/serializers.py:255
msgid ""
Expand All @@ -108,16 +115,16 @@ msgstr ""
"მეტი მნიშვნელობის მითითება. ბიდის განახლებისას შესაძლებელია მხოლოდ ფასის "
"დაკლება."

#: bid/views.py:88
#: bid/views.py:102
msgid "You can not place bids on auctions with a status of `Draft`."
msgstr "თქვენ არ შეგიძლიათ ბიდის განთავსება 'Draft' სტატუსის მქონე აუქციონზე."

#: bid/views.py:96
#: bid/views.py:110
msgid "Auction has already been completed, you can no longer place bids."
msgstr ""
"აუქციონი უკვე დასრულდა, თქვენ ვეღარ შეძლებთ ამ აუქციონზე ბიდის განთავსებას."

#: bid/views.py:102
#: bid/views.py:116
msgid "Auction has not started yet, you can not place bid."
msgstr ""
"აუქციონი ჯერ არ დაწყებულა, თქვენ ვერ შეძლებთ ამ აუქციონზე ბიდის განთავსებას."

0 comments on commit 40d4fd0

Please sign in to comment.