def viewapikey(name, password): if not Account.login(name, password): msg = "Invalid login credentials, pls retry" else: pk = Account.login(name, password).pk retrieve_ak = Account(pk=pk) msg = "Your API Key = {}".format(retrieve_ak.get_account().api_key) return jsonify({'message': msg})
def viewbalance(name, password): if not Account.login(name, password): msg = "Invalid login credentials, pls retry" else: pk = Account.login(name, password).pk retrieve_bal = Account(pk=pk) msg = "Your current balance = {}".format( retrieve_bal.get_account().balance) return jsonify({'message': msg})
def deposit(name, password, amount): if not Account.login(name, password): msg = "Invalid login credentials, pls retry" else: pk = Account.login(name, password).pk account_deposit = Account(pk=pk) new_bal = account_deposit.deposit(float(amount)) account_deposit.save() msg = "New Balance = {}".format(new_bal) return jsonify({'message': msg})
def buy(name, password, ticker, shares): if not Account.login(name, password): msg = "Invalid login credentials, pls retry" else: pk = Account.login(name, password).pk buy_txn = Account(pk=pk) # buy_txn.buy(ticker, shares) # msg = "Buy transaction complete" msg = buy_txn.buy(ticker, shares) return jsonify({'message': msg})
def sell(name, password, ticker, shares): if not Account.login(name, password): msg = "Invalid login credentials, pls retry" else: pk = Account.login(name, password).pk sell_txn = Account(pk=pk) # sell_txn.sell(ticker, shares) # msg = "Sell transaction complete" msg = sell_txn.sell(ticker, shares) return jsonify({'message': msg})
def positions(name, password, ticker): if not Account.login(name, password): msg = "Invalid login credentials, pls retry" else: pk = Account.login(name, password).pk user_position = Account(pk=pk) position = user_position.get_position_for(ticker) valuation = Position() getval = valuation.current_value(ticker, position.shares) msg = "Ticker Symbol: {}, Shares: {}, Valuation: ${}".format( position.ticker, position.shares, getval) return jsonify({'message': msg})
def trades(name, password, ticker): if not Account.login(name, password): msg = "Invalid login credentials, pls retry" else: pk = Account.login(name, password).pk user_trades = Account(pk=pk) trades = user_trades.get_trades_for(ticker) msg = {'trades': []} for trade in trades: msg['trades'].append("Date/Time: {}, Ticker Symbol: {}, No. of Shares: {}, Price per Share: {}". \ format((datetime.fromtimestamp(trade.time) - \ timedelta(hours=2)).strftime('%Y-%m-%d %H:%M:%S'), \ trade.ticker, trade.volume, trade.price)) return jsonify({'message': msg})
def allpositions(name, password): if not Account.login(name, password): msg = "Invalid login credentials, pls retry" else: pk = Account.login(name, password).pk user_positions = Account(pk=pk) positions = user_positions.get_positions() msg = {'positions': []} for position in positions: valuation = Position() getval = valuation.current_value(position.ticker, position.shares) msg['positions'].append( "Ticker Symbol: {}, Shares: {}, Valuation: ${}".format( position.ticker, position.shares, getval)) return jsonify({'message': msg})
def login_menu(): while True: try: user_input = view.login_menu() if int(user_input) == 3: view.program_end() sys.exit() elif int(user_input) == 2: username = view.username_inpt() pwd = util.hash_pass(view.password_inpt()) user = Account.login(username, pwd) if user == None: view.invalid_info() return login_menu() return user elif int(user_input) == 1: user = Account() user.username = view.username_inpt() user.set_password(util.hash_pass(view.set_password_inpt())) user.balance = view.deposit_inpt() user.save() view.acc_created(user.username) return user except ValueError: view.choose_valid()
def welcome_homepage(): while True: selection = view.welcome_screen() if selection not in ["1","2","3"]: view.improper_selection() continue if selection == "1": username, balance, password, confirm_password = view.get_username(), view.add_balance(), view.get_password(), view.confirm_password() if password != confirm_password: view.improper_password() continue if not balance.isdigit() or int(balance) < 0: view.improper_balance() continue account = Account(username = username, balance = balance) hashed_pw = Account.set_password(account, password) account.save() logged_in_homepage(account) return elif selection == "2": username, password = view.get_username(), view.get_password() logged_in_account = Account.login(username=username, password=password) if logged_in_account: logged_in_homepage(logged_in_account) return else: print("Invalid credentials supplied") continue elif selection == "3": view.goodbye() return
def get_api_key(): if not request.json or 'username' not in request.json or 'password' not in request.json: return jsonify({"error": "bad request"}), 400 account = Account.login(request.json['username'], request.json['password']) if not account: return jsonify({"error": "access denied"}) return jsonify({"username": account.username, "api_key": account.api_key})
def login(): username = view.get_username() password = view.get_password() account = Account.login(username, password) if account: mainmenu(account) else: view.bad_input()
def user_login(): username, password = view.user_login( ) #pass username and password from view.user_login user_account = Account.login( username, password ) #passes entered username and password to login check in account class print(user_account) if user_account == False: #checks user print("invalid login") username, password = view.user_login() user_account = Account.login( username, password ) #passes username and password to login function in account class else: print("Welcome: ", user_account.username) login_loop( user_account ) #if user passes login check will loop to following login menu function
def login(): if not request.json or 'email' not in request.json or 'password' not in request.json: return jsonify(BAD_REQUEST), 401 account = Account.login(request.json['email'], request.json['password']) if not account: return jsonify(UNAUTHORIZED), 401 token = encodeAuthToken(account.pk) return jsonify({'status': 'success', 'auth_token': str(token)})
def get_api_key(): if not request.json or 'username' not in request.json or\ 'password' not in request.json: return jsonify(BADREQUEST), 400 account = Account.login(request.json['username'], request.json['password']) if not account: return jsonify(UNAUTHORIZED), 401 return jsonify({ 'username': account.username, 'api_key': account.api_key })
def login_account(): loginaccount = Account() view.enter_user_name() username = input() view.enter_password() password = getpass.getpass() enteraccount = loginaccount.login(username, password) if enteraccount is None: view.login_failed_message() return login_menu() elif enteraccount is not None: return main_menu(enteraccount)
def main_loop(): while True: choice = user_menu() if choice is None: # incorrect selection bad_input('Please select option') elif choice == '4': # exit goodbye() break elif choice == '1': login_input = login_username_pw() verified_account = Account.login(login_input) if verified_account: db_password_hash = verified_account[2] password_verify = checkpw(login_input[1].encode(), db_password_hash) if password_verify: account = Account(account_id = verified_account[0]) account.username = verified_account[1] account.balance = int(verified_account[3]) while True: choice = main_menu() if choice is None: bad_input('Please select option') elif choice == '1': # view account balance display_balance(account.balance) elif choice == '2': # deposit funds account.balance += deposit() Account.update_balance(account) elif choice == '3': # buy stock buy_stock = trade_stock() account.buy(buy_stock[0], buy_stock[1]) elif choice == '4': #sell stock buy_stock = trade_stock() account.sell(buy_stock[0], buy_stock[1]) elif choice == '5': # view positions display_positions(Account.get_positions(account)) elif choice == '6': # view trades display_trade_history(Account.get_trades(account)) elif choice == '7': # lookup price of stock ticker = view.lookup_ticker() print("Ticker: {} is currently: ${}".format(ticker, get_price(ticker))) elif choice == '8': # See API Keys display_api(Account.retrieve_api_key(account)) elif choice == '9': # logout goodbye() else: bad_input('Retry') else: bad_input('Incorrect password') else: bad_input('Incorrect username') elif choice == '2': # login with api user_api_key = login_api_key() # print(user_api_key) user = Account.api_authenticate(user_api_key) display_user_from_api_key(user) elif choice == '3': # create new account account_details[0] = "mark" account_details[1] = "1234" account_details[2] = 1000 account_details = create_account() account = Account() account.username = account_details[0] account.password_hash = account_details[1] account.balance = account_details[2] account.api_key = account.generate_api_key() Account.save(account)
def main(): while True: selection = views.welcome_menu() if selection is None: #Bad input views.generic_msg( "Please enter a number that corresponds to a stated option") elif selection == 3: #Exit views.generic_msg("Bye: Thanks for using Terminal Trader") break elif selection == 1: #Create account new_username = views.get_input("Username") new_password = views.get_input("Password") new_account = Account(username=new_username) new_account.set_password(new_password) new_account.save() elif selection == 2: #Login login_username = views.get_input("Username") login_password = views.get_input("Password") if not Account.login(login_username, login_password): views.generic_msg( "Failed to find an account for the given Username/Password, pls retry" ) else: #Retrieve account balance & pk for re-use balance = Account() current_bal, pk = balance.get_bal(login_username, login_password) while True: choice = views.main_menu() if choice is None: #Bad input views.generic_msg( "Please enter a number that corresponds to a stated option" ) elif choice == 7: #Exit break elif choice == 1: #View Balance & Positions views.generic_msg( "Your current balance = {}".format(current_bal)) while True: position_choice = views.position_menu() if position_choice is None: #Bad input views.generic_msg( "Please enter a number that corresponds to a stated option" ) elif position_choice == 3: #Exit break elif position_choice == 1: #Retrieve and display a given position, taking in a ticker symbol ticker = views.get_input( "Please enter a Ticker Symbol") user_position = Account(pk=pk) position = user_position.get_position_for( ticker) views.show_positions(position) elif position_choice == 2: #Retrieve and display all positions user_positions = Account(pk=pk) positions = user_positions.get_positions() for position in positions: views.show_positions(position) elif choice == 2: #Deposit money deposit_amount = float( views.get_input("Deposit Amount")) account_deposit = Account() new_bal = account_deposit.deposit( login_username, login_password, deposit_amount) account_deposit.save() views.generic_msg("New Balance = {}".format(new_bal)) elif choice == 3: #Look up stock price ticker = views.get_input( "Please enter a Ticker Symbol") print(util.get_price(ticker)) elif choice == 4: #Buy stock ticker = views.get_input( "Please enter a Ticker Symbol") shares_buy = views.get_input( "Please enter the number of shares to buy") elif choice == 5: #Sell stock ticker = views.get_input( "Please enter a Ticker Symbol") shares_sell = views.get_input( "Please enter the number of shares to sell") elif choice == 6: #View Trade History while True: trade_choice = views.trades_menu() if trade_choice is None: #Bad input views.generic_msg( "Please enter a number that corresponds to a stated option" ) elif trade_choice == 3: #Exit break elif trade_choice == 1: #Retrieve and display trades re a give ticker symbol ticker = views.get_input( "Please enter a Ticker Symbol") user_trade = Account(pk=pk) trades = user_trade.get_trades_for(ticker) views.show_trades(trades) elif trade_choice == 2: #Retrieve and display all trades user_trades = Account(pk=pk) trades = user_trades.get_trades() for trade in trades: views.show_trades(trade)
def testLogin(self): greg = Account.login("Greg", "1234") self.assertIsNotNone(greg) self.assertIsInstance(greg, Account)
def main(): while True: views.generic_msg("Welcome to Terminal Trader !") selection = views.welcome_menu() if selection is None: #Bad input views.generic_msg( "Please enter a number that corresponds to a stated option") elif selection == 3: #Exit views.generic_msg("Bye: Thanks for using Terminal Trader") break elif selection == 1: #Create account new_username = views.get_input("Username") new_password = views.get_input("Password") new_account = Account(username=new_username) new_account.set_password(new_password) new_account.save() elif selection == 2: #Login login_username = views.get_input("Username") login_password = views.get_input("Password") if not Account.login(login_username, login_password): views.generic_msg( "Failed to find an account for the given Username/Password, pls retry" ) else: #Store account pk for re-use pk = Account.login(login_username, login_password).pk while True: choice = views.main_menu() if choice is None: #Bad input views.generic_msg( "Please enter a number that corresponds \ to a stated option") elif choice == 8: #Exit break elif choice == 1: #View Balance & Positions positions_sub_menu(pk) elif choice == 2: #Deposit money deposit_amount = float( views.get_input("Deposit Amount")) account_deposit = Account(pk=pk) new_bal = account_deposit.deposit(deposit_amount) account_deposit.save() views.generic_msg("New Balance = {}".format(new_bal)) elif choice == 3: #Look up stock price ticker = views.get_input( "Please enter a Ticker Symbol") quote = util.get_price(ticker) if not quote: views.generic_msg( "The Ticker Symbol entered does not exist") else: views.stock_price(ticker, quote) elif choice == 4: #Look up ticker from Co. name co_name = views.get_input("Please enter Company Name") companies = util.get_ticker(co_name) if not companies: views.generic_msg( "No matches for input Company Name") else: for co in companies: views.show_companies(co) elif choice == 5: #Buy stock ticker = views.get_input( "Please enter a Ticker Symbol") shares_buy = views.get_input( "Please enter the number of shares to buy") buy_txn = Account(pk=pk) buy_txn.buy(ticker, shares_buy) views.generic_msg("Buy transaction complete") elif choice == 6: #Sell stock ticker = views.get_input( "Please enter a Ticker Symbol") shares_sell = views.get_input( "Please enter the number of shares to sell") sell_txn = Account(pk=pk) sell_txn.sell(ticker, shares_sell) views.generic_msg("Sell transaction complete") elif choice == 7: #View Trade History trades_sub_menu(pk)