def placebid(listing): bidform = BidForm() listing_obj = Listing.query.filter_by(id=listing).first() if bidform.validate_on_submit(): # Write to database bid = Bid() bid.bid_amount = bidform.bid_amount.data bid.listing_id = listing_obj.id bid.bidder_name = current_user.name # Check that user is not bidding on their own listing if listing_obj.seller != current_user.name: # Check if bid is more than current bid if bid.bid_amount > listing_obj.current_bid: listing_obj.current_bid = bid.bid_amount # Set bid status to winning bid.bid_status = 'Winning' # Add bid to db db.session.add(bid) # Set bid status of all other bids to 'Outbid' oldbids = Bid.query.filter_by(listing_id=listing).all() for bid in oldbids[:-1]: bid.bid_status = 'Outbid' # Update total bids update_total_bids = Listing.query.filter_by(id=listing).first() update_total_bids.total_bids += 1 # Commit bid to db db.session.commit() flash("Bid was successfully placed!", 'success') print('Bid was successfully placed!') else: flash("Bid amount has to be higher than current bid") else: flash("You cannot bid on your own listing", 'danger') print('User cannot bid on their own listing') else: print('Bid form is not valid') return redirect(url_for('listing.showlisting', id=listing))
def showlisting(id): listing = Listing.query.filter_by(id=id).first() review_form_instance = ReviewForm() bid_form_instance = BidForm() return render_template('listings/showlisting.html', listing=listing, form=review_form_instance, bidform=bid_form_instance)
def bidding(request): if request.method == 'POST': user = request.user form = BidForm(request.POST) if not form.is_valid(): # Return form error if return false return JsonResponse(status=400, data={'status': _('error'), 'msg': form.errors}) try: """ Here we have to check if the bid is allowed. It cannot be lower or equal to the current highest bid """ biddings = Bid.objects.filter(rider_id=form.cleaned_data['rider']).order_by('-amount') if len(biddings) < 1: """ Raise exception if queryset return 0 """ raise Bid.DoesNotExist else: get_highest_bid = biddings[0] if get_highest_bid.amount >= form.cleaned_data['amount']: """ Raise exception once the new bid is lower than current bid highest bid """ raise Exception("Bod moet hoger zijn dan huidige hoogste bod") new_bidding = Bid(rider_id=form.cleaned_data['rider'], team_captain=user, amount=form.cleaned_data['amount']) except Bid.DoesNotExist: new_bidding = Bid(rider_id=form.cleaned_data['rider'], team_captain=user, amount=form.cleaned_data['amount']) except Exception as err: #return JsonResponse(status=400, data={'status': _('error'), 'msg': str(err) }) pass """ Check if user is allowed to make this bid, if he has enough points remaining """ enough = TeamCaptain.objects.filter(user=user) if TeamCaptain.objects.filter(user=user)[0].max_allowed_bid() >= form.cleaned_data['amount']: new_bidding.save() else: pass return JsonResponse(status=200, data={'status': _('success'), 'msg': _('Bid successfully')}) else: return JsonResponse(status=405, data={'status': _('error'), 'msg': _('Method not allowed')})
def bid(cls, request, auction_id): # DZIALA form = BidForm(request.POST) if form.is_valid(): price = form.cleaned_data["bid"] current_user = ShopUser.objects.get(user__pk=request.user.pk) auction = AuctionItem.objects.get(pk=auction_id) if auction.current_price < price: bid = Bid() bid.user = current_user bid.item = auction bid.date = datetime.now() bid.price = price bid.save() auction.bids.add(bid) auction.current_price = price auction.save() return HttpResponseRedirect('/aukcje/%s/' % str(auction_id))
def post(self, request, item_id): # TODO: using is_authenticated is not good since it does not check if user is banned etc. if request.user.is_authenticated: try: # Try to parse the int post_data = {'new_price': int(request.POST['new_price'])} form = BidForm(post_data) if form.is_valid(): result_dict = views.do_bid(request, item_id) status = result_dict['status'] msg = result_dict['msg'] if status == 'success': # This is correct. Test pass. auction = AuctionModel.objects.get(id=item_id) data = BidAPIAuctionSerializer(auction).data data.update({'message': 'Bid successfully'}) return Response(data) elif status == 'fail-redirect-to-index': pass elif status == 'fail-rerender': return Response({'message': msg}, status=400) else: msg = msg + '. Unexpected error' return Response({'message': msg}, status=400) else: response_dict = {'message': 'Invalid form data'} return Response(response_dict) except ValueError: msg = 'Bid must be a number' return Response({'message': msg}, status=400) else: response_dict = { 'detail': 'Authentication credentials were not provided' } return Response(response_dict, status=401)
def bidding(request): if request.method == 'POST': user = request.user form = BidForm(request.POST) if not form.is_valid(): # Return form error if return false return JsonResponse(status=400, data={ 'status': _('error'), 'msg': form.errors }) try: """ Here we have to check if the bid is allowed. It cannot be lower than the current highest bid Except when it is a Joke bid. Let's start accepting equal biddings """ # get the rider that is on auction rider = get_rider_on_auction() rider_id = rider.id biddings = Bid.objects.filter( rider_id=rider_id).order_by('-amount') if len(biddings) < 1: """ first bid, so highest_bid = 0 """ get_highest_bid = 0 else: get_highest_bid = biddings[0].amount if get_highest_bid == form.cleaned_data['amount']: # only allow if user has a Joker if check_joker(user, rider_id): # you can allow the bid new_bidding = Bid(rider_id=rider_id, team_captain=user, amount=form.cleaned_data['amount']) else: #disallow, no joker and equasl bid print("disallow it") raise Exception( "Bod moet hoger zijn dan huidige hoogste bod. Geen joker." ) elif get_highest_bid > form.cleaned_data['amount']: """ Raise exception once the new bid is lower than current bid highest bid """ raise Exception("Bod moet hoger zijn dan huidige hoogste bod") else: # new bid is higher than highest bid new_bidding = Bid(rider_id=rider_id, team_captain=user, amount=form.cleaned_data['amount']) enough = TeamCaptain.objects.filter(user=user) if TeamCaptain.objects.filter(user=user)[0].max_allowed_bid( ) >= form.cleaned_data['amount']: new_bidding.save() else: raise Exception("Not enough points left to make that bid") return JsonResponse(status=200, data={ 'status': _('success'), 'msg': _('Bid successfully') }) except Exception as err: return JsonResponse(status=400, data={ 'status': _('error'), 'msg': str(err) }) #pass """ Check if user is allowed to make this bid, if he has enough points remaining """ else: return JsonResponse(status=405, data={ 'status': _('error'), 'msg': _('Method not allowed') })
def inner(request, id): bid_form = BidForm() return view(request, id, {"bid_form": bid_form})
def test_can_create_bid(self): form = BidForm({'bid_amount': '100.00'}) self.assertTrue(form.is_valid())