def bidding(request, id): # !!! 새로 수정된 곳(입찰가 입력관련) post = get_object_or_404(Write, pk=id) user = User(username=request.user.username) if int(request.GET.get('plus')) >= post.up_price: if request.user.is_authenticated: price = request.GET.get('plus') post.up_price = request.GET.get('plus') post.save() user = request.user user.save() print(price) b = Bid(userId=user, writerId=post, price=price) b.save() print(b.id) biddings = post.get_biddings() print(biddings['li']) biddings['li'].append(b.id) post.set_biddings(biddings) post.save() print('post id:', post.id, ' biddings: ', biddings['li']) # post.biddings.add(user) # queryset = post.biddings.all() # print(queryset) for li in biddings['li']: print( Bid.objects.get(id=li).userId, '가 입찰한 가격은 ', Bid.objects.get(id=li).price, '원 입니다') context = {'post': post} return render(request, 'auction/detail.html', context) else: return redirect(reverse('mainA')) else: context = {'post': post} return render(request, 'auction/detail.html', context)
def bid(request, auction_id): """ Bid on auction. Bid must be higher than current bid """ auction = get_object_or_404(Auction, id=auction_id) bid = Bid.objects.filter(auction=auction_id).order_by('-bid_time') current_bid = bid[0].bid_amount current_user = auth.get_user(request) if request.method == 'POST': bid_form = BidForm(request.POST) if bid_form.is_valid(): """ If form is valid create the bid only if higher then the current bid and the auction has started. """ bid = Bid() if current_bid < bid_form.cleaned_data['bid_amount'] and \ auction.time_starting < timezone.now(): bid.user = current_user bid.auction = auction bid.bid_time = datetime.now() bid.bid_amount = bid_form.cleaned_data['bid_amount'] bid.save() auction.number_of_bids += 1 auction.save() messages.success(request, 'Bid successfully placed. Good Luck!') else: messages.error(request, 'Bid must be higher than current bid') return redirect('auction', auction_id=auction_id) else: bid_form = BidForm() return redirect('auction', auction_id=auction_id)
def post(self, request, *args, **kwargs): auction = Item.objects.get(pk=request.POST.get('auction')) if auction.status != 'AC': messages.info(request, "The auction is not active.", 'danger') return HttpResponseRedirect( reverse('auction-details', kwargs={'pk': auction.pk})) if request.user == auction.creator: messages.info(request, "Sellers can't bid their own auctions.", 'danger') return HttpResponseRedirect( reverse('auction-details', kwargs={'pk': auction.pk})) bid = Bid() bid.user = request.user bid.auction = auction bid_amount = float(request.POST.get('price')) try: bid_amount = Decimal(bid_amount).quantize(Decimal('0.01')) except (DecimalException, InvalidOperation): messages.info(request, 'Invalid value for a bid.', 'danger') return HttpResponseRedirect( reverse('auction-details', kwargs={'pk': auction.pk})) top_bid = auction.bids.order_by('price').last() if top_bid is None and bid_amount > auction.price or top_bid and bid_amount > top_bid.price: bid.price = bid_amount else: messages.info(request, 'Place a higher bid than the current top bid.', 'danger') return HttpResponseRedirect( reverse('auction-details', kwargs={'pk': auction.pk})) bid.save() mail.send_mail("A new bid has been placed.", "A new greater bid has been succesfully placed!", '*****@*****.**', [auction.creator]) if top_bid.user: mail.send_mail("Your bid has been replaced.", "Your bid has beend replaced by a higher bid!", '*****@*****.**', [top_bid.user]) messages.success(request, 'Bid placed succesfully.') return HttpResponseRedirect( reverse('auction-details', kwargs={'pk': bid.auction.id}))
def auction(request, auction_id): """ Render the auction """ auction = get_object_or_404(Auction, id=auction_id) bid = Bid.objects.filter(auction=auction_id).order_by('-bid_time') bid_form = BidForm() if auction: if auction.time_ending > timezone.now(): if bid: latest_bid = bid[0] else: bid_default = Bid() # If bid[0] False create first bid for auction bid_default.user = get_object_or_404(User, id=1) # Set default user to user.id=1 bid_default.auction = auction bid_default.bid_time = auction.time_starting bid_default.bid_amount = 0.00 bid_default.save() latest_bid = bid_default if auction.time_starting < timezone.now(): # If the auction has not started yet context = { # hide the bid form. 'auction': auction, 'latest_bid': latest_bid, 'bid_form': bid_form } else: context = { 'auction': auction, 'latest_bid': latest_bid } return render(request, 'auction.html', context) else: context = { 'auction': auction, } return render(request, 'auction_expired.html', context) else: return render(request, '404.html')
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 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 bid(request): if request.method == 'POST': auction = Auctions.objects.filter(id=request.POST['auction_id'])[:1].get() bid_amount = request.POST['bid_amount'] created_by = request.user max_auto_bid = 0 is_auto_bid = request.POST.get('is_auto_bid', False) if(is_auto_bid == False): auto_bids = Bid.objects.filter(is_auto_bid=True, auctions_id=auction.id, max_auto_bid__gte=bid_amount) if(auto_bids.count() > 0): earlest_auto_bids = sorted(auto_bids, key=operator.attrgetter('created_at')) earlest_auto_bid = earlest_auto_bids[0] bid_amount = bid_amount is_auto_bid = True created_by = earlest_auto_bid.created_by max_auto_bid = earlest_auto_bid.max_auto_bid auction.current_bid = bid_amount else: auto_bids = Bid.objects.filter(is_auto_bid=True, auctions_id=auction.id, max_auto_bid__gte=request.POST['max_auto_bid']).exclude(created_by=request.user) if(auto_bids.count() > 0): earlest_auto_bids = sorted(auto_bids, key=operator.attrgetter('created_at')) earlest_auto_bid = earlest_auto_bids[0] if(earlest_auto_bid.max_auto_bid == Decimal(request.POST['max_auto_bid'])): bid_amount = earlest_auto_bid.max_auto_bid else: bid_amount = Decimal(request.POST['max_auto_bid']) + auction.min_bid is_auto_bid = True created_by = earlest_auto_bid.created_by max_auto_bid = earlest_auto_bid.max_auto_bid else: max_bid = Bid.objects.filter(is_auto_bid=True, auctions_id=auction.id).aggregate(Max('max_auto_bid')) is_auto_bid = True max_auto_bid = request.POST['max_auto_bid'] if(max_bid['max_auto_bid__max'] == 0 or max_bid['max_auto_bid__max'] == None): bid_amount = auction.current_bid + auction.min_bid else: bid_amount = max_bid['max_auto_bid__max'] + auction.min_bid auction.current_bid = bid_amount new_bid = Bid(auctions = auction, bid_amount = bid_amount, is_auto_bid = is_auto_bid, created_by = created_by, max_auto_bid = max_auto_bid) auction.save() new_bid.save() return HttpResponseRedirect('/home') else: return render(request, 'home.html', {})
def bid(request, item_id): auction = Auction.objects.get(pk=item_id) if auction.version != request.session["version"]: return HttpResponse( "Someone else is currently bidding on this item, try again later.") if auction.status == "active": if request.method == "POST": if request.user.is_authenticated: bid_form = BidForm(request.POST) if bid_form.is_valid(): new_bid = Bid() new_bid.price = bid_form.cleaned_data['new_price'] new_bid.auction_id = item_id new_bid.bidder = request.user if (float(auction.minimum_price) + 0.01) > float( new_bid.price) or (float(auction.highbid) + 0.01) > float(new_bid.price): msg = _( "Bid must be at least 0.01 higher than minimum price or previous bid." ) messages.add_message(request, messages.ERROR, msg) #print("Bid has to be at least 0.01 higher than minimum price or previous bid.") return HttpResponseRedirect(reverse('index')) previous_bidder = auction.highbidder auction.highbid = new_bid.price auction.highbidder = request.user.username new_bid.save() auction.save() # Check for previous bidder and notify via email if previous_bidder != "None": previous_bidder_email = User.objects.get( username=previous_bidder) send_mail( "Outbid", "Your have been outbid on the following item: " + auction.title, "*****@*****.**", [previous_bidder_email.email], fail_silently=False) # Notify new bidder send_mail("New bid", "Your bid on " + auction.title + " has been recieved", "*****@*****.**", [request.user.email], fail_silently=False) # Notify the seller send_mail("New bid", "A new bid " + str(new_bid.price) + " has been placed on you auction: " + auction.title, "*****@*****.**", [auction.seller.email], fail_silently=False) print("New bid recieved on ", auction.title, " with the amount of ", new_bid.price) return HttpResponseRedirect(reverse('index')) else: print("Incorrect bid given") return render(request, "bid.html", {"bid_form": bid_form}) else: print("Unauthenticated user tried to place a bid") return HttpResponseRedirect(reverse('signin')) else: if float(auction.highbid) == 0: price = auction.minimum_price auction.highbid = float(price) auction.save() else: price = float(auction.minimum_price) bid_form = BidForm() return render(request, "bid.html", { "bid_form": bid_form, "auction": auction, "price": price }) else: # Auction is not active, return to home page return HttpResponseRedirect(reverse('index'))