def buy(): """Buy shares of stock""" if request.method == 'POST': symbol = request.form.get('symbol') shares = request.form.get('shares') if not symbol: return h.apology("Provide a symbol") elif not shares or not shares.isdigit(): return h.apology("Provide a valid quantity") quote = h.lookup(symbol) if not quote: return h.apology("No such company") order_total = float(shares) * float(quote['price']) user = q.select_user_by_id(session['user_id']) if order_total <= user.cash: stock = q.select_stock_by_symbol(quote['symbol']) try: stock.id except AttributeError: q.insert_stock(quote['symbol'], quote['name']) stock = q.select_stock_by_symbol(quote['symbol']) q.insert_transaction(session['user_id'], stock.id, shares, quote['price']) q.update_user_cash(order_total * -1, session['user_id']) return redirect('/') return h.apology("Not enough cash") else: return render_template('buy.html', stocks=stocks)
def sell(): """Sell shares of stock""" if request.method == 'POST': symbol = request.form.get('symbol') shares = request.form.get('shares') if not symbol: return h.apology("Provide a symbol") elif not shares or not shares.isdigit(): return h.apology("Provide a valid quantity") shares = float(shares) quote = h.lookup(symbol) if not quote: return h.apology("No such company") try: stock = q.select_stock_by_symbol(symbol) position = q.select_transactions_by_stock(stock.id, session['user_id']) if position.shares >= shares: order_total = shares * quote['price'] q.insert_transaction(session['user_id'], stock.id, shares * -1, quote['price']) q.update_user_cash(order_total, session['user_id']) return redirect('/') else: return h.apology("You don't own enough of that stock.") except AttributeError: return h.apology("You don't own that stock") else: user = q.select_user_by_id(session['user_id']) return render_template('sell.html', portfolio=h.build_portfolio( q.select_stocks_by_user(user.id), user.cash))
def nuke(): """Reset user portfolio""" if request.method == 'POST': confirm = request.form.get('yesno') if not confirm or confirm == 'no': return h.apology("Ok, we won't reset your portfolio") if confirm == 'yes': q.delete_transactions_by_user(session['user_id']) user = q.select_user_by_id(session['user_id']) q.update_user_cash(10000 - user.cash, session['user_id']) return redirect('/') return redirect('/profile')
def profile(): """Show user profile information""" if request.method == 'POST': password = request.form.get('password') new_password = request.form.get('new') confirmation = request.form.get('confirmation') if not password or not new_password or not confirmation: return h.apology("Please fill all fields") elif new_password != confirmation: return h.apology("New password and confirmation don't match") user = q.select_user_by_id(session['user_id']) if check_password_hash(user.password_hash, password): new_hash = generate_password_hash(new_password) q.update_user_hash(new_hash, session['user_id']) return redirect('/login') else: return h.apology("Incorrect password") else: user = q.select_user_by_id(session['user_id']) user.cash = h.usd(user.cash) return render_template('profile.html', user=user)
def index(): """Show portfolio of user's stocks""" user = q.select_user_by_id(session['user_id']) portfolio = h.build_portfolio(q.select_stocks_by_user(user.id), user.cash) return render_template("index.html", portfolio=portfolio, stocks=stocks)
def test_select_user_by_id(self): """Get a user based on ID""" self.populateTestDb() assert q.User.query.first() == q.select_user_by_id(1)