Esempio n. 1
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"})
Esempio n. 2
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"})