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))
Exemple #2
0
 def test_build_portfolio(self):
     """Create portfolio w/ current prices from user stocks"""
     self.populateTestDb()
     portfolio = h.build_portfolio(q.select_stocks_by_user(1), 9999)
     assert 'stocks' in portfolio
     assert 'AAPL' in portfolio['stocks']
     assert 'name' in portfolio['stocks']['AAPL']
     assert 'value' in portfolio['stocks']['AAPL']
     assert 'cash' in portfolio
     assert 'total' in portfolio
Exemple #3
0
 def test_build_portfolio_negative_quantity(self):
     """Build Portfolio should exclude stocks that've been sold"""
     self.populateTestDb()
     transaction = q.Transaction(user_id=1,
                                 stock_id=1,
                                 quantity=-1,
                                 price=1)
     db.session.add(transaction)
     db.session.commit()
     portfolio = h.build_portfolio(
         q.select_stocks_by_user(1),
         9999,
     )
     assert 'AAPL' not in portfolio['stocks']
     assert 'cash' in portfolio
     assert 'total' in portfolio
def leaders():
    """Show leader board"""
    users = q.select_all_users()
    leaders = []
    for user in users:
        total = user.cash
        stocks = q.select_stocks_by_user(user.id)
        for stock in stocks:
            if stock.quantity > 0:
                price = h.lookup(stock.symbol)['price']
                total += stock.quantity * price
        leaders.append([user.username, total])
    leaders.sort(reverse=True, key=lambda x: x[1])
    leaders = leaders[:10]
    for i in range(len(leaders)):
        leaders[i][1] = h.usd(leaders[i][1])
    return render_template('leaders.html', leaders=leaders[:11])
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)
Exemple #6
0
 def test_select_stocks_by_user(self):
     """Get a sum of user's stocks"""
     self.populateTestDb()
     assert (1.0, 'Apple', 'AAPL') in q.select_stocks_by_user(1)