Commit 5207bc71 authored by Felizia Tiburcio's avatar Felizia Tiburcio

Fix form validation for Place Bid

parent 251a4d81
a{% extends 'boodlesite\templates\base.html' %}
{% extends 'boodlesite\templates\base.html' %}
{% load static %}
{% block title %}Auction{% endblock %}
......@@ -21,7 +21,6 @@ a{% extends 'boodlesite\templates\base.html' %}
</div>
<!--product display -->
product display div
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
......@@ -55,7 +54,6 @@ a{% extends 'boodlesite\templates\base.html' %}
</div>
<div class="col-lg-5 col-md-6">
product info div
<div class="product-info" style="border: 2px dotted black;">
......@@ -63,6 +61,7 @@ a{% extends 'boodlesite\templates\base.html' %}
<h4>Product Specifications</h4>
<p>{{ item_specs }}</p>
<p>Starting at: {{item_floor_price}}</p>
</div>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#placeBidModal"
......@@ -83,7 +82,7 @@ a{% extends 'boodlesite\templates\base.html' %}
<div class="modal-body">
<form action="" method="POST">
<div class="form-group">
<h3>Current Highest Bid: PHP {{highest_bid.amount}}</h3>
<h3>Current Highest Bid: PHP {{highest_bid}}</h3>
{% csrf_token %}
{{ form }}
</div>
......@@ -114,7 +113,7 @@ a{% extends 'boodlesite\templates\base.html' %}
</li>
{% endfor %}
{% else %}
<h3> no bids placed yet.</h3>
<h3> No bids have been placed for this item yet.</h3>
{% endif %}
</ul>
......
......@@ -3,17 +3,28 @@ from django.forms import (ModelForm,
TextInput, Textarea, widgets)
from .models import *
from django.core.exceptions import ValidationError
class PlaceBidForm(forms.ModelForm):
class Meta:
model = AuctionBid
fields = ['amount']
fields = ['amount','auctionid']
widgets = {'auctionid': forms.HiddenInput()}
def clean(self):
super().clean()
form_amount = self.cleaned_data.get('amount')
auction = self.cleaned_data.get('auctionid')
def clean_amount(self):
form_amount = self.cleaned_data.get("amount")
prev_amt = AuctionBid.objects.latest("amount")
prev_amt_cleaned = prev_amt.amount
auction_item = Item.objects.get(auction=auction)
if prev_amt_cleaned > form_amount:
raise forms.ValidationError("Please Put Higher Bid")
auction_bids = AuctionBid.objects.filter(auctionid=auction)
return form_amount
if not auction_bids:
highest_bid = auction_item.floorprice
if form_amount < highest_bid:
raise ValidationError('Please enter an amount greater than or equal to the floorprice.')
else:
highest_bid = auction_bids.latest('bidtime').amount
if form_amount <= highest_bid:
raise ValidationError('Please enter an amount higher than the current highest bid.')
\ No newline at end of file
......@@ -40,20 +40,23 @@ def auction(request, pk):
# Auction bids
auction_bids = AuctionBid.objects.filter(auctionid=pk).order_by('-bidtime')
if not auction_bids:
highest_bid = auction_item.floorprice
else:
highest_bid = auction_bids[0]
form = PlaceBidForm()
if auction_bids:
highest_bid = auction_bids[0].amount
form = PlaceBidForm(initial={'auctionid':auction})
if request.method == 'POST':
form = PlaceBidForm(request.POST)
form = PlaceBidForm(request.POST,initial={'auctionid':auction})
if form.is_valid():
try:
amount = form.cleaned_data['amount']
if amount > highest_bid.amount:
new_bid = AuctionBid(amount=amount,bidtime=datetime.now(),auctionid=auction)
new_bid.save()
return redirect(f"/auction/{pk}")
except:
pass
context = {
'item_name':auction_item.itemname,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment