def delete_class(): if request.method == "GET": return render_template('delete_class.html', portfolio_class=session.get('portfolio_class')) if request.method == "POST": # for test account - don't do anything if session.get('userid') == test_account_userid: return redirect("/class_and_tickers") classname = request.form.get("classname") # check that user select real class if classname == "": return error_page("ERROR!\nYou should choose class for deletion.") # change classname to None for all tickers in this class portfolio_ticker = session.get('portfolio_ticker') not_string = "" for ticker in portfolio_ticker: if portfolio_ticker[ticker]['classname'] == classname: not_string += " " + ticker ticker_db.query.filter_by(userid=session.get('userid'), ticker=ticker).update({ 'classname': 'None'}) db.session.commit() # delete this class from db class_db.query.filter_by(userid=session.get('userid'),classname=classname).delete(synchronize_session='evaluate') db.session.commit() # reload info in session load_portfolio_info(session.get('userid'), ticker_db, cash_db, class_db, user_db, False) if len(not_string) >= 1: return error_page('Class has been deleted.\n Class deletion resulted in None class for next tickers:' + not_string) return redirect('/class_and_tickers')
def delete_ticker(): if request.method == "GET": return render_template('delete_ticker.html', portfolio_ticker=session.get('portfolio_ticker')) if request.method == "POST": # for test account - don't do anything if session.get('userid') == test_account_userid: return redirect("/class_and_tickers") # load ticker name ticker = request.form.get("ticker") # check for empty input field if ticker == "": return error_page("ERROR!\nYou should choose ticker for deletion.") # check if it is active ticker for some class portfolio_class = session.get('portfolio_class') for classname in portfolio_class: if portfolio_class[classname]['activeticker']==ticker: # put None in active ticker cell for this class class_db.query.filter_by(userid=session.get('userid'), classname=classname).update({ 'activeticker': 'None'}) db.session.commit() # delete this ticker from db ticker_db.query.filter_by(userid=session.get('userid'),ticker=ticker).delete(synchronize_session='evaluate') db.session.commit() # reload info in session load_portfolio_info(session.get('userid'), ticker_db, cash_db, class_db, user_db, False) return redirect('/class_and_tickers')
def add_class(): if request.method == "GET": return render_template('add_class.html') if request.method == "POST": # for testoaccount - don't do anything if session.get('userid') == test_account_userid: return redirect("/class_and_tickers") classname = request.form.get("classname") # check: is it new name for class if session.get('portfolio_class') is not None: for name in session.get('portfolio_class'): if name == classname: return error_page('ERROR.\nSuch class exists! Choose another name.') # check for spaces in the name for i in classname: if ord(i) == 32: return error_page('ERROR.\nPlease, do not use space in the name!') # #check, that name consists of letters only # if classname.isalpha() == False: # return error_page('Class name should consist of letters only!') # # # check that name consist of english letter only # eng_alphabet=("abcdefghijklmnopqrstuvwxyz") # for one_char in classname.lower(): # if one_char not in eng_alphabet: # return error_page('Use only latin letters!') # load last id from class_db and put new id by hand (to avoid IntegrityError duplicate key violates unique-constraint) max_id = class_db.query.order_by(class_db.id.desc()).first().id # change portfolio new_row = class_db(id=max_id+1, userid=session.get('userid'), classname=classname, fraction=0, diapason=0, activeticker="None") db.session.add(new_row) db.session.commit() # reload new portfolio in session if session.get('portfolio_ticker') is None: # load new prices load_portfolio_info(session.get('userid'), ticker_db, cash_db, class_db, user_db, True) else: # use prices from session load_portfolio_info(session.get('userid'), ticker_db, cash_db, class_db, user_db, False) return redirect("/class_and_tickers")
def change_ticker_info(): if request.method == "GET": # create dict of id : ticker + '_' + currency|class ids = {} idtag = ['currency', 'classname'] for tck in session.get('portfolio_ticker'): ids[tck] = {} for tag in idtag: ids[tck].update({tag: tag + "_" + tck}) return render_template('tickers_change.html', portfolio_ticker=session.get('portfolio_ticker'), portfolio_class=session.get('portfolio_class'), ids=ids ) if request.method == "POST": # for test account - don't do anything if session.get('userid') == test_account_userid: return redirect("/class_and_tickers") portfolio_ticker = session.get("portfolio_ticker") for tck in portfolio_ticker: # load new currency and class from website tag = 'currency_' + tck new_currency = request.form.get(tag) tag = 'classname_' + tck new_class = request.form.get(tag) old_class = portfolio_ticker[tck]['classname'] # if class was changed, check if it was active ticker if new_class != old_class and old_class != 'None': portfolio_class = session.get('portfolio_class') if portfolio_class[old_class]['activeticker']==tck: # put None in active ticker cell for this class class_db.query.filter_by(userid=session.get('userid'), classname=old_class).update({ 'activeticker': 'None'}) db.session.commit() # save new values in db ticker_db.query.filter_by(userid=session.get('userid'), ticker=tck).update({ 'currency': new_currency, 'classname': new_class }) db.session.commit() # reload portfolio load_portfolio_info(session.get('userid'), ticker_db, cash_db, class_db, user_db, False) return redirect('/class_and_tickers')
def add_ticker(): if request.method == "GET": return render_template('add_ticker.html', portfolio_class=session.get('portfolio_class') ) if request.method == "POST": # for test account - don't do anything if session.get('userid') == test_account_userid: return redirect("/class_and_tickers") # check new ticker and load ticker price ticker = request.form.get("newticker") ticker_info = apiprice(ticker) if ticker_info['price'] == 0: print("apology") return error_page("Error! Could not load price for such ticker. Probably, ticker name is not correct!") # load other info about this ticker if session.get('portfolio_class') is not None: classname = request.form.get("classname") else: classname = 'None' currency = request.form.get('currency') # check that this ticker is not in portfolio datas = ticker_db.query.filter_by(userid=session.get('userid'),ticker=ticker).all() print(f"check the db for such ticker {datas}") if len(datas) != 0: return error_page("You already have such ticker!") # load last id from ticker_db and put new id by hand (to avoid IntegrityError duplicate key violates unique-constraint) max_id = ticker_db.query.order_by(ticker_db.id.desc()).first().id # change portfolio new_row = ticker_db(id=max_id+1, userid=session.get('userid'), ticker=ticker, number=0,classname=classname,currency=currency ) db.session.add(new_row) db.session.commit() # reload new portfolio in session load_portfolio_info(session.get('userid'), ticker_db, cash_db, class_db, user_db, True) return redirect("/class_and_tickers")
def cash(): if request.method == "GET": # check user in session if session.get('userid') is None: return render_template("index_intro.html") return render_template('cash.html', portfolio_cash=session.get('portfolio_cash'), exchange=session.get('exchange') ) if request.method == "POST": # for test account - don't do anything if session.get('userid') == test_account_userid: return redirect("/cash") if request.form.get('cashvalue') == "": return error_page('Input window was empty.') print('get new cash values from user') # value from cash page cash = float(request.form.get('cashvalue')) currency = request.form.get('currency') # value from cash in session oldcash = session.get('portfolio_cash') print(f"old cash is {oldcash}") newcash = 100*(oldcash[currency] + cash) # in case of decreasing of cash - check do we have such money if newcash < 0: return error_page("You don't have enough cash.") # change cash db cash_db.query.filter_by(userid=session.get('userid')).update({currency:newcash}) db.session.commit() # reload portfolio load_portfolio_info(session.get('userid'), ticker_db, cash_db, class_db, user_db, False) return redirect('/cash')
def index_page(): if request.method == "GET": # load_portfolio_info(userid, ticker_db, cash_db, class_db,user_db, True) if session.get('userid') is None: return render_template("index_intro.html") else: userid = session.get('userid') #check session for portfolio information if session.get('portfolio_ticker') is None: boolres = load_portfolio_info(userid, ticker_db, cash_db, class_db,user_db, True) # case of empty portfolio if boolres == False: return redirect('/create_portfolio') else: # check the date of last loading of prices # find day, month, year year_month_day = "" for i in session.get('datetime'): if i == " ": break else: year_month_day += i # if prices wasn't load today - refresh them if year_month_day != time.strftime("%Y-%m-%d"): load_portfolio_info(userid, ticker_db, cash_db, class_db, user_db, True) symbols = {"USD": '$', "EUR": '€'} main_currency = session.get('main_currency') chart_data = prepare_data_for_chart() # for user WITH PORTFOLIO return render_template('index.html', portfolio_ticker=session.get('portfolio_ticker'), portfolio_cash=session.get('portfolio_cash'), portfolio_class=session.get('portfolio_class'), total=session.get('total'), total_cash=session.get('total_cash'), suggestion=session.get('suggestion'), date=datetime.strptime(session.get('datetime'), "%Y-%m-%d %H:%M"), symbol=symbols, main_currency=main_currency, chart_data=chart_data, recommendation=session.get('recommendation') ) if request.method == "POST": if request.form.get("refresh") is not None: print("refreshing page") boolres = load_portfolio_info(session.get('userid'), ticker_db, cash_db, class_db, user_db, True) if boolres == False: return redirect('/create_portfolio') return redirect("/") if request.form.get("change_currency") is not None: session['main_currency'] = request.form.get('change_currency') # change values in user db user_db.query.filter_by(userid=session.get('userid')).update({ 'currency': request.form.get('change_currency'), }) db.session.commit() return redirect("/")
def change_class_info(): if request.method == "GET": portfolio_class = session.get('portfolio_class') # create dict of id : classname +_realfraction /fraction_diap / active ticker ids = {} idtag = ['fraction','diapason','activeticker','name'] for key in portfolio_class: ids[key] = {} for tag in idtag: ids[key].update({tag: tag + "_" + key}) print(ids) return render_template('classes_change.html', portfolio_class=portfolio_class, portfolio_ticker=session.get('portfolio_ticker'), ids = ids ) if request.method == "POST": if request.form.get("submit") is not None: # for test account - don't do anything if session.get('userid') == test_account_userid: return redirect("/class_and_tickers") portfolio_class = session.get("portfolio_class") for classname in portfolio_class: # load classname from website tag = 'name_' + classname new_classname = request.form.get(tag) if new_classname != classname: # check for spaces in the name for i in new_classname: if ord(i) == 32: return error_page('Please, do not use space in the name!') # check: is it new name for class if session.get('portfolio_class') is not None: for name in session.get('portfolio_class'): if name == new_classname: return error_page('Such class exists! Choose another name.') # change classname in ticker db ticker_db.query.filter_by(userid=session.get('userid'), classname=classname).update({ 'classname': new_classname }) # load new fraction from website tag = 'fraction_' + classname new_fraction = request.form.get(tag) #load new diapason from website tag = 'diapason_' + classname new_diapason = request.form.get(tag) # load new active ticker tag = 'activeticker_' + classname new_activeticker = request.form.get(tag) # save new values in db class_db.query.filter_by(userid=session.get('userid'),classname=classname).update({ 'classname': new_classname, 'fraction': new_fraction, 'diapason' : new_diapason, 'activeticker' : new_activeticker }) db.session.commit() # reload portfolio load_portfolio_info(session.get('userid'), ticker_db, cash_db, class_db, user_db, False) return redirect("/class_and_tickers")
def rebalance(): if request.method == "GET": # check user in session if session.get('userid') is None: return render_template("index_intro.html") # check nonempty portfolio if session.get('portfolio_ticker') is None: return redirect('/create_portfolio') # create dict of id ids = {} idtag = ['price', 'newnumber', 'oldnumber', 'classname','exchange'] for ticker in session.get("portfolio_ticker"): ids[ticker] = {} for tag in idtag: ids[ticker].update({tag: tag + "_" + ticker}) # calc fraction for cash total = session.get("total") if total['USD'] != 0: cash_fraction = round(100 * session.get('total_cash')['USD'] / total['USD']) else: cash_fraction = 100 # list of classes classname_list = ['None'] for classname in session.get("portfolio_class"): classname_list = classname_list + [classname] symbols = {"USD": '$', "EUR": '€'} return render_template("rebalance.html", portfolio_ticker=session.get("portfolio_ticker"), portfolio_class=session.get('portfolio_class'), suggestion=session.get('suggestion'), total=total, total_cash=session.get('total_cash'), date=datetime.strptime(session.get('datetime'), "%Y-%m-%d %H:%M"), classname_list=classname_list, main_currency=session.get('main_currency'), exchange=session.get('exchange'), cash_fraction=cash_fraction, symbols=symbols, ids=ids ) if request.method == "POST": # for test account - don't do anything if session.get('userid') == test_account_userid: return redirect("/") portfolio_cash = session.get('portfolio_cash') portfolio_ticker = session.get('portfolio_ticker') # check that we have enough cash # calculate cash changes for all tickers in ticker currency for ticker in portfolio_ticker: # load new number and price new_number = float(request.form.get('newnumber_' + ticker)) price = float(request.form.get('price_' + ticker)) old_number = portfolio_ticker[ticker]['number'] currency = portfolio_ticker[ticker]['currency'] # change cash in currency of ticker portfolio_cash[currency] += (old_number - new_number) * price # load new values in ticker_db ticker_db.query.filter_by(userid=session.get('userid'), ticker=ticker).update({ 'number': new_number }) db.session.commit() # load new cash values in db cash_db.query.filter_by(userid=session.get('userid')).update({ 'USD': portfolio_cash['USD'], 'EUR' : portfolio_cash['EUR'], 'RUB': portfolio_cash['RUB'] }) db.session.commit() # reload portfolio in session load_portfolio_info(session.get('userid'), ticker_db, cash_db, class_db, user_db, False) # check for negative cash for key in portfolio_cash: if portfolio_cash[key] < 0: flash("You have negative cash. You need to exchange some of you currency. Go to /cash page ") return redirect("/")