From 8b9c1adadc57e843ff8d6482456adf5a353f6b21 Mon Sep 17 00:00:00 2001 From: kukemuna Date: Fri, 26 Apr 2024 08:38:56 +0300 Subject: [PATCH] automated commit by check50 [check50=True] --- app.py | 44 ++++++++++-------- finance.db | Bin 20480 -> 20480 bytes .../16fa13c5890feb947b5d5c907cfedf5b | Bin 46 -> 46 bytes templates/buy.html | 2 +- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/app.py b/app.py index 8a78623..cd55ffa 100644 --- a/app.py +++ b/app.py @@ -78,28 +78,32 @@ def buy(): if stock == None: return apology("Symbol not found") - if 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"] - - if 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) - - date = datetime.datetime.now() - - 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) + 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"] + + if 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) + + date = datetime.datetime.now() + + 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") diff --git a/finance.db b/finance.db index d33873659efab4581d1e87b86f79fba1219ff77b..82ffe12280571445902f1e353acbde825b3c40ff 100644 GIT binary patch delta 181 zcmZozz}T>Wae_2s+(a2?#<-0M=5lNkB4+}@=DBj=f{famzv}Y~Fl%r&PG)y-<>3nE z(&l33yv4bSvvFgiCMTb|Hai2SC?mUZfKN!!_sIg@BAgah#^ykzr!`sETSdZ5!2m2^ zWTCE!q?8Gyln*FlYGrJprU8=SoXq7d1=4L|WoV(MKDpLQk=@w9%FtX*4a8Wae_2s$V3@u#*mE(=5lQD4U4aH*>0XI7cR)Cy7{X
- +