Answer
The error was in my index()
implementation. I had made the mistake of including the following code in index()
:
# Query database stocks database for total number of shares of each stock owned by user
securities = db.execute("SELECT symbol, SUM(shares) FROM stock WHERE userid = :userid GROUP BY symbol",
userid=session["user_id"])
if not securities:
return apology("error retrieving securities", 400)
I now realize that the errors were because of this last if
statement, which I didn't notice because I was logging in with an account that had securities purchased! Removing this if
statement eliminated the error.
Thanks all!
Original query
I am having difficulty with check50
's validation of my code.
In my initial version, the final line of code in the register()
and login()
functions for POST was:
return redirect("/")
Running check50
, I got the following errors:
:( registering user succeeds
expected status code 200, but got 400
Log
sending POST request to /register
checking that status code 200 is returned...:( registration rejects duplicate username
expected status code 200, but got 400
Log
sending POST request to /register
checking that status code 200 is returned...
I managed to "fix" the issue per
PSET7 Finance - check50 error by rendering a blank html
page with return render_template("blank.html")
at the end of the register()
function for POST, rather than using return redirect("/")
. For clarity, I have created a blank.html
page, which is simply a blank page. Making this change to register()
resulted in check50
no longer returning an error message in the registration validation. NOTE Once I fixed index()
per above, this change wasn't needed.
Now check50
gives me the following error:
:( logging in as registered user succceeds
expected status code 200, but got 400
Log
sending POST request to /login
checking that status code 200 is returned...
sending GET request to /
checking that status code 200 is returned...
I tried to similarly replace return redirect("/")
at the end of the login()
function for POST with return render_template("blank.html")
. This did not eliminate this check50
error.
Any insight would be greatly appreciated!
Here is the current version of my register()
and login()
functions:
@app.route("/login", methods=["GET", "POST"])
def login():
"""Log user in"""
# Forget any user_id
session.clear()
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Ensure username was submitted
if not request.form.get("username"):
return apology("must provide username", 403)
# Ensure password was submitted
elif not request.form.get("password"):
return apology("must provide password", 403)
# Query database for username
rows = db.execute("SELECT * FROM users WHERE username = :username",
username=request.form.get("username"))
# Ensure username exists and password is correct
if len(rows) != 1 or not check_password_hash(rows[0]["hash"], request.form.get("password")):
return apology("invalid username and/or password", 403)
# Remember which user has logged in
session["user_id"] = rows[0]["id"]
# Redirect user to home page
return redirect("/")
# User reached route via GET (as by clicking a link or via redirect)
else:
return render_template("login.html")
@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user."""
# Forget any user_id
session.clear()
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Ensure username was submitted
if not request.form.get("username"):
return apology("must provide username", 400)
# Ensure password and confirmation were submitted
if not request.form.get("password") or not request.form.get("confirmation"):
return apology("must provide password", 400)
# Ensure password and confirmation are the same
if not request.form.get("password") == request.form.get("confirmation"):
return apology("password and password confirmation must match", 400)
# Insert username into database and check for error
result = db.execute("INSERT INTO users (username, hash) VALUES (:username, :passwordhash)",
username=request.form.get("username"),
passwordhash=generate_password_hash(request.form.get("password"),
method='pbkdf2:sha256', salt_length=8))
if not result:
return apology("username already taken, choose a different username", 400)
# Remember which user has logged in
session["user_id"] = result
# Load blank page (redirect will make check50 fail) -- would prefer to use return redirect("/")
return render_template("blank.html")
# User reached route via GET (as by clicking a link or via redirect)
else:
return render_template("register.html")
Here is my code for index()
:
@app.route("/")
@login_required
def index():
"""Show portfolio of stocks"""
# Query database stocks database for total number of shares of each stock owned by user
securities = db.execute("SELECT symbol, SUM(shares) FROM stock WHERE userid = :userid GROUP BY symbol",
userid=session["user_id"])
if not securities:
return apology("error retrieving securities", 400)
# Add current price and total value to each row; calculate total securities value
securities_value = 0.0
for security in securities:
# try 10 times to get quote -- often no value is returned
for i in range(0, 10):
quote_value = lookup(security["symbol"])
if not quote_value == None:
break
if quote_value == None:
security["current_price"] = 0
else:
security["current_price"] = round(float(quote_value["price"]), 2)
security["total_value"] = round(security["current_price"] * int(security["SUM(shares)"]), 2)
securities_value += security["total_value"]
# Query database for current cash balance
rows = db.execute("SELECT * FROM users WHERE id = :userid", userid=session["user_id"])
if not rows:
return apology("error retreiving cash balance", 400)
cash_balance = rows[0]["cash"]
# Calculate total portfolio value
portfolio_value = round(securities_value + cash_balance, 2)
return render_template("index.html", securities=securities, securities_value=securities_value, cash_balance=cash_balance,
portfolio_value=portfolio_value)
index
?