/
main.py
247 lines (230 loc) · 8.86 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
from app import account
from app import position
from app.account import Account, InsufficientFundsError, InsufficientSharesError
from app.position import Position
from app.trade import Trade
from app import view
import settings
import bcrypt
import requests
from credentials import PUBLICKEY
from time import ctime
#to reactivate virtual enviroment: source venv/bin/activate
#to leave virtual enviroment: deactivate
def run():
while True:
user_account = login_menu() # returns the logged in user or None for quit
if user_account == None: # login menu returns None if 'quit' is selected
view.goodbye()
break
main_menu(user_account) #removed arguement user_account # when the user exits the main menu they will go back to login
def login_menu():#removed user as arg
"""This is the first menu that the user sees"""
while True:
view.print_login_menu()
choice = view.login_prompt().strip()
if choice not in ("1", "2", "3"):
view.bad_login_input()
elif choice == "3":
return None #returns none to exit function from run
elif choice == "1":#choice 1 = create account #NW added the below code in choice 1
"""calls create_account() function """
try:
create_account()
except view.UsernameUnavailableError:
view.invalid_username()
pass #needed?
elif choice == "2":
account = login()
if account:## needs to be fixed does return true need to get to main menu
return account# print('yay')
else:
pass
def main_menu(user):
while True:
if user.username == 'admin':
view.print_admin_menu(user)
choice = view.main_prompt()
else:
view.print_main_menu(user)
choice = view.main_prompt()
if choice == "1":
view.show_balance(user)#Check Balance
pass
elif choice == "2":#Withdraw Funds
amount = view.withdrawal_amount()
try:
user.withdraw(amount)#func is in account.py
view.post_withdrawal(amount, user.balance)
except ValueError:
view.not_positive()
except account.InsufficientFundsError:
view.insufficient_funds()
elif choice == "3":#Deposit Funds
amount = view.deposit_amount()
try:
user.deposit(amount)
view.post_deposit(amount, user.balance)
except ValueError:
view.not_positive()
elif choice == "4":#Trading Menu
trading_menu(user)
elif choice == "5":#Sign Out #exits to first login menu
break
elif choice == "6":#Admin - Leaderboard
if user.username == 'admin':
admin_leaderboard()
else:
view.bad_menu_input()
elif choice == "7":
if user.username == 'admin':
username = view.select_username()
toggle_admin(username)
else:
view.bad_menu_input()
else:#loops back
view.bad_menu_input()
continue
def trading_menu(user):
while True:
view.print_trading_menu(user)
choice = view.trading_menu_prompt()
if choice == "1":#GetQuote
ticker = view.ticker_prompt()
try:
quote = account.get_quote(ticker)
# print(quote) #shows whole quote as json dictionary
print(f"""
Company Name: {quote['companyName']}
Symbol: {quote['symbol']}
Latest Price: {quote['latestPrice']}
IEX Bid Price: {quote['iexBidPrice']}
IEX Ask Price: {quote['iexAskPrice']}
High: {quote['high']}
Low: {quote['low']}
52 Week High: {quote['week52High']}
52 Week Low: {quote['week52Low']}
PE Ratio {quote['peRatio']}\n\n""")
except ConnectionError:
view.connection_error()
elif choice == "2":#Buy Shares NOT COMPLETE
view.buy_intro()
ticker = view.ticker_prompt()
quantity = view.quantity()
try:
quantity = int(quantity)
if quantity < 0:
view.negative_quantity_error()#TODO: fix this so it doesn't send you back to trading menu
elif quantity == 0:
view.zero_quantity_error()
else:
try:
market_value = user.trade(ticker, quantity)
view.sucessful_buy_trade(ticker, quantity, market_value)
except InsufficientFundsError:
view.insufficient_funds()
except ConnectionError:
view.connection_error()
except ValueError:
view.not_number_error()
elif choice =="3":#Sell Shares
view.sell_intro()
ticker = view.ticker_prompt()
quantity = view.quantity()
try:
quantity = int(quantity)
if quantity < 0:
view.negative_quantity_error()#TODO: fix this so it doesn't send you back to trading menu
elif quantity == 0:
view.zero_quantity_error()
else:
quantity = quantity * -1
try:
market_value = user.trade(ticker, quantity)
view.sucessful_sell_trade(ticker, quantity, market_value)
except InsufficientSharesError:
view.insufficient_shares()
except ConnectionError:
view.connection_error()
except ValueError:
view.not_number_error()
# except account.NegativeQuantityError:
# view.negative_quantity_error()
elif choice == "4":#See Holdings
user.show_holdings_by_account()
elif choice == "5":#Show Trade History
while True:
selection = view.trade_history_prompt()
if selection.lower() == "quit":
break
elif selection.lower() == "total":
user.show_trades_by_account()
break
else:
user.show_trades_by_account_and_ticker(selection)
break
elif choice == "6":#Exit to Menu
break
else:
view.bad_menu_input()
def create_account():
view.intro_create_acc()
first = view.input_first()
last = view.input_last()
username = view.input_username()
password = view.input_password() #this is the start of what will become the password hash
balance = 0
email = view.input_email()
new_account = Account(first=first, last=last, username=username, balance=balance, email=email)
#password_hash=password_hash - not used b/c save in set pw fucntion
new_account.set_password(password) #creates the password has which is missing in the above line
if Account.from_username(username) is None:
new_account.save()#save function to put the new account into the sql db
else:
raise view.UsernameUnavailableError
def login():
username, password = view.user_login_attempt()
loaded_acct = Account.login_attempt(username, password)
if loaded_acct:
return loaded_acct #True
else:
view.invalid_credentials()
return None
def admin_leaderboard():
print('Terminal Trader Leaderboard\n')
accounts = Account.all()
account_username_pk_list = []
for account in accounts:
account_username_pk_list.append([account, account.pk, account.username])
for item in account_username_pk_list:
Account.leaderboard_stats_by_acct(item[0], item[1], item[2])
def toggle_admin(username):
acct = Account.from_username(username)
if acct.admin == 1:
view.admin_on(username)
elif acct.admin == 0:
view.admin_off(username)
while True:
choice = view.toggle()
if choice == '1':
acct.admin = 1
acct.save()
view.admin_switched_on(username)
elif choice == '0':
acct.admin = 0
view.admin_switched_off(username)
acct.save()
elif choice == 'q':
break
else:
invalid_admin_choice()
###############
run()
# create_account() #to test
# print(login())#njet247 , password #to test #MarSim password
# new_pos_1 = Position(account_pk=1, quantity=10, ticker="f", avg_price=100) #test done and in DB
# new_pos_1.save()
# x = Position.all()
# x = Position.from_pk(1)
# x = Position.from_account_and_ticker(1, "f")
# print(x)