Skip to content

Commit

Permalink
feat: You can't place bids in your own products
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasFASouza committed Feb 1, 2023
1 parent 3f6417d commit 27e7b40
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 7 additions & 1 deletion auctions/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ class Meta:

class NewBid(forms.ModelForm):
def __init__(self, *args, **kwargs):
print(kwargs)
self.item = kwargs.pop('item')

if kwargs['buyer']:
self.buyer = kwargs.pop('buyer')

super(NewBid, self).__init__(*args, **kwargs)

def clean(self):
value = self.cleaned_data['value']
if value <= self.item.price:
raise forms.ValidationError("Your bid is lower than current price")

if self.buyer == self.item.seller:
raise forms.ValidationError("You can't place bids in your own products")

return self.cleaned_data

class Meta:
Expand Down
5 changes: 3 additions & 2 deletions auctions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ def add_listing(request):
def place_bid(request, listing_id):
if request.method == "POST":
item = Listing.objects.get(id=listing_id)
form = forms.NewBid(request.POST, item=item)
buyer = request.user
form = forms.NewBid(request.POST, item=item, buyer=buyer)

if form.is_valid():
obj = form.save(commit=False)
obj.buyer = request.user
obj.buyer = buyer
obj.item = item
obj.save()

Expand Down

0 comments on commit 27e7b40

Please sign in to comment.