def quote(): """Get stock quote.""" if request.method == "POST": quote = lookup(request.form.get("symbol")) if quote == None: return apology("invalid symbol", 400) res = liveUpdate(request.form.get("symbol")) #print("RES") #print(res) graph = pygal.Line() graph.title = 'Updates of ' + request.form.get("symbol") graph.x_labels = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] graph.add(request.form.get("symbol") + ":high", res["high"]) graph.add(request.form.get("symbol") + ":low", res["low"]) graph_data = graph.render_data_uri() return render_template("quoted.html", quote=quote, graph_data=graph_data) # User reached route via GET (as by clicking a link or via redi) else: val = random_ads() ads1 = dict(list(val.items())[0:2]) ads2 = dict(list(val.items())[2:4]) ads3 = dict(list(val.items())[4:6]) '''print("Ads1") print(ads1) print("Ads2") print(ads2) print("Ads3") print(ads3)''' return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3)
def deleteFriend(): val = random_ads() ads1 = dict(list(val.items())[0:2]) ads2 = dict(list(val.items())[2:4]) ads3 = dict(list(val.items())[4:6]) if request.method == "POST": newid = request.form.get("friend") newid = int(newid, 10) val = db.execute("SELECT * FROM friends WHERE uid=:user_id", user_id=session["user_id"]) if not val: return apology("You haven't added any friends yet") else: if newid == val[0]["f1"]: db.execute("UPDATE friends SET f1=:f1 WHERE uid=:user_id", f1=None, user_id=session["user_id"]) return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3) if newid == val[0]["f2"]: db.execute("UPDATE friends SET f2=:f1 WHERE uid=:user_id", f1=None, user_id=session["user_id"]) return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3) if newid == val[0]["f3"]: db.execute("UPDATE friends SET f3=:f1 WHERE uid=:user_id", f1=None, user_id=session["user_id"]) return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3) if newid == val[0]["f4"]: db.execute("UPDATE friends SET f4=:f1 WHERE uid=:user_id", f1=None, user_id=session["user_id"]) return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3) if newid == val[0]["f5"]: db.execute("UPDATE friends SET f5=:f1 WHERE uid=:user_id", f1=None, user_id=session["user_id"]) return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3) return apology("Sorry, that person was not in your friend list") return render_template("deleteFriend.html", ads1=ads1, ads2=ads2, ads3=ads3)
def download(): try: return send_file('out.pdf', attachment_filename='transactions.pdf') except Exception as e: print(e) val = random_ads() ads1 = dict(list(val.items())[0:2]) ads2 = dict(list(val.items())[2:4]) ads3 = dict(list(val.items())[4:6]) return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3)
def buy(): """Buy shares of stock""" val = random_ads() ads1 = dict(list(val.items())[0:2]) ads2 = dict(list(val.items())[2:4]) ads3 = dict(list(val.items())[4:6]) if request.method == "POST": quote = lookup(request.form.get("symbol")) # Check if the symbol exists if quote == None: return apology("invalid symbol", 400) # Check if shares was a positive integer try: shares = int(request.form.get("shares")) except: return apology("shares must be a positive integer", 400) # Check if # of shares requested was 0 if shares <= 0: return apology("can't buy less than or 0 shares", 400) # Query database for username rows = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=session["user_id"]) # How much $$$ the user still has in her account cash_remaining = rows[0]["cash"] price_per_share = quote["price"] # Calculate the price of requested shares total_price = price_per_share * shares if total_price > cash_remaining: return apology("not enough funds") # Book keeping (TODO: should be wrapped with a transaction) db.execute("UPDATE users SET cash = cash - :price WHERE id = :user_id", price=total_price, user_id=session["user_id"]) db.execute( "INSERT INTO transactions (user_id, symbol, shares, price_per_share) VALUES(:user_id, :symbol, :shares, :price)", user_id=session["user_id"], symbol=request.form.get("symbol"), shares=shares, price=price_per_share) flash("Bought!") return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3) else: return render_template("buy.html", ads1=ads1, ads2=ads2, ads3=ads3)
def ads(): if request.method == 'POST': symbol = request.form.get("symbol").upper() image = request.files["image"] if image.filename == '': flash('No selected file') return redirect(request.url) if image and allowed_file(image.filename): filename = secure_filename(image.filename) image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) alttext = request.form.get("alttext") desc = request.form.get("desc") link = request.form.get("link") priority = request.form.get("priority") duration = request.form.get("duration") imagesource = "/static/images/" + image.filename try: db.execute("DELETE FROM advertisement WHERE symbol=:symbol", symbol=symbol) db.execute( "INSERT INTO advertisement(symbol,imagesource,alttext,description,link,priority,duration) VALUES (:symbol,:imagesource,:alttext,:description,:link,:priority,:duration)", symbol=symbol, imagesource=imagesource, alttext=alttext, description=desc, link=link, priority=priority, duration=duration) #Cost formula=300*priority+100*duration rows = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=session["user_id"]) money = rows[0]["cash"] #check balance if float(money) < float(priority * 300 + duration * 100): flash("Not enough money!") return render_template("ads.html") else: new_cash = money - (priority * 300 + duration * 100) db.execute("UPDATE users SET cash=:cash WHERE id=:user_id", cash=new_cash, user_id=session["user_id"]) except Exception as e: print(e) val = random_ads() ads1 = dict(list(val.items())[0:2]) ads2 = dict(list(val.items())[2:4]) ads3 = dict(list(val.items())[4:6]) return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3) return render_template("ads.html")
def messageFriend(): try: val = random_ads() ads1 = dict(list(val.items())[0:2]) ads2 = dict(list(val.items())[2:4]) ads3 = dict(list(val.items())[4:6]) if request.method == "POST": message = request.form.get("friend") password = request.form.get("password") val = db.execute("SELECT * FROM friends WHERE uid=:user_id", user_id=session["user_id"]) set1 = set() list1 = [] myEmail = "" if val[0]["f1"] is not None: set1.add(val[0]["f1"]) if val[0]["f2"] is not None: set1.add(val[0]["f2"]) if val[0]["f3"] is not None: set1.add(val[0]["f3"]) if val[0]["f4"] is not None: set1.add(val[0]["f4"]) if val[0]["f5"] is not None: set1.add(val[0]["f5"]) val = db.execute("SELECT id, email FROM users") for user in val: if user["id"] in set1: list1.append(user["email"]) if user["id"] == session["user_id"]: myEmail = user["email"] print(list1) for a in list1: sendmail(message, myEmail, a, password) return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3) except: return apology("Sorry, non-secure access to gmail not permitted") return render_template("messageFriend.html", ads1=ads1, ads2=ads2, ads3=ads3)
def sell(): """Sell shares of stock""" val = random_ads() ads1 = dict(list(val.items())[0:2]) ads2 = dict(list(val.items())[2:4]) ads3 = dict(list(val.items())[4:6]) print("Ads1") print(ads1) print("Ads2") print(ads2) print("Ads3") print(ads3) if request.method == "POST": quote = lookup(request.form.get("symbol")) # Check if the symbol exists if quote == None: return apology("invalid symbol", 400) # Check if shares was a positive integer try: shares = int(request.form.get("shares")) except: return apology("shares must be a positive integer", 400) # Check if # of shares requested was 0 if shares <= 0: return apology("can't sell less than or 0 shares", 400) # Check if we have enough shares stock = db.execute( "SELECT SUM(shares) as total_shares FROM transactions WHERE user_id = :user_id AND symbol = :symbol GROUP BY symbol", user_id=session["user_id"], symbol=request.form.get("symbol")) if len(stock) != 1 or stock[0]["total_shares"] <= 0 or stock[0][ "total_shares"] < shares: return apology("you can't sell less than 0 or more than you own", 400) # Query database for username rows = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=session["user_id"]) # How much money the user still has in her account cash_remaining = rows[0]["cash"] price_per_share = quote["price"] # Calculate the price of requested shares total_price = price_per_share * shares db.execute("UPDATE users SET cash = cash + :price WHERE id = :user_id", price=total_price, user_id=session["user_id"]) db.execute( "INSERT INTO transactions (user_id, symbol, shares, price_per_share) VALUES(:user_id, :symbol, :shares, :price)", user_id=session["user_id"], symbol=request.form.get("symbol"), shares=-shares, price=price_per_share) flash("Sold!") return redirect(url_for("index")) else: stocks = db.execute( "SELECT symbol, SUM(shares) as total_shares FROM transactions WHERE user_id = :user_id GROUP BY symbol HAVING total_shares > 0", user_id=session["user_id"]) return render_template("sell.html", ads1=ads1, ads2=ads2, ads3=ads3)
def index(): """Show portfolio of stocks""" try: # look up the current user users = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=session["user_id"]) stocks = db.execute( "SELECT symbol, SUM(shares) as total_shares FROM transactions WHERE user_id = :user_id GROUP BY symbol HAVING total_shares > 0", user_id=session["user_id"]) quotes = {} predictedPrices = db.execute( "SELECT symbol, price FROM predictions WHERE symbol IN (SELECT symbol FROM transactions WHERE user_id=:user_id)", user_id=session["user_id"]) print(predictedPrices) spmap = {} a = 0 for val in predictedPrices: spmap[val["symbol"]] = a a = a + 1 for stock in stocks: quotes[stock["symbol"]] = lookup(stock["symbol"]) if quotes[stock["symbol"]] is None: continue print(stock["symbol"]) if stock["symbol"] in spmap: quotes[stock["symbol"] + "p"] = predictedPrices[spmap[stock["symbol"]]]["price"] else: quotes[stock["symbol"] + "p"] = 0 #print(quotes) cash_remaining = users[0]["cash"] total = cash_remaining #sendmail() x = db.execute( "SELECT Symbol,SUM(ABS(shares)) as t FROM transactions WHERE created_at >= date('now','-1 day') GROUP BY symbol ORDER BY t DESC LIMIT 3" ) #print(x) if len(x) >= 3: t="Today's most trending shares are: "+x[0]["symbol"]+" with "+str(x[0]["t"])+" transactions, "+x[1]["symbol"]+" wi"\ "th "+str(x[1]["t"])+" transactions and "+x[2]["symbol"]+" with "+str(x[2]["t"])+" transactions" elif len(x) == 2: t="Today's most trending shares are: "+x[0]["symbol"]+" with "+str(x[0]["t"])+" transactions, "+x[1]["symbol"]+" wi"\ "th "+str(x[1]["t"])+" transactions" elif len(x) == 1: t = "Today's most trending shares are: " + x[0][ "symbol"] + " with " + str(x[0]["t"]) + " transactions" else: t = "Sorry, no transactions have been done yet" flash(t) if quotes is None or stocks is None: return apology( "Something went wrong, please access the page a little later") return render_template("portfolio.html", quotes=quotes, stocks=stocks, total=total, cash_remaining=cash_remaining) except Exception as e: print(e) val = random_ads() ads1 = dict(list(val.items())[0:2]) ads2 = dict(list(val.items())[2:4]) ads3 = dict(list(val.items())[4:6]) return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3)
def addFriend(): """Allows to add a friend""" val = random_ads() ads1 = dict(list(val.items())[0:2]) ads2 = dict(list(val.items())[2:4]) ads3 = dict(list(val.items())[4:6]) if request.method == "POST": newid = request.form.get("friend") newid = int(newid, 10) all_users = db.execute("SELECT id FROM users") present = 0 for user in all_users: #print(user["id"]) #print(newid) if user["id"] == newid: present = 1 break if present == 0: return apology("Sorry, we can't find that user") val = db.execute("SELECT * FROM friends WHERE uid=:user_id", user_id=session["user_id"]) #print(val); if not val: db.execute("INSERT INTO friends VALUES (:uid,:f1,:f2,:f3,:f4,:f5)", uid=session["user_id"], f1=newid, f2=None, f3=None, f4=None, f5=None) return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3) else: if val[0]["f1"] is None: db.execute("UPDATE friends SET f1=:f1 WHERE uid=:user_id", f1=newid, user_id=session["user_id"]) return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3) elif val[0]["f2"] is None: if val[0]["f1"] == newid: flash("Already Added") return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3) db.execute("UPDATE friends SET f2=:f1 WHERE uid=:user_id", f1=newid, user_id=session["user_id"]) return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3) elif val[0]["f3"] is None: if val[0]["f2"] == newid or val[0]["f1"] == newid: flash("Already Added") return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3) db.execute("UPDATE friends SET f3=:f1 WHERE uid=:user_id", f1=newid, user_id=session["user_id"]) return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3) elif val[0]["f4"] is None: if val[0]["f3"] == newid or val[0]["f2"] == newid or val[0][ "f1"] == newid: flash("Already Added") return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3) db.execute("UPDATE friends SET f4=:f1 WHERE uid=:user_id", f1=newid, user_id=session["user_id"]) return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3) elif val[0]["f5"] is None: if val[0]["f4"] == newid or val[0]["f3"] == newid or val[0][ "f2"] == newid or val[0]["f1"] == newid: flash("Already Added") return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3) db.execute("UPDATE friends SET f5=:f1 WHERE uid=:user_id", f1=newid, user_id=session["user_id"]) return render_template("quote.html", ads1=ads1, ads2=ads2, ads3=ads3) return apology("You exceeded maximum number of friends", 400) return render_template("addFriend.html", ads1=ads1, ads2=ads2, ads3=ads3)