0

I tried to implement sell, but there's a problem in the index template that came up when the website tried to redirect from sell.html to index.html after the sail of stocks was done.

For reference, here's the sell route:

@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
    """Sell shares of stock."""
    if request.method == "POST":
        stock = lookup(request.form.get("symbol"))
        if not stock:
            return apology("invalid symbol!")

        stock_query = db.execute("SELECT stock_symbol FROM transactions WHERE user_id=:uid", uid=session["user_id"])
        for i in range(len(stock_query)):
            if stock_query[i]["stock_symbol"] != stock["symbol"]:
                return apology("You don't have any shares of that stock!")

        share_query = db.execute("SELECT no_of_shares FROM transactions WHERE user_id=:uid", uid=session["user_id"])
        for i in range(len(share_query)):
            if share_query[i]["no_of_shares"] < int(request.form.get("shares")):
                return apology("You don't have enough shares of that stock for this!")

        share_query = db.execute("SELECT no_of_shares FROM transactions WHERE user_id=:uid AND stock_symbol=:ssymbol", uid=session["user_id"], ssymbol=stock["symbol"])
        if share_query[0]["no_of_shares"] - int(request.form.get("shares")) == 0:
            share_query = db.execute("DELETE FROM transactions WHERE user_id=:uid AND stock_symbol=:ssymbol", uid=session["user_id"], ssymbol=stock["symbol"])

        user_cash = db.execute("SELECT cash FROM users WHERE id=:uid", uid=session["user_id"])
        total_cash = user_cash[0]["cash"] + (stock["price"] * int(request.form.get("shares")))
        update_query = db.execute("UPDATE users SET cash=:cash WHERE id=:uid", cash=total_cash, uid=session["user_id"])

        return render_template("index.html")

    elif request.method == "GET":
        return render_template("sell.html")

And this the sell template:

{% extends "layout.html" %}

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

{% block main %}
    <form action="{{ url_for('sell') }}" method="post">
        <fieldset>
            <div class="form-group">
                <input autocomplete="off" autofocus class="form-control" name="symbol" type="text" placeholder="Symbol"/>
            </div>
            <div class="form-group">
                <input autocomplete="off" autofocus class="form-control" name="shares" type="number" placeholder="Shares"/>
            </div>
            <div class="form-group">
                <button class="btn btn-default" type="submit">Sell</button>
            </div>
       </fieldset>
    </form>
{% endblock %}

I'd also like to know if this is the correct way to do an "update" query (for buy): db.execute("UPDATE transactions (stock_name, stock_price, stock_symbol, user_id, time, no_of_shares) VALUES(:stock_name, :stock_price, :stock_symbol, :user_id, :time, :no_of_shares)", stock_name=stock["name"], stock_price=stock["price"], stock_symbol=stock["symbol"], user_id=session["user_id"], time=time.time(), no_of_shares=user_input_shares)

Here's the index template:

{% extends "layout.html" %}

{% block title %}
    Portfolio
{% endblock %}

{% block main %}
    <table class="table table-striped">
        <thead>
            <tr>
                <th>Symbol</th>
                <th>Name</th>
                <th>Shares</th>
                <th>Price</th>
                <th>TOTAL</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <td colspan="4"></td>
                <td><b>{{ "${:,.2f}".format(grand_total) }}</b></td>
            </tr>
        </tfoot>
        <tbody>
            {% for i in transactions_info %}
            <tr>
                <td>{{ i.stock_symbol }}</td>
                <td>{{ i.stock_name }}</td>
                <td>{{ i.no_of_shares }}</td>
                <td>{{ "${:,.2f}".format(i.stock_price) }}</td>
                <td>{{ "${:,.2f}".format(i.no_of_shares * i.stock_price) }}</td>
            {% endfor %}
            </tr>
            <tr>
                <td colspan="4">CASH</td>
                <td>{{ "${:,.2f}".format(users_info[0]["cash"]) }}</td>
            </tr>
        </tbody>
    </table>
{% endblock %}

The error is for the call to format with grand_total passed to it.

Also, if there are any other mistakes I've made, please point them out to be and help me fix them.

0

Okay, never mind. I got it. I had to correctly use a filter to format the money as a float with a precision of two places after the decimal point. For that, I did this and it worked: <td>{{ "${:.2f}".format(total|float) }}</td>.

You must log in to answer this question.

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