0

I passed all the check50 tests and I am working on my personal touch component, which for me is adding buying and selling to index.

Everything works except:

  1. When more than one stock is sold/bought, the flash at the top of the page is duplicated e.g. "Bought!Bought!"
  2. If the inputs are not valid, the result is mostly correct i.e. the stock isn't sold/bought, BUT an apology is not rendered as it normally would be if using either /Buy or /Sell.

Can someone explain why this is happening to my code? Is it because in application.py in index() I am calling Buy() and Sell() instead of somehow redirecting there instead? I would prefer someone help me before I spend a bunch of time implementing the wrong solution.

Application.py POST part of index():

else:
    shares = request.form.get("shares")
    stocks = []
    
    # create list of all selected stocks and transaction type
    for i in request.form.keys():
        stocks.append(i)
    stocks.remove("shares")

    if "Buy" in stocks:
        stocks.remove("Buy")
        for i in stocks:
            buy(shares, i)

    elif "Sell" in stocks:
        stocks.remove("Sell")
        for i in stocks:
            sell(shares, i)
    
    return redirect("/")

index.html

{% extends "layout.html" %}

{% block title %}
    Index
{% endblock %}

{% block main %}
    <form action="/" method="post">
      <table class="table table-striped">
        <thead>
          <tr>
            <th scope="col"></th>
            <th scope="col">Symbol</th>
            <th scope="col">Name</th>
            <th scope="col">Shares</th>
            <th scope="col">Price</th>
            <th scope="col">Total</th>
          </tr>
        </thead>
        <tbody>
          {% for entry in stocks %}
          <tr>
              <td><input type="checkbox" name="{{ entry.ticker }}" value="{{ entry.ticker }}"></td>
              <td>{{ entry.ticker }}</td>
              <td>{{ entry.company }}</td>
              <td>{{ entry.shares }}</td>
              <td>{{ entry.price|usd }}</td>
              <td>{{ entry.cost|usd }}</td>
          </tr>
          {% endfor %}
          <tr>
              <td>CASH</td>
              <td></td>
              <td></td>
              <td></td>
              <td></td>
              <td>{{ balance|usd }}</td>
          </tr>
          <tr id="totalrow">
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td><b>{{ total|usd }}</b></td>
          </tr>
        </tbody>
      </table>
      <div class="p-3" style="display:inline">
        <input autocomplete="off" class="form-control block" name="shares" placeholder="Number of Shares" type="number" min=1 step=1 pattern="\d*">
      </div>
      <div class="p-3" style="display:inline">
        <input class="btn btn-primary" type="submit" name="Buy" value="Buy">
        <input class="btn btn-primary" type="submit" name="Sell" value="Sell">
      </div>
    </form>

{% endblock %}

Application.py Sell(shares=None, symbol=None)

For academic honesty purposes, I will exclude this. You can trust it works correctly. It has two default parameters which exist so that index() can pass on its information.

index.html

4
  • It could be "calling Buy() and Sell() instead of somehow redirecting there instead?" because the flask doc says "Flashes a message to the next request." and to my mind calling the buy/sell function is not the same as a request. Apr 30 at 13:33
  • You are right, the issue was calling the functions. I just ended up implementing a custom version of Buy() and Sell() in Index(). I thought there might have been a more efficient way of going about it.
    – Clueless
    Apr 30 at 19:18
  • The buttons in index can "call" buy or sell without the need to reimplement code. See formaction attribute of button element May 1 at 12:07
  • Yes, I am aware of the formaction attribute. I would have used that if I had a buy/sell button at the end of each row. With my approach I only have one buy/sell button, so would have to iterate through a list of stocks. Calling buy or sell iteratively is where I believe the issues rose. For example, rendering apologies and duplicate flashes.
    – Clueless
    May 2 at 22:02
0

I am not sure if this is the optimal solution, but I basically reimplemented Buy() and Sell() in Index() instead of having to call Buy() and Sell(). I used a helper function to factor out common code among the three functions.

There might have been functionality in Flask that would allow for iterative requests to Buy() and Sell(), but I couldn't get it to work as intended as described in the post.

0

You must log in to answer this question.

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