def bids(): if request.method == 'GET': bids = Bid.query.all() return BidSchema().jsonify(bids, many=True) if request.method == 'POST': form = BidForm() if form.validate(): bid = Bid(**request.form.to_dict()) bid.usd_value = int(bid.amount) * int(bid.rate) if will_oversubscribe(bid): bid.status = 'Waitlisted' try: db.session.add(bid) db.session.commit() except IntegrityError: db.session.rollback() data = {'errors': {'name': 'You\'ve already bidded.'}} return make_response(jsonify(data), 400) return BidSchema().jsonify(bid) else: return make_response(jsonify({'errors': form.errors}), 400)
def item(item_id): form = BidForm() item = Item.query.get_or_404(item_id) offer = Offer.query.filter_by(item_id=item_id).order_by( Offer.id.desc()).first() category = Category.query.filter_by(id=item.category_id).first() item.views = item.views + 1 db.session.commit() time = datetime.now() return render_template('single-item.html', title=item.name, item=item, offerLen=len(item.offer), form=form, time=time, category=category)
def offer(item_id): form = BidForm() price = form.offer.data item = Item.query.get_or_404(item_id) new_offer = Offer(item_id=item_id, user_id=current_user.id, price=int(price)) db.session.add(new_offer) print(price) print(item.min_price) if item.user_id == current_user.id: flash('You cant bid on your own item!', 'info') else: if item.min_price < price: priceUpdate = Item.query.filter_by(id=item_id).first() priceUpdate.min_price = price db.session.commit() flash('Bid successful!', 'success') else: flash('Price too low!', 'info') return redirect(url_for('item', item_id=item_id))
def auction(auction_id): auction = Auction.query.filter_by(id=auction_id).first_or_404() users = auction.users.order_by(db.func.lower(User.username)) lot = auction.current_lot() if auction not in current_user.auctions: flash("You are not an authorized member of that auction.") return redirect(url_for("index")) balances = (auction.balances.join(User).order_by( db.func.lower(User.username))) if not lot: return render_template("auction.html", title=f"Auction {auction_id}", auction=auction, users=users, balances=balances, lot=None) advance_form = AdvanceForm() advance_form.auction_id.data = auction_id close_bidding_form = CloseBiddingForm() waiting_on = lot.waiting_on() if (current_user == auction.creator) and not waiting_on: if advance_form.submit_advance.data and advance_form.validate(): lot.record_winner() if lot.content: auction.pool.remove_from_pool(lot.content) if not auction.is_complete(): requested_lot = advance_form.next_lot.data if requested_lot: try: num_cards = int(requested_lot) auction.add_lot(size=num_cards) except ValueError: auction.add_lot(card=advance_form.next_lot.data) else: auction.add_lot(size=auction.default_lot) db.session.commit() emit("next lot", {"auction.id": auction.id}, namespace=None, broadcast=True) if auction.is_complete(): return render_template("auction.html", title=f"Auction {auction_id}", auction=auction, users=users, lot=None) if advance_form.submit_reset.data: lot.reset() db.session.commit() emit("reset", {"auction.id": auction.id}, namespace=None, broadcast=True) if (current_user == auction.creator) and waiting_on: if close_bidding_form.submit_close.data: for user in waiting_on: bid = Bid(bidder=user, lot=auction.current_lot(), amount=0) db.session.add(bid) db.session.commit() skipped = ", ".join([user.username for user in waiting_on]) flash(f"Proceeding without {skipped}.") emit("some bidders skipped", {"auction.id": auction.id}, namespace=None, broadcast=True) bid_form = BidForm() bid_form.auction_id.data = auction_id bid_form.lot_id.data = auction.current_lot().id if bid_form.validate_on_submit() and bid_form.submit_bid.data: if bid_form.lot_id.data != auction.current_lot().id or not waiting_on: # Don't take bids if user wasn't looking at current lot for some # reason, or if the bid was somehow submitted on a completed lot. # Not sure if the first part is actually working as implemented. return redirect(url_for("auction", auction_id=auction_id)) amount = bid_form.amount.data if not amount: amount = 0 bid = Bid(bidder=current_user, lot=auction.current_lot(), amount=amount) db.session.add(bid) db.session.commit() emit("a bid", {"auction.id": auction.id}, namespace=None, broadcast=True) flash(f"Bid of {amount} recorded.") return redirect(url_for("auction", auction_id=auction_id)) lot = auction.current_lot() waiting_on = lot.waiting_on() if waiting_on and current_user not in waiting_on: names = ", ".join([user.username for user in waiting_on]) flash(f"Waiting on {names}.") return render_template("auction.html", title=f"Auction {auction_id}", auction=auction, users=users, balances=balances, lot=lot, waiting_on=waiting_on, bid_form=bid_form, advance_form=advance_form, close_bidding_form=close_bidding_form)
def index(): form = None bids = Bid.query.filter_by(active=1).order_by(Bid.time.desc()) if current_user.is_anonymous: return render_template('index.html', title='Main screen', bids=bids) player_id = None max_bid = 1 if bids.count() > 0: player_id = bids[0].player_id max_bid = bids[0].bid + 1 form = BidForm(bid=max_bid, user_id=current_user.get_id(), player_id=player_id) if form.validate_on_submit(): latest_bid = Bid.query.filter_by(active=1).order_by( Bid.time.desc()).first() user = User.query.filter_by(id=form.user_id.data).first() if form.bid.data <= user.money: sql = """select CURRENT_TIMESTAMP from users""" #stupido hack result = db.engine.execute(sql) names = [] for row in result: names.append(row[0]) #flash('latest bid time ' + str(latest_bid.time) + ' current time ' + names[0]) #datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p') 2018-06-25 14:03:39 datetime_object = datetime.strptime(names[0], '%Y-%m-%d %H:%M:%S') #flash('differenza ' + str(datetime_object - latest_bid.time)) if (datetime_object - latest_bid.time).total_seconds() < 31: if form.bid.data > latest_bid.bid: #flash('ultimo user id ' + str(latest_bid.user_id) + ' il tuo invece ' + str(form.user_id.data) + ' la espressione ' + form.user_id.data != latest_bid.user_id) if int(form.user_id.data) != int(latest_bid.user_id): new_bid = Bid(bid=form.bid.data, active=1, user_id=form.user_id.data, player_id=form.player_id.data) db.session.add(new_bid) db.session.commit() #flash('Good luck on your bid') latest_bid = Bid.query.filter_by(active=1).order_by( Bid.time.desc()).first() str_new_bid = r'<li class="list-group-item d-flex justify-content-between align-items-center"> <span> ' + str( latest_bid.player.name ) + r' </span> <span>' + str( latest_bid.player.surname ) + r'</span> <span> ' + str( latest_bid.user.name ) + r' </span> <span class="badge badge-primary badge-pill">' + str( latest_bid.bid) + r'</span> <span>' + str( latest_bid.time) + r'</span></li>' socketio.emit('my_response', {'data': str_new_bid}, namespace='/test') else: flash('You are already the best bidder') else: flash('You must offer more') else: flash('Too late :(') else: flash('Not enough money') return render_template('index.html', title='Main screen', bids=bids, form=form)