Skip to main content
added 108 characters in body
Source Link

Issue: Unable to get the output or the dropdown list of bought share in my portfolio. I get a stack trace. enter image description here

Issue: Unable to get the output or the dropdown list of bought share in my portfolio.

Issue: Unable to get the output or the dropdown list of bought share in my portfolio. I get a stack trace. enter image description here

added 122 characters in body
Source Link
# SELL
@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
    """Sell shares of stock"""
    if request.method == "POST":
        # ensure proper symbol
        stock = lookup(request.form.get("symbol"))
        if not stock:
            return apology("Missing Symbol")

        # ensure password was submitted
        elif not request.form.get("shares"):
            return apology("Missing shares")

        else:
            shares = request.form.get("shares")

            # query database for user's cash
            cash = db.execute("SELECT cash FROM users WHERE id = :id", id = session["user_id"])
            cash = cash[0]["cash"]


            # select the symbol shares of that user
            user_shares = db.execute("SELECT shares FROM portfolio WHERE id = :id AND symbol=:symbol", \
                                 id=session["user_id"], symbol=stock["symbol"])

            stocks = db.execute("SELECT shares, symbol FROM portfolio WHERE id=:id", id=session["user_id"])


            # check if enough shares to sell
            if user_shares[0]["shares"] < shares:
                return apology("Too many shares")

            # update  of a sell
            db.execute("INSERT INTO history (symbol, shares, price, id) \
                    VALUES(:symbol, :shares, :price, :id)", \
                    symbol=stock["symbol"], shares=-shares, \
                    price=usd(stock["price"]), id=session["user_id"])

            # update user cash (increase)
            db.execute("UPDATE users SET cash = cash + :cash_after WHERE id = :id", \
                    id=session["user_id"], \
                    cash_after=stock["price"] * float(shares))

            # decrement the shares count
            shares_total = user_shares[0]["shares"] - shares

            # if after decrement is zero, delete shares from portfolio
            if shares_total == 0:
                db.execute("DELETE FROM portfolio \
                        WHERE id=:id AND symbol=:symbol", \
                        id=session["user_id"], \
                        symbol=stock["symbol"])
            # otherwise, update portfolio shares count
            else:
                db.execute("UPDATE portfolio SET shares=:shares \
                    WHERE id=:id AND symbol=:symbol", \
                    shares=shares_total, id=session["user_id"], \
                    symbol=stock["symbol"])

            # flash bought alert
            flash('Sold!')

            # Redirect user to home page
            return redirect("/")

    else:
        # If request method GET
        return render_template("sell.html", cash = cash[0]["cash"], stocks=stocks)
# SELL
@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
    """Sell shares of stock"""
    if request.method == "POST":
    # ensure proper symbol
    stock = lookup(request.form.get("symbol"))
    if not stock:
        return apology("Missing Symbol")

    # ensure password was submitted
    elif not request.form.get("shares"):
        return apology("Missing shares")

    else:
        shares = request.form.get("shares")

        # query database for user's cash
        cash = db.execute("SELECT cash FROM users WHERE id = :id", id = session["user_id"])
        cash = cash[0]["cash"]


        # select the symbol shares of that user
        user_shares = db.execute("SELECT shares FROM portfolio WHERE id = :id AND symbol=:symbol", \
                                 id=session["user_id"], symbol=stock["symbol"])

        stocks = db.execute("SELECT shares, symbol FROM portfolio WHERE id=:id", id=session["user_id"])


        # check if enough shares to sell
        if user_shares[0]["shares"] < shares:
            return apology("Too many shares")

        # update  of a sell
        db.execute("INSERT INTO history (symbol, shares, price, id) \
                    VALUES(:symbol, :shares, :price, :id)", \
                    symbol=stock["symbol"], shares=-shares, \
                    price=usd(stock["price"]), id=session["user_id"])

        # update user cash (increase)
        db.execute("UPDATE users SET cash = cash + :cash_after WHERE id = :id", \
                    id=session["user_id"], \
                    cash_after=stock["price"] * float(shares))

        # decrement the shares count
        shares_total = user_shares[0]["shares"] - shares

        # if after decrement is zero, delete shares from portfolio
        if shares_total == 0:
            db.execute("DELETE FROM portfolio \
                        WHERE id=:id AND symbol=:symbol", \
                        id=session["user_id"], \
                        symbol=stock["symbol"])
        # otherwise, update portfolio shares count
        else:
            db.execute("UPDATE portfolio SET shares=:shares \
                    WHERE id=:id AND symbol=:symbol", \
                    shares=shares_total, id=session["user_id"], \
                    symbol=stock["symbol"])

        # flash bought alert
        flash('Sold!')

        # Redirect user to home page
        return redirect("/")

else:
    # If request method GET
    return render_template("sell.html", cash = cash[0]["cash"], stocks=stocks)
# SELL
@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
    """Sell shares of stock"""
    if request.method == "POST":
        # ensure proper symbol
        stock = lookup(request.form.get("symbol"))
        if not stock:
            return apology("Missing Symbol")

        # ensure password was submitted
        elif not request.form.get("shares"):
            return apology("Missing shares")

        else:
            shares = request.form.get("shares")

            # query database for user's cash
            cash = db.execute("SELECT cash FROM users WHERE id = :id", id = session["user_id"])
            cash = cash[0]["cash"]


            # select the symbol shares of that user
            user_shares = db.execute("SELECT shares FROM portfolio WHERE id = :id AND symbol=:symbol", \
                                 id=session["user_id"], symbol=stock["symbol"])

            stocks = db.execute("SELECT shares, symbol FROM portfolio WHERE id=:id", id=session["user_id"])


            # check if enough shares to sell
            if user_shares[0]["shares"] < shares:
                return apology("Too many shares")

            # update  of a sell
            db.execute("INSERT INTO history (symbol, shares, price, id) \
                    VALUES(:symbol, :shares, :price, :id)", \
                    symbol=stock["symbol"], shares=-shares, \
                    price=usd(stock["price"]), id=session["user_id"])

            # update user cash (increase)
            db.execute("UPDATE users SET cash = cash + :cash_after WHERE id = :id", \
                    id=session["user_id"], \
                    cash_after=stock["price"] * float(shares))

            # decrement the shares count
            shares_total = user_shares[0]["shares"] - shares

            # if after decrement is zero, delete shares from portfolio
            if shares_total == 0:
                db.execute("DELETE FROM portfolio \
                        WHERE id=:id AND symbol=:symbol", \
                        id=session["user_id"], \
                        symbol=stock["symbol"])
            # otherwise, update portfolio shares count
            else:
                db.execute("UPDATE portfolio SET shares=:shares \
                    WHERE id=:id AND symbol=:symbol", \
                    shares=shares_total, id=session["user_id"], \
                    symbol=stock["symbol"])

            # flash bought alert
            flash('Sold!')

            # Redirect user to home page
            return redirect("/")

    else:
        # If request method GET
        return render_template("sell.html", stocks=stocks)
deleted 148 characters in body
Source Link
# SELL
@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
    """Sell shares of stock"""
    if request.method == "POST":
    # ensure ifproper notsymbol
    stock = lookup(request.form.get("symbol"):)
    if not stock:
        return apology("Missing symbol"Symbol")

    # ensure password was submitted
    elif not request.form.get("shares"):
        return apology("Missing shares")

    #else:
 Check how much cash and what stocks usershares have= request.form.get("shares")

        # query database for user's cash
        cash = db.execute("SELECT cash FROM users WHERE id=id = :id", id = session["user_id"])
        cash = cash[0]["cash"]


        # select the symbol shares id=session["user_id"])of that user
    stocks    user_shares = db.execute("SELECT shares, symbol FROM portfolio WHERE id=:id",
 id = :id AND symbol=:symbol", \
                id=session["user_id"])

    # update each symbol prices and total
    # for stock inid=session["user_id"], stocks:symbol=stock["symbol"])
   
  symbol = request.form.get("symbol")
    stockstocks = lookupdb.execute(symbol)
  "SELECT shares, symbol = stock["symbol"]
  FROM portfolio sharesWHERE =id=:id", int(stock["shares"]id=session["user_id"]) 


    price = stock["price"]
  # check totalif =enough shares *to price
sell
    db.execute("UPDATE portfolio SET price=:price, total=:total WHEREif id=:iduser_shares[0]["shares"] AND< symbol=shares:symbol",
            price=usd(stock["price"]),return total=usdapology(total),"Too id=session["user_id"],many symbol=symbolshares")

    # Calculate how many shares# ofupdate this stockof usera hassell
    share_qty =   db.execute("SELECT shares FROM"INSERT totalINTO WHEREhistory symbol=:(symbol AND id=:id",
  shares, price, id) \
                    VALUES(:symbol, :shares, symbol=symbol:price, id=session["user_id"]:id)", \
    total_share = share_qty[0]["shares"]

    # Check if user has enough shares of the stock
 symbol=stock["symbol"], shares=-shares, \
 if total_share < shares:
        return apology("You don't have enough stocks")
   price=usd(stock["price"]), else:id=session["user_id"])

        # Calcalute how much cashupdate user nowcash has(increase)
        new_cash = floatdb.execute(cash[0]["cash"])"UPDATE +users total

SET cash = cash + :cash_after WHERE id #= Log:id", in\
 updated cash
        db.execute("UPDATE users SET cash=:new_cash WHERE id=:id",
     id=session["user_id"], \
                    cash_after=stock["price"] new_cash=new_cash,* id=session["user_id"]float(shares))

        # Insertdecrement intothe hisotryshares tablecount
 to record the transaction
    shares_total = user_shares[0]["shares"] - shares

  db.execute("INSERT INTO history (symbol, shares, price, id)# VALUESif (:symbol,after :shares,decrement :price,is :id)"zero,
  delete shares from portfolio
        if shares_total == 0:
            symbol=symbol,db.execute("DELETE shares=shares,FROM price=price,portfolio id=session["user_id"])
\
        # Updating portfolio table reflecting the sell of shares
        ifWHERE total_shareid=:id ==AND sharessymbol=:
 symbol", \
           db.execute("DELETE FROM portfolio WHERE symbol=:symbol AND id=:id",
       id=session["user_id"], \
                       symbol=symbol, id=session["user_id"]symbol=stock["symbol"])
        else:
   # otherwise, update portfolio shares count
    new_total = total_share - shareselse:
            db.execute("UPDATE portfolio SET shares=:new_shareshares \
                    WHERE id=:id"id AND symbol=:symbol", \
                    shares=shares_total, id=session["user_id"], \
          new_share=new_total, id=session["user_id"]         symbol=stock["symbol"])

        # Flashflash soldbought alert
        flash('Sold!')

        # Redirect user to indexhome page
        return redirect("/")

else:
    # If request method GET
 else:
    return render_template("sell.html", cash=cash[0]["cash"]cash = cash[0]["cash"], stocks=stocks)
# SELL
@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
    """Sell shares of stock"""
  if request.method == "POST":
      if not request.form.get("symbol"):
          return apology("Missing symbol")

    # ensure password was submitted
    elif not request.form.get("shares"):
        return apology("Missing shares")

    # Check how much cash and what stocks user have
    cash = db.execute("SELECT cash FROM users WHERE id=:id",
                   id=session["user_id"])
    stocks = db.execute("SELECT shares, symbol FROM portfolio WHERE id=:id",
                      id=session["user_id"])

    # update each symbol prices and total
    # for stock in stocks:
    symbol = request.form.get("symbol")
    stock = lookup(symbol)
    symbol = stock["symbol"]
    shares = int(stock["shares"])
    price = stock["price"]
    total = shares * price

    db.execute("UPDATE portfolio SET price=:price, total=:total WHERE id=:id AND symbol=:symbol",
            price=usd(stock["price"]), total=usd(total), id=session["user_id"], symbol=symbol)

    # Calculate how many shares of this stock user has
    share_qty = db.execute("SELECT shares FROM total WHERE symbol=:symbol AND id=:id",
                           symbol=symbol, id=session["user_id"])
    total_share = share_qty[0]["shares"]

    # Check if user has enough shares of the stock
    if total_share < shares:
        return apology("You don't have enough stocks")
    else:
        # Calcalute how much cash user now has
        new_cash = float(cash[0]["cash"]) + total

        # Log in updated cash
        db.execute("UPDATE users SET cash=:new_cash WHERE id=:id",
                           new_cash=new_cash, id=session["user_id"])

        # Insert into hisotry table to record the transaction
        db.execute("INSERT INTO history (symbol, shares, price, id) VALUES (:symbol, :shares, :price, :id)",
                            symbol=symbol, shares=shares, price=price, id=session["user_id"])

        # Updating portfolio table reflecting the sell of shares
        if total_share == shares:
             db.execute("DELETE FROM portfolio WHERE symbol=:symbol AND id=:id",
                               symbol=symbol, id=session["user_id"])
        else:
            new_total = total_share - shares
            db.execute("UPDATE portfolio SET shares=:new_share WHERE id=:id",
                                new_share=new_total, id=session["user_id"])

        # Flash sold alert
        flash('Sold!')

        # Redirect to index page
        return redirect("/")

 # If request method GET
 else:
    return render_template("sell.html", cash=cash[0]["cash"], stocks=stocks)
# SELL
@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
    """Sell shares of stock"""
    if request.method == "POST":
    # ensure proper symbol
    stock = lookup(request.form.get("symbol"))
    if not stock:
        return apology("Missing Symbol")

    # ensure password was submitted
    elif not request.form.get("shares"):
        return apology("Missing shares")

    else:
        shares = request.form.get("shares")

        # query database for user's cash
        cash = db.execute("SELECT cash FROM users WHERE id = :id", id = session["user_id"])
        cash = cash[0]["cash"]


        # select the symbol shares of that user
        user_shares = db.execute("SELECT shares FROM portfolio WHERE id = :id AND symbol=:symbol", \
                                 id=session["user_id"], symbol=stock["symbol"])
 
        stocks = db.execute("SELECT shares, symbol FROM portfolio WHERE id=:id", id=session["user_id"]) 


        # check if enough shares to sell
        if user_shares[0]["shares"] < shares:
            return apology("Too many shares")

        # update  of a sell
        db.execute("INSERT INTO history (symbol, shares, price, id) \
                    VALUES(:symbol, :shares, :price, :id)", \
                    symbol=stock["symbol"], shares=-shares, \
                    price=usd(stock["price"]), id=session["user_id"])

        # update user cash (increase)
        db.execute("UPDATE users SET cash = cash + :cash_after WHERE id = :id", \
                    id=session["user_id"], \
                    cash_after=stock["price"] * float(shares))

        # decrement the shares count
        shares_total = user_shares[0]["shares"] - shares

        # if after decrement is zero, delete shares from portfolio
        if shares_total == 0:
            db.execute("DELETE FROM portfolio \
                        WHERE id=:id AND symbol=:symbol", \
                        id=session["user_id"], \
                        symbol=stock["symbol"])
        # otherwise, update portfolio shares count
        else:
            db.execute("UPDATE portfolio SET shares=:shares \
                    WHERE id=:id AND symbol=:symbol", \
                    shares=shares_total, id=session["user_id"], \
                    symbol=stock["symbol"])

        # flash bought alert
        flash('Sold!')

        # Redirect user to home page
        return redirect("/")

else:
    # If request method GET
    return render_template("sell.html", cash = cash[0]["cash"], stocks=stocks)
added 2 characters in body
Source Link
Loading
deleted 105 characters in body
Source Link
Loading
deleted 105 characters in body
Source Link
Loading
added 184 characters in body
Source Link
Loading
Source Link
Loading