From 19f8c59f376cfa9a48ceccea531dd0a52370288e Mon Sep 17 00:00:00 2001 From: Arno Wunderlich <arno.a.wunderlich@student.jyu.fi> Date: Thu, 16 Feb 2023 12:37:32 +0000 Subject: [PATCH] bidding backend frontend integration --- src/tjts5901/templates/view.html | 2 +- src/tjts5901/views.py | 34 ++++++++------------------------ 2 files changed, 9 insertions(+), 27 deletions(-) diff --git a/src/tjts5901/templates/view.html b/src/tjts5901/templates/view.html index 5df612f0..820d6cd2 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 5b744c4a..2350c42c 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!")) -- GitLab