Compare commits

...

4 Commits

Author SHA1 Message Date
kukemuna 46fffb7da2 automated commit by submit50 2024-04-26 09:03:03 +03:00
kukemuna 5a47056e0a automated commit by check50 [check50=True] 2024-04-26 08:57:48 +03:00
kukemuna 8b9c1adadc automated commit by check50 [check50=True] 2024-04-26 08:38:56 +03:00
kukemuna b9530e6711 automated commit by check50 [check50=True] 2024-04-26 08:18:04 +03:00
7 changed files with 66 additions and 31 deletions

85
app.py
View File

@ -41,22 +41,30 @@ 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"]
stock["total"] = usd(stock_price["price"] * stock["sum(shares)"])
# Get total value of owned stock
stock["total"] = stock_price["price"] * stock["sum(shares)"]
# Get value of all owned stocks
stocks_total = stocks_total + (stock_price["price"] * stock["sum(shares)"])
total = usd(cash[0]["cash"] + stocks_total)
# Get total value of stocks and cash
total = cash[0]["cash"] + stocks_total
return render_template("home.html", cash=usd(cash[0]["cash"]), stocks=stocks, total=total)
return render_template(
"home.html", cash=cash[0]["cash"], stocks=stocks, total=total
)
# return apology("MOFO")
@ -68,7 +76,7 @@ def buy():
return render_template("buy.html")
else:
symbol = request.form.get("symbol")
shares = float(request.form.get("shares"))
shares = request.form.get("shares")
if not symbol:
return apology("Not Symbol")
@ -78,26 +86,40 @@ def buy():
if stock == None:
return apology("Symbol not found")
if not shares == "":
transaction_value = float(shares) * stock["price"]
if not shares or shares.isalpha() or not float(shares).is_integer():
return apology("invalid shares")
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"]
else:
if float(shares) > 0:
transaction_value = float(shares) * stock["price"]
if user_cash < transaction_value:
return apology("U broke, m8!")
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"]
free_cash = user_cash - transaction_value
if user_cash < transaction_value:
return apology("U broke, m8!")
db.execute("UPDATE users SET cash = ? WHERE id = ?", free_cash, user_id)
free_cash = user_cash - transaction_value
date = datetime.datetime.now()
db.execute("UPDATE users SET cash = ? WHERE id = ?", free_cash, user_id)
db.execute("INSERT INTO transactions (user_id, symbol, shares, price, date) VALUES (?, ?, ?, ?, ?)",
user_id, stock["symbol"], shares, stock["price"], date)
date = datetime.datetime.now()
flash("Bought!")
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)
return redirect("/")
# return apology("TODO")
@ -208,11 +230,13 @@ 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")
@ -238,7 +262,10 @@ 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")
@ -254,8 +281,14 @@ 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!")
@ -263,6 +296,8 @@ 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.

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="text">
<input class="form-control mx-auto w-auto" autocomplete="off" autofocus min="1" name="shares" placeholder="Shares" type="number">
</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 }}</td>
<td class="text-end">{{ stock.price | usd}}</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 }}</td>
<td class="text-end">{{ stock.total }}</td>
<td class="text-end">{{ stock.price | usd }}</td>
<td class="text-end">{{ stock.total | usd }}</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 }}</td>
<td class="border-0 text-end" colspan="3">{{ cash | usd }}</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 }}</td>
<td class="border-0 fw- bold text-end" colspan="3">{{ total | usd }}</td>
</tr>
</tfoot>
</table>