0

I am getting this message in the following checks

  • :| sell page has all required elements/
  • :| sell handles invalid number of shares
  • :| sell handles valid sale

I guess the problem is in by sell or buy functions or history functions since most of the previous checks went well.

Thanks in advanced!

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

    buy_db = db.execute("SELECT DISTINCT(Symbol) FROM buy WHERE user_id = :user_id",
                                user_id=session["user_id"])
    """Sell shares of stock"""
    if request.method == "POST":

        if not request.form.get("symbol"):
            return apology("must enter a symbol", 400)

        elif not bool(lookup(request.form.get("symbol"))):

            return apology("symbol does not exists", 400)

        elif not request.form.get("shares"):
            return apology("must enter a quantity of shares", 400)

        elif not request.form.get("shares").isnumeric():
            return apology("mus enter a integer number", 400)

        elif int(request.form.get("shares")) < 0:
            return apology("must enter a positive number", 400)

        else:

            buy_db = db.execute("SELECT SUM(Shares) as Shares, Symbol FROM buy WHERE user_id = :user_id AND Symbol = :symbol GROUP BY user_id",
                                user_id=session["user_id"], symbol = request.form.get("symbol"))
            if len(buy_db) == 0:
                return apology("you do not own any share of this company", 400)

            elif buy_db[0]["Shares"] < int(request.form.get("shares")):
                return apology("you do not own enough shares of this company", 400)

            else:
                 owned_shares = buy_db[0]["Shares"]
                 to_be_sold = int(request.form.get("shares"))
                 price = lookup(request.form.get("symbol"))['price']

                 db.execute("INSERT INTO buy (user_id, Symbol, Shares, Price) VALUES (?, ?, ?, ?)",
                           session["user_id"], request.form.get("symbol"), to_be_sold * -1, price)

            #return render_template("test.html", buy_db = buy_db)
            return redirect("/")

    else:
        return render_template("sell.html", buy_db = buy_db)

--

def buy():
    """Buy shares of stock"""
    if request.method == "POST":
        if not request.form.get("symbol"):
            return apology("must enter a symbol", 400)

        elif not bool(lookup(request.form.get("symbol"))):

            return apology("symbol does not exists", 400)

        elif not request.form.get("shares"):
            return apology("must enter a quantity of shares", 400)

        elif not request.form.get("shares").isnumeric():
            return apology("mus enter a integer number", 400)

        elif int(request.form.get("shares")) < 0:
            return apology("must enter a positive number", 400)

        #    symbol = request.form.get("symbol")
        #    shares = request.form.get("shares")
        #    price = lookup(request.form.get("symbol"))['price']

        else:
            price = lookup(request.form.get("symbol"))['price']

            cash = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=session["user_id"])[0]['cash']

            shares = int(request.form.get("shares"))

            user_id = session["user_id"]

            if shares*price > cash:
                return apology("there is no enough cash", 403)
            else:
                price = usd(price)
                db.execute("INSERT INTO buy (user_id, Symbol, Shares, Price) VALUES (?, ?, ?, ?)",
                           session["user_id"], request.form.get("symbol"), shares, price)
                return redirect("/")

            # return render_template("test.html", price = type(price), cash = type(cash), shares = type(shares),user_id = user_id)

    else:
        return render_template("buy.html")

-- @app.route("/history") @login_required def history(): """Show history of transactions""" buy_db = db.execute("SELECT * FROM buy WHERE user_id = :user_id", user_id=session["user_id"])

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

Here is the complete response I am getting

:) application.py exists
Log
checking that application.py exists...
--
:) application starts up
Log
sending GET request to /
checking that status code 200 is returned...
--
:) register page has all required elements
Log
sending GET request to /register
found required "username" field
found required "password" field
found required "confirmation" field
--
:) registering user succeeds
Log
sending POST request to /register
checking that status code 200 is returned...
--
:) registration with an empty field fails
Log
sending POST request to /register
checking that status code 400 is returned...
sending POST request to /register
checking that status code 400 is returned...
sending POST request to /register
checking that status code 400 is returned...
--
:) registration with password mismatch fails
Log
sending POST request to /register
checking that status code 400 is returned...
--
:) registration rejects duplicate username
Log
sending POST request to /register
checking that status code 200 is returned...
sending POST request to /register
checking that status code 400 is returned...
--
:) login page has all required elements
Log
sending GET request to /login
found required "username" field
found required "password" field
--
:) logging in as registered user succceeds
Log
sending POST request to /login
checking that status code 200 is returned...
sending GET request to /
checking that status code 200 is returned...
--
:) quote page has all required elements
Log
sending POST request to /login
sending GET request to /quote
found required "symbol" field
--
:) quote handles invalid ticker symbol
Log
sending POST request to /login
sending POST request to /quote
checking that status code 400 is returned...
--
:) quote handles blank ticker symbol
Log
sending POST request to /login
sending POST request to /quote
checking that status code 400 is returned...
--
 :) quote handles valid ticker symbol
Log
sending POST request to /login
sending POST request to /quote
checking that status code 200 is returned...
checking that "28.00" is in page
--
:) buy page has all required elements
Log
sending POST request to /login
sending GET request to /buy
found required "symbol" field
found required "shares" field
--
:) buy handles invalid ticker symbol
Log
sending POST request to /login
sending POST request to /buy
checking that status code 400 is returned...
--
:) buy handles fractional, negative, and non-numeric shares
Log
sending POST request to /login
sending POST request to /buy
checking that status code 400 is returned...
sending POST request to /buy
checking that status code 400 is returned...
sending POST request to /buy
checking that status code 400 is returned...
--
:( buy handles valid purchase
Cause
expected to find "112.00" in page, but it wasn't found
Log
sending POST request to /login
sending POST request to /buy
checking that "112.00" is in page
--
:| sell page has all required elements
Cause
can't check until a frown turns upside down
:| sell handles invalid number of shares
Cause
can't check until a frown turns upside down
:| sell handles valid sale
Cause
can't check until a frown turns upside down

My sell.html id down here:

  {% extends "layout.html" %}

{% block title %}
    Sell
{% endblock %}

{% block main %}
    <form action="/sell" method="post">

        <div class="form-group">
        <select name = "symbol">
        <option disabled selected value = "Symbol" value ="">Symbol</option>
        {%for row in buy_db %}

        <option value = {{row.Symbol}}>{{row.Symbol}}</option>
        {% endfor %}
        </select>
        </div>
        <div class="form-group">
            <input autocomplete = "off" class="form-control"  name="shares" placeholder="Shares" type="number" min = 0 >
        </div>

        <button class="btn btn-primary" type="submit">Sell</button>
    </form>
{% endblock %}
3
  • ":| sell page has all required elements/" sounds like the problem is with sell.html. Make sure it conforms to the first 2 bullets of the sell spec Sep 6 at 15:39
  • If a bullet-point from Check50 begins with " :| ", it means that the specified aspect your code is not necessarily failing but it can't be checked because something earlier on in the process is failing (indicated using " :( ". So I'd suggest posting the full report from Check50, including the " :( ". Someone might be able to help then. That person won't be me, as I haven't finished Finance yet :-P Sep 6 at 15:42
  • @DinoCoderSaurus I have updated my question so that my html file is there. Could you please check it out Sep 8 at 17:27
1

Have a look at what your index renders if user buys 2 shares of something. I suspect it will show the price twice and not "total value for each holding".

The usd function returns a string, so price will be treated by database as a string because price = usd(price). (sqlite uses dynamic typing, more detail can be found in the doc)

python will gladly do multiplication with a string and an integer. (see week 6 python video from the 1:36:00 mark)

It is better to store price in the database as a numeric value because it will be needed in calculations.

Ulitmately you want the value of the transaction (price * shares) to be in usd, not simply the price. If the calculation is being done in the index route, something like usd(price * shares) should do the trick.

Or it can be done in the template (index.html) according to this spec hint:

To format a value as a US dollar value (with cents listed to two decimal places), you can use the usd filter in your Jinja templates (printing values as {{ value | usd }} instead of {{ value }}

--original--

The first failure ("sad face") is :( buy handles valid purchase....expected to find "112.00" in page, but it wasn't found. More often than not, that indicates a problem with index, usually the format of "total value for each holding"; ie not using usd

5
  • Thanks a lot. It did solve my quote problem but I havent been able to get it work for buy. I have updated my question so now it includes my buy function. Would you mind checking it out? I have included the usd function now. Sep 8 at 22:28
  • Answer has been updated. Sep 9 at 11:29
  • Thanks!! I worked. Got full scored!! I appreciate it Sep 10 at 3:28
  • Hey DinoCoderSaurus, I was wondering how would use the Jinja templates (printing values as {{ value | usd }} is there were2 variables to pass on the funciton! thanks in advance Sep 11 at 17:52
  • You mean like {{ (price * shares) | usd }}? Sep 12 at 21:18

You must log in to answer this question.

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