Ejemplo n.º 1
0
def bid(request, id):
    if request.method == "POST"\
                and len(Auction.objects.filter(id=id)) > 0\
                and request.user is not Auction.objects.get(id=id).seller\
                and not Auction.objects.get(id=id).resolved\
                and not Auction.objects.get(id=id).banned:
        getcontext().prec = 2
        auction = Auction.objects.get(id=id)
        bid = Bid()
        bid.auction = auction
        bid.bidder = request.user
        price = Decimal(request.POST["price"])
        bids = Bid.objects.filter(auction_id=auction).order_by("price")
        if len(bids) > 0 and bids.last().bidder.id is request.user.id:
            messages.add_message(request, messages.ERROR,
                                 "You already have the highest bid!")
            if request.POST.get("next") is not None:
                return redirect(request.POST.get("next"))
            else:
                return HttpResponseRedirect('/auction/' + id + '/')

        if len(bids) > 0 and price > bids.last().price and price.as_tuple(
        ).exponent == -2:
            bid.price = price
        elif price >= auction.priceMin and price.as_tuple().exponent == -2:
            bid.price = price
        else:
            messages.add_message(
                request, messages.ERROR,
                "The bid must exceed the minimum price or the highest bid, whichever is higher, "
                "by at least 0.01 (always use 2 decimals).")
            if request.POST.get("next") is not None:
                return redirect(request.POST.get("next"))
            else:
                return HttpResponseRedirect('/auction/' + id + '/')
        bid.time = datetime.now()
        if auction.a_hash != Auction.objects.get(id=id).a_hash:
            messages.add_message(
                request, messages.ERROR,
                "The auction has either been edited or a new bid has been made since last time the "
                "page was loaded. Please try bidding again.")
            return redirect('/auction/' + id + '/')
        bid.save()
        auction.a_hash = hash(auction.name + auction.description +
                              str(auction.due) + str(
                                  Bid.objects.filter(auction_id=id).order_by(
                                      "price").first().price) + salt)
        auction.save()
        # auction.new_bid_notify()
        update_session_stats(request, "bid")
        messages.add_message(request, messages.INFO, "Bid created")
        return redirect(request.POST.get("next"))