From 895060be4205f43cfd6043db012c12557f396f92 Mon Sep 17 00:00:00 2001 From: Ekaterina Vititneva Date: Sat, 10 Aug 2024 11:03:44 +0200 Subject: [PATCH] Improved handling of different post requests from listing --- .../auctions/templates/auctions/listing.html | 2 +- project_2/commerce/auctions/urls.py | 1 - project_2/commerce/auctions/views.py | 95 +++++++------------ 3 files changed, 37 insertions(+), 61 deletions(-) diff --git a/project_2/commerce/auctions/templates/auctions/listing.html b/project_2/commerce/auctions/templates/auctions/listing.html index 955dce3..4bce7ae 100644 --- a/project_2/commerce/auctions/templates/auctions/listing.html +++ b/project_2/commerce/auctions/templates/auctions/listing.html @@ -19,7 +19,7 @@

Listing: {{ listing.title }}

{{ listing.bid }}€

{% if user.is_authenticated %} -
+
{% csrf_token %} {{ form.as_p }} diff --git a/project_2/commerce/auctions/urls.py b/project_2/commerce/auctions/urls.py index ee9d39e..35eac95 100644 --- a/project_2/commerce/auctions/urls.py +++ b/project_2/commerce/auctions/urls.py @@ -9,7 +9,6 @@ path("register", views.register, name="register"), path("create_listing", views.create_listing, name="create_listing"), path("listing/", views.listing, name="listing"), - path("listing//bid/", views.place_bid, name="place_bid"), path("categories", views.categories, name="categories"), path("watchlist", views.watchlist, name="watchlist"), ] diff --git a/project_2/commerce/auctions/views.py b/project_2/commerce/auctions/views.py index da6e6ed..8ed2a28 100644 --- a/project_2/commerce/auctions/views.py +++ b/project_2/commerce/auctions/views.py @@ -87,77 +87,54 @@ def create_listing(request): return render(request, "auctions/create_listing.html", {"form": form}) -def listing(request, title): - listing = Listing.objects.get(title=title) - return render(request, "auctions/listing.html", { - "listing": listing - }) - def listing(request, title): listing = get_object_or_404(Listing, title=title) user = request.user if request.method == "POST": - if 'add_to_watchlist' in request.POST: - if listing not in user.watchlist.all(): - user.watchlist.add(listing) - messages.success(request, "Listing added to your watchlist.") + if 'add_to_watchlist' in request.POST or 'remove_from_watchlist' in request.POST: + # Handle watchlist modification + if 'add_to_watchlist' in request.POST: + if listing not in user.watchlist.all(): + user.watchlist.add(listing) + elif 'remove_from_watchlist' in request.POST: + if listing in user.watchlist.all(): + user.watchlist.remove(listing) + return redirect('listing', title=listing.title) + + elif 'bid' in request.POST: + # Handle bid submission + form = BidForm(request.POST) + if form.is_valid(): + bid_amount = form.cleaned_data['bid'] + if bid_amount > listing.bid: + listing.bid = bid_amount + listing.last_modified_by = request.user + listing.save() + + new_bid = Bid( + price=bid_amount, + bidder=request.user, + listing=listing, + ) + new_bid.save() + + messages.success(request, "Your bid was placed successfully!") + return redirect('listing', title=listing.title) + else: + messages.error(request, "Your bid must be higher than the current bid.", extra_tags='danger') else: - messages.info(request, "Listing is already in your watchlist.") - elif 'remove_from_watchlist' in request.POST: - if listing in user.watchlist.all(): - user.watchlist.remove(listing) - messages.success(request, "Listing removed from your watchlist.") - else: - messages.info(request, "Listing was not in your watchlist.") - - return render(request, "auctions/listing.html", { - "listing": listing, - "form": BidForm(), # Assuming you're passing a BidForm as well - }) - -def place_bid(request, title): - listing = get_object_or_404(Listing, title=title) + messages.error(request, "Please enter a valid bid.", extra_tags='danger') - if request.method == "POST": - form = BidForm(request.POST) - if form.is_valid(): - bid_amount = form.cleaned_data['bid'] - - if bid_amount > listing.bid: - listing.bid = bid_amount - listing.last_modifed_by = request.user - listing.save() - - new_bid = Bid( - price=bid_amount, - bidder=request.user, - listing=listing, - ) - new_bid.save() - - messages.success(request, "Your bid was placed successfully!") - return redirect('listing', title=listing.title) - else: - messages.error(request, "Your bid must be higher than the current bid.", extra_tags='danger') - else: - messages.error(request, "Please enter a valid bid.", extra_tags='danger') else: form = BidForm() - return render(request, "auctions/listing.html", {"listing": listing, "form": form}) - -def watchlist_add(request, listing_id): - listing = get_object_or_404(Listing, id=listing_id) - user = request.user + return render(request, "auctions/listing.html", { + "listing": listing, + "form": form, + }) - if listing not in user.watchlist.all(): - user.watchlist.add(listing) - messages.success(request, "Listing added to your watchlist.") - else: - messages.info(request, "Listing is already in your watchlist.") - return redirect('listing', title=listing.title) def categories(request): return render(request, "auctions/categories.html", {