diff --git a/src/tjts5901/templates/view.html b/src/tjts5901/templates/view.html index 5df612f052c189e31d7f2f774a632980d63fc577..820d6cd22f90b9c526c7c802f1cb3cd7e5c8b6bb 100644 --- a/src/tjts5901/templates/view.html +++ b/src/tjts5901/templates/view.html @@ -20,7 +20,7 @@ <p class="mb-1">Created : {{ item.created_at }}</p> <p class="mb-1">Current bid : {{ item.starting_bid }}</p><br/> <form method="POST" action="{{ url_for('views.bid', id=item.id)}}" class="row" enctype="multipart/form-data"> - <input class="col-md-9" id="bid" type="number" min="{{ item.starting_bid }}" placeholder="write your bid"> + <input class="col-md-9" id="bid" name ="bid" type="number" min="{{ item.starting_bid }}" placeholder="write your bid"> <button for="bid" type="submit" class="btn btn-primary col-md-3">Validate</button> </form> </div> diff --git a/src/tjts5901/views.py b/src/tjts5901/views.py index 5b744c4a7036d5990efb9ba5f082e44fe25eb6b3..2350c42c06d419ce9c41592b6ad084995918a039 100644 --- a/src/tjts5901/views.py +++ b/src/tjts5901/views.py @@ -172,42 +172,24 @@ def list_bid(): html = render_template("listBid.html", items=items) return html -@bp.route('/item/<id>') -def view(id): - """ - Item view page. - - Displays the item details, and a form to place a bid. - """ - - item = Item.objects.get_or_404(id=id) - # Set the minumum price for the bid form from the current winning bid - winning_bid = get_winning_bid(item) - min_bid = get_item_price(item) - - if item.closes_at < datetime.utcnow() and winning_bid.bidder == current_user: - flash(("Congratulations! You won the auction!")) - elif item.closes_at < datetime.utcnow() + timedelta(hours=1): - # Dark pattern to show enticing message to user - flash(("This item is closing soon! Act now! Now! Now!")) - - return render_template("item.html", item=item) - - -@bp.route("item/<id>/bid") +@bp.route("item/<id>/bid", methods=('POST',)) @login_required def bid(id): """ method that saves bid on object + + If the bid is valid, create a new bid and redirect to the item view page. + Otherwise, display an error message and redirect back to the item view page. + """ # frontend not implemented yet, therefore not active item = Item.objects.get_or_404(id=id) min_amount = item.starting_bid - amount = request.form['amount'] + amount = int(request.form.get('bid')) - if amount < min_amount: + if amount <= min_amount: flash(("Bid must be at least %(min_amount)s", min_amount)) return redirect(url_for('views.list_bid')) @@ -225,7 +207,7 @@ def bid(id): ) bid.save() except Exception as exc: - flash(_("Error placing bid: %(exc)s", exc=exc)) + flash(("Error placing bid: %(exc)s", exc=exc)) else: flash(("Bid placed successfully!"))