mirror of https://github.com/me50/kukemuna.git
automated commit by submit50
This commit is contained in:
parent
5a47056e0a
commit
46fffb7da2
50
app.py
50
app.py
|
|
@ -41,7 +41,9 @@ 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
|
||||
|
||||
|
|
@ -60,13 +62,15 @@ def index():
|
|||
# Get total value of stocks and cash
|
||||
total = cash[0]["cash"] + stocks_total
|
||||
|
||||
return render_template("home.html", cash=cash[0]["cash"], stocks=stocks, total=total)
|
||||
return render_template(
|
||||
"home.html", cash=cash[0]["cash"], stocks=stocks, total=total
|
||||
)
|
||||
# return apology("MOFO")
|
||||
|
||||
|
||||
@app.route("/buy", methods=["GET", "POST"])
|
||||
@login_required
|
||||
def buy():#
|
||||
def buy():
|
||||
"""Buy shares of stock"""
|
||||
if request.method == "GET":
|
||||
return render_template("buy.html")
|
||||
|
|
@ -90,7 +94,9 @@ def buy():#
|
|||
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_db = db.execute(
|
||||
"SELECT cash FROM users WHERE id = ?", user_id
|
||||
)
|
||||
user_cash = user_cash_db[0]["cash"]
|
||||
|
||||
if user_cash < transaction_value:
|
||||
|
|
@ -102,8 +108,14 @@ def buy():#
|
|||
|
||||
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:
|
||||
|
|
@ -220,8 +232,11 @@ def register():
|
|||
if username_exists:
|
||||
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")
|
||||
|
|
@ -247,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")
|
||||
|
|
@ -263,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!")
|
||||
|
||||
|
|
@ -272,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.
Binary file not shown.
Binary file not shown.
|
|
@ -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>
|
||||
|
|
|
|||
Loading…
Reference in New Issue