def bid(request, item_id): auction = Auction.objects.get(pk=item_id) if auction.version != request.session["version"]: return HttpResponse( "Someone else is currently bidding on this item, try again later.") if auction.status == "active": if request.method == "POST": if request.user.is_authenticated: bid_form = BidForm(request.POST) if bid_form.is_valid(): new_bid = Bid() new_bid.price = bid_form.cleaned_data['new_price'] new_bid.auction_id = item_id new_bid.bidder = request.user if (float(auction.minimum_price) + 0.01) > float( new_bid.price) or (float(auction.highbid) + 0.01) > float(new_bid.price): msg = _( "Bid must be at least 0.01 higher than minimum price or previous bid." ) messages.add_message(request, messages.ERROR, msg) #print("Bid has to be at least 0.01 higher than minimum price or previous bid.") return HttpResponseRedirect(reverse('index')) previous_bidder = auction.highbidder auction.highbid = new_bid.price auction.highbidder = request.user.username new_bid.save() auction.save() # Check for previous bidder and notify via email if previous_bidder != "None": previous_bidder_email = User.objects.get( username=previous_bidder) send_mail( "Outbid", "Your have been outbid on the following item: " + auction.title, "*****@*****.**", [previous_bidder_email.email], fail_silently=False) # Notify new bidder send_mail("New bid", "Your bid on " + auction.title + " has been recieved", "*****@*****.**", [request.user.email], fail_silently=False) # Notify the seller send_mail("New bid", "A new bid " + str(new_bid.price) + " has been placed on you auction: " + auction.title, "*****@*****.**", [auction.seller.email], fail_silently=False) print("New bid recieved on ", auction.title, " with the amount of ", new_bid.price) return HttpResponseRedirect(reverse('index')) else: print("Incorrect bid given") return render(request, "bid.html", {"bid_form": bid_form}) else: print("Unauthenticated user tried to place a bid") return HttpResponseRedirect(reverse('signin')) else: if float(auction.highbid) == 0: price = auction.minimum_price auction.highbid = float(price) auction.save() else: price = float(auction.minimum_price) bid_form = BidForm() return render(request, "bid.html", { "bid_form": bid_form, "auction": auction, "price": price }) else: # Auction is not active, return to home page return HttpResponseRedirect(reverse('index'))