Esempio n. 1
0
def bid(request, auction_id):
    """ Bid on auction. Bid must be higher than current bid """

    auction = get_object_or_404(Auction, id=auction_id)
    bid = Bid.objects.filter(auction=auction_id).order_by('-bid_time')
    current_bid = bid[0].bid_amount
    current_user = auth.get_user(request)

    if request.method == 'POST':
        bid_form = BidForm(request.POST)

    if bid_form.is_valid():
        """ If form is valid create the bid only if higher then 
        the current bid and the auction has started. """
        bid = Bid()
        if current_bid < bid_form.cleaned_data['bid_amount'] and \
            auction.time_starting < timezone.now():
            bid.user = current_user
            bid.auction = auction
            bid.bid_time = datetime.now()
            bid.bid_amount = bid_form.cleaned_data['bid_amount']
            bid.save()
            auction.number_of_bids += 1
            auction.save()
            messages.success(request, 'Bid successfully placed. Good Luck!')
        else:
            messages.error(request, 'Bid must be higher than current bid')
            return redirect('auction', auction_id=auction_id)
    else:
        bid_form = BidForm()

    return redirect('auction', auction_id=auction_id)
Esempio n. 2
0
def auction(request, auction_id):
    """ Render the auction """

    auction = get_object_or_404(Auction, id=auction_id)
    bid = Bid.objects.filter(auction=auction_id).order_by('-bid_time')
    bid_form = BidForm()

    if auction:
        if auction.time_ending > timezone.now():   
            if bid:                                 
                latest_bid = bid[0]                 
            else:
                bid_default = Bid()                                 # If bid[0] False create first bid for auction
                bid_default.user = get_object_or_404(User, id=1)    # Set default user to user.id=1
                bid_default.auction = auction
                bid_default.bid_time = auction.time_starting
                bid_default.bid_amount = 0.00
                bid_default.save()
                latest_bid = bid_default

            if auction.time_starting < timezone.now():  # If the auction has not started yet
                context = {                             # hide the bid form.
                    'auction': auction,
                    'latest_bid': latest_bid,
                    'bid_form': bid_form
                }
            else: 
                context = {
                    'auction': auction,
                    'latest_bid': latest_bid
                }

            return render(request, 'auction.html', context)
        else:
            context = {
                'auction': auction,
            }

            return render(request, 'auction_expired.html', context)
    else:
        return render(request, '404.html')