Skip to main content
2 of 5
added 1080 characters in body

Problem Set 8 / Web Track - Finance (Redirect Error)

My functions seens to work fine, but when i have that redirect the user to the homepage (redirect("/")) the application crash and show the error message: 500 internal server error and I don't know if I'm wrong in the function (def index()) or in the redirect("/"). How may i solve this?? Thanks in advance. Here is my fully code (i don't even know how to publish a code here sorry):

import os

from cs50 import SQL
from flask import Flask, flash, jsonify, redirect, render_template, request, session
from flask_session import Session
from tempfile import mkdtemp
from werkzeug.exceptions import default_exceptions, HTTPException, InternalServerError
from werkzeug.security import check_password_hash, generate_password_hash

from helpers import apology, login_required, lookup, usd

# Configure application
app = Flask(__name__)

# Ensure templates are auto-reloaded
app.config["TEMPLATES_AUTO_RELOAD"] = True

# Ensure responses aren't cached
@app.after_request
def after_request(response):
    response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
    response.headers["Expires"] = 0
    response.headers["Pragma"] = "no-cache"
    return response

# Custom filter
app.jinja_env.filters["usd"] = usd

# Configure session to use filesystem (instead of signed cookies)
app.config["SESSION_FILE_DIR"] = mkdtemp()
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)

# Configure CS50 Library to use SQLite database
db = SQL("sqlite:///finance.db")

# Make sure API key is set
if not os.environ.get("API_KEY"):
    raise RuntimeError("API_KEY not set")


@app.route("/")
@login_required
def index():
    """Show portfolio of stocks"""

    user_id= session["user_id"]

    users = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=user_id)
    stocks = db.execute("SELECT symbol, SUM(shares) as total_shares FROM transactions WHERE user_id = :user_id GROUP BY symbol HAVING total_shares > 0", user_id=user_id)
    quotes = {}

    for stock in stocks:
        quotes[stock["symbol"]] = lookup(stock["symbol"])

    cash_remaining = users[0]["cash"]
    total = cash_remaining

    return render_template("index.html", quotes=quotes, stocks=stocks, total=total, cash_remaining=cash_remaining)


@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
    """Buy shares of stock"""

    if request.method == "POST":
        symbol = request.form.get("symbol")
        shares = request.form.get("shares")
        quote = lookup(symbol)
        user_id = session["user_id"]

        if quote == None:
            return apology("invalid symbol", 400)

        if not isinstance(shares, int):
            return apology("input is not a positive integer", 400)

        if shares <= 0:
            return apology("can't buy a negative number of shares or 0 share", 400)

        rows = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=user_id)

        total_cash = rows[0]["cash"]
        price = quote["price"]

        cost = price * shares

        if cost > total_cash:
            return apology("you don't have enough funds")

        db.execute("UPDATE users SET cash = cash - :price WHERE id = :user_id", price=cost, user_id=user_id)
        db.execute("INSERT INTO transactions (user_id, symbol, shares, price) VALUES(:user_id, :symbol, :shares, :price)", user_id=user_id, symbol=symbol, shares=shares, price=price)

        flash("Bought!")
        return redirect("/")

    else:
        return render_template("buy.html")


@app.route("/history")
@login_required
def history():
    """Show history of transactions"""

    user_id=session["user_id"]

    transactions = db.execute("SELECT symbol, shares, price_per_share, created_at FROM transactions WHERE user_id = :user_id ORDER BY created_at ASC", user_id=user_id)

    return render_template("history.html", transactions=transactions)


@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("/logout")
def logout():
    """Log user out"""

    # Forget any user_id
    session.clear()

    # Redirect user to login form
    return redirect("/")


@app.route("/quote", methods=["GET", "POST"])
@login_required
def quote():
    """Get stock quote."""

    symbol = request.form.get("symbol")

    if request.method == "POST":
        quote = lookup(symbol)

        if quote == None:
            return apology("invalid symbol", 400)

        return render_template("quoted.html", quote=quote)

    else:
        return render_template("quote.html")

@app.route("/register", methods=["GET", "POST"])
def register():
    """Register user"""

    if request.method == 'POST':

        username = request.form.get('username')
        password = generate_password_hash(request.form.get('password'))
        confirmation = request.form.get("confirmation")

        if not username:
            return apology("must provide username", 400)

        elif not request.form.get("password"):
            return apology("must provide password", 400)

        elif not confirmation:
            return apology("must provide equal password", 400)

        elif request.form.get("password") != confirmation:
            return apology("passwords do not match", 400)

        double_username= db.execute("SELECT username FROM users where username = :username", username=username)
        if double_username:
            return apology("username already taken", 400)

        row = db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)", username=username, hash=password)
        session["user_id"] = row
        flash("Registered!")
        return redirect("/")

    else:
        return render_template("register.html")

@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
    """Sell shares of stock"""

    if request.method == "POST":
        symbol = request.form.get = ("symbol")
        shares = request.form.get("shares")
        user_id=session["user_id"]
        quote = lookup(symbol)

        if quote == None:
            return apology("invalid symbol", 400)

        if quote == None:
            return apology("invalid symbol", 400)

        if not isinstance(shares, int):
            return apology("input is not a positive integer", 400)

        if shares <= 0:
            return apology("can't have a negative number of shares or 0 share", 400)

        stock = db.execute("SELECT SUM(shares) as total_shares FROM transactions WHERE user_id = :user_id AND symbol = :symbol GROUP BY symbol", user_id=user_id, symbol=symbol)

        if len(stock) != 1 or stock[0]["total_shares"] <= 0 or stock[0]["total_shares"] < shares:
            return apology("can't sell a negative number of shares or 0 share or more than you own", 400)

        rows = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=user_id)

        total_cash = rows[0]["cash"]
        price = quote["price"]

        cost = price * shares

        db.execute("UPDATE users SET cash = cash + :price WHERE id = :user_id", price=cost, user_id=user_id)
        db.execute("INSERT INTO transactions (user_id, symbol, shares, price) VALUES(:user_id, :symbol, :shares, :price)", user_id=user_id, symbol=symbol, shares=-shares, price=price)

        flash("Sold!")
        return redirect("/")

    else:
        stocks = db.execute("SELECT symbol, SUM(shares) as total_shares FROM transactions WHERE user_id = :user_id GROUP BY symbol HAVING total_shares > 0", user_id=user_id)

        return render_template("sell.html", stocks=stocks)


def errorhandler(e):
    """Handle error"""
    if not isinstance(e, HTTPException):
        e = InternalServerError()
    return apology(e.name, e.code)


# Listen for errors
for code in default_exceptions:
    app.errorhandler(code)(errorhandler)