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

View File

@ -20,7 +20,7 @@
<tr>
<td class="text-start">{{ stock.symbol }}</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>
</tr>
{% endfor%}

View File

@ -20,8 +20,8 @@
<tr>
<td class="text-start">{{ stock.symbol }}</td>
<td class="text-end">{{ stock.shares }}</td>
<td class="text-end">{{ stock.price | usd }}</td>
<td class="text-end">{{ stock.total | usd }}</td>
<td class="text-end">{{ stock.price }}</td>
<td class="text-end">{{ stock.total }}</td>
</tr>
{% endfor%}
</tbody>
@ -29,11 +29,11 @@
<tfoot>
<tr>
<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>
<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>
</tfoot>
</table>