0

def sell():

"""Sell shares of stock"""

# method check
if request.method == "GET":
    return render_template("sell.html")
else:
    # valid name
    info = lookup(request.form.get("symbol"))
    if not info:
        return apology("invalid stock symbol")

# validate shares
    shares = int(request.form.get("shares"))
    if shares < 0:
        return apology("enter valid no. of shares to be sold", 403)

# getting the proper shares from users account

    user_shares = db.execute("SELECT shares FROM log WHERE id = :id AND symbol=:symbol",
                             id=session["user_id"], symbol=info["symbol"])

# validate shares in hand
    if not user_shares or int(user_shares[0]["shares"]) < shares:
        return apology("insufficient no. of shares")

# adding the transaction in history

    db.execute("INSERT INTO history (symbol, shares, price, id, moment) VALUES(:symbol, :shares, :price, :id, :moment)",
               symbol=info["symbol"], shares=-shares, price=usd(info["price"]), id=session["user_id"],
               moment=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

# update cash in hand after selling
    db.execute("UPDATE users SET cash = cash + :purchase WHERE id = :id",
               id=session["user_id"], purchase=info["price"] * float(shares))

# updating new shares in hand

    total_shares = user_shares[0]["shares"] - shares

# now if shares are 0 after selling delete it from account else update all the info

    if total_shares == 0:
        db.execute("DELETE FROM log WHERE id=:id AND symbol=:symbol", id=session["user_id"], symbol=info["symbol"])

    else:
        db.execute("UPDATE log SET shares=:shares WHERE id=:id AND symbol=:symbol",
                   shares=total_shares, id=session["user_id"], symbol=info["symbol"])

    return redirect("/")

check50: :| sell page has all required elements can't check until a frown turns upside down :| sell handles invalid number of shares can't check until a frown turns upside down :| sell handles valid sale can't check until a frown turns upside down

1

Those errors all mean that there's an earlier test that failed and has to be corrected before the remaining tests can run.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .