Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def pulling_portfolio(rows, cash):
- """Creating a searcher to rearrange the transactions into a portfolio"""
- share_cache = []
- """Creating a filtered copy of the list"""
- for i in range(0, len(rows)):
- if len(share_cache) < 1:
- share_cache.append(rows[i].copy())
- else:
- for j in range(0, len(share_cache)):
- if rows[i]["symbol"] == share_cache[j]["symbol"]:
- break
- else:
- share_cache.append(rows[i].copy())
- '''Setting every quantity to 0 to avoid doubles'''
- for i in range(0, len(share_cache)):
- share_cache[i]["quantity"] = 0
- """Summing the quantity out of the transactions"""
- for i in range(0, len(share_cache)):
- cache = share_cache[i]["symbol"]
- for j in range(0, len(rows)):
- if rows[j]["symbol"] == cache:
- share_cache[i]["quantity"] += rows[j]["quantity"]
- """Adding Price and Value to the list"""
- for i in range(0, len(share_cache)):
- cache = share_cache[i]["symbol"]
- cache_dict = lookup(cache)
- share_cache[i]["price"] = cache_dict["price"]
- share_cache[i]["value"] = float(share_cache[i]["price"]) * float(share_cache[i]["quantity"])
- return share_cache
- def buy():
- """Buy shares of stock."""
- if request.method == "POST":
- symbol = request.form.get("symbol")
- try:
- quantity_buy = int(request.form.get("shares"))
- except:
- try:
- quantity_buy = int(request.form.get("shares_buy"))
- except:
- return apology("Please check your input: METHOD Error")
- quote_buy = lookup(symbol)
- if not quote_buy:
- return apology("Yo die Quote geht nicht")
- for key, value in quote_buy.items():
- if isinstance(quote_buy[key], type(None)):
- return apology("Fehler ist im quote_buy")
- user_id = session.get("user_id")
- if not isinstance(quantity_buy, int) or len(symbol) < 1 or quantity_buy < 1:
- return apology("Please check your input: INPUT ERROR")
- elif not quote:
- return apology("Insert a valid symbol")
- try:
- try:
- stock_price = quote_buy["price"] * quantity_buy
- cash_cache = db.execute("SELECT cash FROM users WHERE id = :id", id = user_id)
- if (isinstance(cash_cache[0]["cash"], type(None))):
- return apology("Fehler ist im cash_cache")
- old_cash = cash_cache[0]["cash"]
- new_cash = old_cash - stock_price
- except:
- return apology("Here is the BUG")
- if new_cash < 0:
- return apology("Sorry you have not enough cash!")
- else:
- try:
- transaction_completed = db.execute("UPDATE users SET cash = :newcash WHERE id = :sessionid", newcash = new_cash, sessionid = user_id)
- result = db.execute("INSERT INTO transactions (id, type, symbol, quantity, current_price) VALUES (:id, :type, :quote, :quantity, :current_price)", id = user_id, type = "buy", quote = quote_buy["name"], quantity = quantity_buy, current_price = stock_price *-1)
- except:
- return apology("Problem mit updaten der DBs")
- if not transaction_completed or not result:
- return apology("Your transaction could not be finished.")
- else:
- return redirect(url_for("index"))
- except:
- return apology("Oops, something went wrong")
- else:
- return render_template("buy.html")
- @app.route("/index")
- @login_required
- def index():
- """Show a Dashboard."""
- user_id = session.get("user_id")
- rows = db.execute("SELECT symbol, quantity FROM transactions WHERE id = :uid", uid = user_id)
- cash = db.execute("SELECT cash FROM users WHERE id = :uid", uid = user_id)
- share_cache = pulling_portfolio(rows, cash)
- """Calculating the current value of all the shares"""
- try:
- total = 0
- for i in range(0, len(share_cache)):
- total += share_cache[i]["value"]
- share_cache[i]["price"] = usd(share_cache[i]["price"])
- share_cache[i]["value"] = usd(share_cache[i]["value"])
- total += cash[0]["cash"]
- return render_template("index.html", table_content = share_cache, total = usd(total), total_cash = usd(cash[0]["cash"]))
- except:
- return apology("Your portfolio cannot be displayed")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement