Exemple #1
0
def create(request):
    """ This function will display your creat page for new auctions """
    if request.method == 'GET':
        return render(request, 'create.html')

    if request.method == 'POST':
        
        try:
            duration = request.POST.get('aduration')

            auction = Auction()
            auction.item_name = request.POST.get('aname')
            auction.description = request.POST.get('adesc')
            auction.list_price = price_to_cents(request.POST.get('aprice'))
            auction.cur_price = auction.list_price
            auction.owner = request.user
            auction.expires = duration_to_datetime(duration)
            auction.save()
            AUCTIONEER.create(auction, float(duration))
            
            return HttpResponseRedirect(reverse('auction', args=(auction.id,)))

        except (TypeError, ValueError) as err:
            print err
            return render(request, 'create.html', {
                'error_message': 'Invalid input types for parameters'
            })

        except IntegrityError as err:
            print err
            return render(request, 'create.html', {
                'error_message': 'You have already created an Auction by that name'
            })

    return HttpResponse(status=500)
Exemple #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"})
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 pusher_auth(request):
    """ This function is the authentication end point for the pusher stream """
    if not request.user.is_authenticated():
        return JsonResponse({'error': 'Not Authenticated for this stream'}, status=404)
        
    if not request.POST.get('channel_name') or not request.POST.get('socket_id'):
        return JsonResponse({'error': 'Incorrect parameters for authentication'}, status=404)

    auth = AUCTIONEER.authenticate(
        request.POST.get('channel_name'),
        request.POST.get('socket_id')
    )
    return JsonResponse(auth)
Exemple #5
0
def create(request):
    """ This function will display your creat page for new auctions """
    if request.method == 'GET':
        return render(request, 'create.html')

    if request.method == 'POST':

        try:
            duration = request.POST.get('aduration')

            auction = Auction()
            auction.item_name = request.POST.get('aname')
            auction.description = request.POST.get('adesc')
            auction.list_price = price_to_cents(request.POST.get('aprice'))
            auction.cur_price = auction.list_price
            auction.owner = request.user
            auction.expires = duration_to_datetime(duration)
            auction.save()
            AUCTIONEER.create(auction, float(duration))

            return HttpResponseRedirect(reverse('auction',
                                                args=(auction.id, )))

        except (TypeError, ValueError) as err:
            print err
            return render(
                request, 'create.html',
                {'error_message': 'Invalid input types for parameters'})

        except IntegrityError as err:
            print err
            return render(
                request, 'create.html', {
                    'error_message':
                    'You have already created an Auction by that name'
                })

    return HttpResponse(status=500)
Exemple #6
0
def pusher_auth(request):
    """ This function is the authentication end point for the pusher stream """
    if not request.user.is_authenticated():
        return JsonResponse({'error': 'Not Authenticated for this stream'},
                            status=404)

    if not request.POST.get('channel_name') or not request.POST.get(
            'socket_id'):
        return JsonResponse(
            {'error': 'Incorrect parameters for authentication'}, status=404)

    auth = AUCTIONEER.authenticate(request.POST.get('channel_name'),
                                   request.POST.get('socket_id'))
    return JsonResponse(auth)