Exemple #1
0
def FormEditBid(request, bid_id):
    bid = get_object_or_404(Bid, pk=bid_id)
    form = BidForm(request.POST or None, instance=bid)
    if form.is_valid():
        bid = form.save()
        # this is where you might choose to do stuff.
        # contact.name = 'test'
        bid.save()
        return redirect(FormBid)

    return render_to_response(
        "bids/formeditbid.html", {"bid_form": form, "bid_id": bid_id}, context_instance=RequestContext(request)
    )
Exemple #2
0
def FormAddBid(request):
    # sticks in a POST or renders empty form
    form = BidForm(request.POST or None)
    if form.is_valid():
        cmodel = form.save(commit=False)
        cmodel.User = request.user
        cmodel.Status = "A"
        # TODO check if there are enouf funds, Participation < 100 and competitive bid rate < 100
        # TODO Set status also
        cmodel.save()
        return redirect(FormBid)

    return render_to_response("bids/formaddbid.html", {"bid_form": form}, context_instance=RequestContext(request))
Exemple #3
0
    def get(self, request, pk):

        user = request.user

        if not user.is_authenticated:
            return HttpResponseRedirect('/login/')

        auction = get_object_or_404(Auction, pk=pk)

        try:
            currency_code = request.session['currency']
        except KeyError:
            currency_code = 'EUR'
        converted_price = round(
            currencies.convert('EUR', float(auction.min_price), currency_code),
            2)

        form = BidForm(converted_price, currency_code)

        context = {
            'auction': auction,
            'form': form,
            'converted_price': converted_price,
            'currency_code': currency_code,
        }
        return render(request, 'bids/new_bid.html', context)
Exemple #4
0
def view_listing(request, book_id):

    # Grab the listing itself
    listing = get_object_or_404(Listing,pk=book_id)

    # grab the bidder
    bidder = request.user.seller

    # if the listing is over a week old, it's old
    old_threshold = timezone.now() - timedelta(weeks=3)

    # get all bids associated with this listing
    bids = Bid.objects.filter( listing = listing )
    bid_count = len(bids)

    bid_form = BidForm()
    if request.method == 'POST' and listing.active and not listing.sold:
        if listing.active and not listing.sold:
            bid_form = BidForm( request.POST.copy() )

            # Override whatever the user may have input into the bidder and
            # listing fields (hopefully they will not have set these values
            # anyway).
            bid_form.data['bidder'] = bidder.pk
            bid_form.data['listing'] = listing.pk

            if bid_form.is_valid():
                bid = bid_form.save(commit=False)
                bid.bidder = bidder
                bid.listing = listing
                bid.full_clean()
                bid.save()
                return redirect( 'view_listing', listing.pk )

    return render(request, 'listing.html', {
        'listing' : listing,
        'media' : settings.MEDIA_URL,
        'old' : listing.date_created < old_threshold,
        'bid_count' : bid_count,
        'bids' : bids,
        'bid_form' : bid_form,
    },
    )
Exemple #5
0
    def post(self, request, pk):

        print(request.POST)
        bidder = request.user

        if not bidder.is_authenticated:
            return HttpResponse(status=401,
                                reason='Wrong username or password')

        auction = get_object_or_404(Auction, pk=pk)
        min_price = auction.min_price
        currency_code = request.POST['currency']

        form = BidForm(min_price, currency_code, request.POST)

        if bidder.username == auction.author \
                or bidder.username == auction.leader \
                or auction.state != 'Active':
            # Bidder is the seller or has made the highest bid or auction is not active
            # TODO: Test that POST request to banned auctions are ignored
            # TODO: Handle in a more user-friendly way
            return HttpResponse(status=400)

        if form.is_valid():
            data = form.cleaned_data
            bid_amount = data['bid']
            currency = data['currency']

            # print('Bid_amount = ', bid_amount)
            euro_bid_amount = round(
                currencies.convert(currency, float(bid_amount), 'EUR'), 2)
            # print('Euro bid amount: ', euro_bid_amount)

            if euro_bid_amount > min_price:

                try:
                    auction = Auction.bid(auction.id,
                                          amount=euro_bid_amount,
                                          bidder=bidder)
                    bid = Bid.objects.create(auction_id=auction.id,
                                             amount=euro_bid_amount,
                                             bidder=bidder.username)
                    bid.save()

                    # Extend deadline if auction closes within 5 minutes
                    now = timezone.make_aware(datetime.now())
                    five_minutes_from_now = timezone.make_aware(
                        datetime.now() + timedelta(minutes=5))
                    if auction.deadline <= five_minutes_from_now:
                        auction.deadline = now + timedelta(minutes=5)

                    auction.save()
                    print('successful bid')
                    # Bid was successful
                    return HttpResponseRedirect(
                        reverse('bids:success', args=(pk, )))
                except:
                    # Someone else has locked the auction
                    print('Auction is locked')
                    return HttpResponseRedirect(
                        reverse('auctions:detail', args=(pk, )))
            else:
                print(euro_bid_amount, ' was lover than ', min_price)
                return HttpResponseRedirect(
                    reverse('auctions:detail', args=(pk, )))

        else:
            # The form is not valid
            print('Form is not valid')
            return render(request, 'bids/new_bid.html', {
                'auction': auction,
                'form': form
            })
Exemple #6
0
def new_bid(request, pk):

    try:
        auction = Auction.objects.get(pk=pk)
    except Auction.DoesNotExist:
        return JsonResponse(error_dict(status=404, reason='Auction does not exist.'))

    if request.method == 'GET':
        return JsonResponse(auction.to_dict())

    if request.method == 'POST':

        try:
            username = request.POST['username']
            password = request.POST['password']
            currency_code = request.POST['currency']
            version = request.POST['version']
            bid_amount = request.POST['bid']
        except KeyError:
            return JsonResponse(error_dict(status=400, reason='Missing keys in body.'))

        user = authenticate(username=username, password=password)
        if user is None:
            return JsonResponse(error_dict(status=401, reason='Wrong username or password.'))
        else:
            bidder = user

        if bidder.username == auction.author \
                or bidder.username == auction.leader \
                or auction.state != 'Active':
            return JsonResponse(error_dict(status=400, reason='user cannot bid on auction.'))

        auction_version = auction.get_version()
        if version != auction_version:
            print(version,  ' != ',  str(auction_version))
            return JsonResponse(error_dict(status=400, reason='Auction was updated.'))

        if not currencies.is_currency_valid(currency_code):
            return JsonResponse(error_dict(status=400, reason='Currency code does not exist.'))

        min_price = auction.min_price
        form = BidForm(min_price, currency_code,  request.POST)

        if form.is_valid():
            data = form.cleaned_data
            bid_amount = data['bid']
            euro_bid_amount = currencies.convert(currency_code, float(bid_amount), 'EUR')

            if euro_bid_amount > min_price:
                try:
                    Auction.bid(auction.id, amount=euro_bid_amount, bidder=bidder)
                    bid = Bid.objects.create(auction_id=auction.id, amount=euro_bid_amount, bidder=bidder.username)
                    bid.save()
                    # Bid was successful
                    return JsonResponse(success_dict(status=201, saved_bid=bid_amount))
                except:
                    # auction is locked while updating
                    return JsonResponse(error_dict(status=400, reason='Auction was updated by somebody else.'))
            else:
                return JsonResponse(error_dict(status=400, reason='Minimum bid is 0.1€'))
        else:
            return JsonResponse(error_dict(status=400, reason='Key values are not in right format'))