Exemple #1
0
def bidding(request, user_id, auction_id):
    auction = get_object_or_404(Auction, pk=auction_id)
    # bids = Bid.objects.filter(id=auction_id)
    if request == "POST":
        bid = Bid()
        bid.bidder = user_id
        bid.bid_amount = request.POST['bid_amount']
        bid.save()
        return render(request, 'auction_detail.html', {'bid': bid})
    return render(request, 'bidding.html', {{'auction': auction}})
Exemple #2
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"))
Exemple #3
0
def bid(request, auction_id):
    """ Allows a user to bid on a particular item """

    if request.method != 'POST':
        return JsonResponse({'error': 'Must be called with post'}, status=405)

    auctions = Auction.objects.filter(id=auction_id)
    if len(auctions) == 0:
        return JsonResponse({'error': 'That auction does not exist'},
                            status=404)

    auction = auctions[0]

    if pytz.utc.localize(datetime.now()) > auction.expires:
        return JsonResponse({'error': 'This auction has ended'})

    try:
        bid_amount = price_to_cents(request.POST.get('bprice'))
    except (TypeError, ValueError) as err:
        print err
        return JsonResponse({'error': "Invalid input for bid amount"},
                            status=400)

    try:
        with transaction.atomic():
            bids = Bid.objects.filter(auction=auction).order_by('-bid_amount')

            if (len(bids) > 0 and bids[0].bid_amount >= bid_amount) or (
                    auction.list_price > bid_amount):
                return JsonResponse(
                    {'error': "Entered amount is lower than current bid"})

            bid = Bid()
            bid.bidder = request.user
            bid.auction = auction
            bid.bid_amount = bid_amount
            bid.save()

            auction.cur_price = bid_amount
            auction.save()
            AUCTIONEER.bid(auction, bid)

    except IntegrityError as err:
        print err
        return JsonResponse({'error': "You've already been outbid"})

    return JsonResponse({'success': "You're the highest bidder"})
Exemple #4
0
def bid(request, auction_id):
    """ Allows a user to bid on a particular item """

    if request.method != 'POST':
        return JsonResponse({'error': 'Must be called with post'}, status=405)

    auctions = Auction.objects.filter(id=auction_id)
    if len(auctions) == 0:
        return JsonResponse({'error': 'That auction does not exist'}, status=404)
        
    auction = auctions[0]

    if pytz.utc.localize(datetime.now()) > auction.expires:
        return JsonResponse({'error': 'This auction has ended'})
    
    try:
        bid_amount = price_to_cents(request.POST.get('bprice'))
    except (TypeError, ValueError) as err:
        print err
        return JsonResponse({'error': "Invalid input for bid amount"}, status=400)
    
    try:
        with transaction.atomic():
            bids = Bid.objects.filter(auction=auction).order_by('-bid_amount')

            if (len(bids) > 0 and bids[0].bid_amount >= bid_amount) or (auction.list_price > bid_amount):
                return JsonResponse({'error': "Entered amount is lower than current bid"})

            bid = Bid()
            bid.bidder = request.user
            bid.auction = auction
            bid.bid_amount = bid_amount
            bid.save()

            auction.cur_price = bid_amount
            auction.save()
            AUCTIONEER.bid(auction, bid)

    except IntegrityError as err:
        print err
        return JsonResponse({'error': "You've already been outbid"})
        
    return JsonResponse({'success': "You're the highest bidder"})