Compare commits

..

No commits in common. "46fffb7da264bb773eef2fe97662df66e3e89c56" and "f12011707e9f66aac3f23d212707cf713ffeabdd" have entirely different histories.

7 changed files with 31 additions and 66 deletions

85
app.py
View File

@ -41,30 +41,22 @@ def index():
cash = db.execute("SELECT * FROM users where id = ?", user_id) cash = db.execute("SELECT * FROM users where id = ?", user_id)
stocks = db.execute( stocks = db.execute(
"SELECT symbol, sum(shares) FROM transactions WHERE user_id = ? GROUP BY symbol HAVING sum(shares) > 0", "SELECT symbol, sum(shares) FROM transactions WHERE user_id = ? GROUP BY symbol HAVING sum(shares) > 0", user_id)
user_id,
)
stocks_total = 0 stocks_total = 0
for stock in stocks: for stock in stocks:
symbol = stock["symbol"] symbol = stock["symbol"]
# Update share price
stock_price = lookup(symbol.upper()) stock_price = lookup(symbol.upper())
stock["shares"] = stock["sum(shares)"] stock["shares"] = stock["sum(shares)"]
stock["price"] = stock_price["price"] stock["price"] = stock_price["price"]
# Get total value of owned stock stock["total"] = usd(stock_price["price"] * stock["sum(shares)"])
stock["total"] = stock_price["price"] * stock["sum(shares)"]
# Get value of all owned stocks
stocks_total = stocks_total + (stock_price["price"] * stock["sum(shares)"]) stocks_total = stocks_total + (stock_price["price"] * stock["sum(shares)"])
# Get total value of stocks and cash total = usd(cash[0]["cash"] + stocks_total)
total = cash[0]["cash"] + stocks_total
return render_template( return render_template("home.html", cash=usd(cash[0]["cash"]), stocks=stocks, total=total)
"home.html", cash=cash[0]["cash"], stocks=stocks, total=total
)
# return apology("MOFO") # return apology("MOFO")
@ -76,7 +68,7 @@ def buy():
return render_template("buy.html") return render_template("buy.html")
else: else:
symbol = request.form.get("symbol") symbol = request.form.get("symbol")
shares = request.form.get("shares") shares = float(request.form.get("shares"))
if not symbol: if not symbol:
return apology("Not Symbol") return apology("Not Symbol")
@ -86,40 +78,26 @@ def buy():
if stock == None: if stock == None:
return apology("Symbol not found") return apology("Symbol not found")
if not shares or shares.isalpha() or not float(shares).is_integer(): if not shares == "":
return apology("invalid shares") transaction_value = float(shares) * stock["price"]
else: user_id = session["user_id"]
if float(shares) > 0: user_cash_db = db.execute("SELECT cash FROM users WHERE id = ?", user_id)
transaction_value = float(shares) * stock["price"] user_cash = user_cash_db[0]["cash"]
user_id = session["user_id"] if user_cash < transaction_value:
user_cash_db = db.execute( return apology("U broke, m8!")
"SELECT cash FROM users WHERE id = ?", user_id
)
user_cash = user_cash_db[0]["cash"]
if user_cash < transaction_value: free_cash = user_cash - transaction_value
return apology("U broke, m8!")
free_cash = user_cash - transaction_value db.execute("UPDATE users SET cash = ? WHERE id = ?", free_cash, user_id)
db.execute("UPDATE users SET cash = ? WHERE id = ?", free_cash, user_id) date = datetime.datetime.now()
date = datetime.datetime.now() db.execute("INSERT INTO transactions (user_id, symbol, shares, price, date) VALUES (?, ?, ?, ?, ?)",
user_id, stock["symbol"], shares, stock["price"], date)
db.execute( flash("Bought!")
"INSERT INTO transactions (user_id, symbol, shares, price, date) VALUES (?, ?, ?, ?, ?)",
user_id,
stock["symbol"],
shares,
stock["price"],
date,
)
flash("Bought!")
else:
return apology("Missing shares", 400)
return redirect("/") return redirect("/")
# return apology("TODO") # return apology("TODO")
@ -230,13 +208,11 @@ def register():
username_exists = db.execute("SELECT * FROM users WHERE username = ?", username) username_exists = db.execute("SELECT * FROM users WHERE username = ?", username)
if username_exists: if username_exists:
flash("Username exists already!")
return apology("username already exists!", 400) return apology("username already exists!", 400)
db.execute( db.execute("INSERT INTO users(username, hash) VALUES(?, ?)",
"INSERT INTO users(username, hash) VALUES(?, ?)", username, generate_password_hash(password))
username,
generate_password_hash(password),
)
return redirect("/") return redirect("/")
else: else:
return render_template("register.html") return render_template("register.html")
@ -262,10 +238,7 @@ def sell():
return apology("Symbol not found") return apology("Symbol not found")
stocks = db.execute( stocks = db.execute(
"SELECT symbol, sum(shares) FROM transactions WHERE user_id = ? AND symbol = ?", "SELECT symbol, sum(shares) FROM transactions WHERE user_id = ? AND symbol = ?", user_id, symbol)
user_id,
symbol,
)
if stocks[0]["sum(shares)"] < shares: if stocks[0]["sum(shares)"] < shares:
return apology("Not enough shares") return apology("Not enough shares")
@ -281,14 +254,8 @@ def sell():
date = datetime.datetime.now() date = datetime.datetime.now()
db.execute( db.execute("INSERT INTO transactions (user_id, symbol, shares, price, date) VALUES (?, ?, ?, ?, ?)",
"INSERT INTO transactions (user_id, symbol, shares, price, date) VALUES (?, ?, ?, ?, ?)", user_id, stock["symbol"], -abs(shares), stock["price"], date)
user_id,
stock["symbol"],
-abs(shares),
stock["price"],
date,
)
flash("Sold!") flash("Sold!")
@ -296,8 +263,6 @@ def sell():
else: else:
stocks = db.execute( stocks = db.execute(
"SELECT symbol FROM transactions WHERE user_id = ? GROUP BY symbol HAVING sum(shares) > 0", "SELECT symbol FROM transactions WHERE user_id = ? GROUP BY symbol HAVING sum(shares) > 0", user_id)
user_id,
)
return render_template("sell.html", stocks=stocks) return render_template("sell.html", stocks=stocks)
# return apology("TODO") # return apology("TODO")

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -10,7 +10,7 @@
<input class="form-control mx-auto w-auto" autocomplete="off" autofocus="" name="symbol" placeholder="Symbol" type="text"> <input class="form-control mx-auto w-auto" autocomplete="off" autofocus="" name="symbol" placeholder="Symbol" type="text">
</div> </div>
<div class="mb-3"> <div class="mb-3">
<input class="form-control mx-auto w-auto" autocomplete="off" autofocus min="1" name="shares" placeholder="Shares" type="number"> <input class="form-control mx-auto w-auto" autocomplete="off" autofocus min="1" name="shares" placeholder="Shares" type="text">
</div> </div>
<button class="btn btn-primary" type="submit">Buy</button> <button class="btn btn-primary" type="submit">Buy</button>
</form> </form>

View File

@ -20,7 +20,7 @@
<tr> <tr>
<td class="text-start">{{ stock.symbol }}</td> <td class="text-start">{{ stock.symbol }}</td>
<td class="text-end">{{ stock.shares }}</td> <td class="text-end">{{ stock.shares }}</td>
<td class="text-end">{{ stock.price | usd}}</td> <td class="text-end">{{ stock.price }}</td>
<td class="text-end">{{ stock.date }}</td> <td class="text-end">{{ stock.date }}</td>
</tr> </tr>
{% endfor%} {% endfor%}

View File

@ -20,8 +20,8 @@
<tr> <tr>
<td class="text-start">{{ stock.symbol }}</td> <td class="text-start">{{ stock.symbol }}</td>
<td class="text-end">{{ stock.shares }}</td> <td class="text-end">{{ stock.shares }}</td>
<td class="text-end">{{ stock.price | usd }}</td> <td class="text-end">{{ stock.price }}</td>
<td class="text-end">{{ stock.total | usd }}</td> <td class="text-end">{{ stock.total }}</td>
</tr> </tr>
{% endfor%} {% endfor%}
</tbody> </tbody>
@ -29,11 +29,11 @@
<tfoot> <tfoot>
<tr> <tr>
<td class="border-0 fw-bold text-end" colspan="3">Cash</td> <td class="border-0 fw-bold text-end" colspan="3">Cash</td>
<td class="border-0 text-end" colspan="3">{{ cash | usd }}</td> <td class="border-0 text-end" colspan="3">{{ cash }}</td>
</tr> </tr>
<tr> <tr>
<td class="border-0 fw-bold text-end" colspan="3">TOTAL</td> <td class="border-0 fw-bold text-end" colspan="3">TOTAL</td>
<td class="border-0 fw- bold text-end" colspan="3">{{ total | usd }}</td> <td class="border-0 fw- bold text-end" colspan="3">{{ total }}</td>
</tr> </tr>
</tfoot> </tfoot>
</table> </table>