-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
841 lines (715 loc) · 27.6 KB
/
app.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
# AS simple as possbile flask google oAuth 2.0
from flask import Flask, redirect, url_for, session, request, jsonify, Response
from authlib.integrations.flask_client import OAuth
import os
from flask_cors import CORS
from datetime import timedelta
import sys
from flask_cors import CORS, cross_origin
import psycopg2
from werkzeug.exceptions import HTTPException
import hashlib
import jwt
import urllib.parse as urlparse
from yahoo_fin.stock_info import get_live_price, get_quote_data
# dotenv setup
from dotenv import load_dotenv
load_dotenv()
# App config
app = Flask(__name__)
CORS(app)
url = urlparse.urlparse(os.environ['DATABASE_URL'])
dbname = url.path[1:]
user = url.username
password = url.password
host = url.hostname
port = url.port
con = psycopg2.connect(
dbname=dbname,
user=user,
password=password,
host=host,
port=port
)
# con = psycopg2.connect(
# dbname=chronicle.db
# # user=user,
# # password=password,
# # host=host,
# # port=port
# )
class InvalidUsage(Exception):
status_code = 400
def __init__(self, message, status_code=None, payload=None):
Exception.__init__(self)
self.message = message
if status_code is not None:
self.status_code = status_code
self.payload = payload
def to_dict(self):
rv = dict(self.payload or ())
rv['message'] = self.message
return rv
@app.errorhandler(InvalidUsage)
def handle_invalid_usage(error):
response = jsonify(error.to_dict())
response.status_code = error.status_code
return response
def hasher(string):
return hashlib.sha256(string.encode()).hexdigest()
# Generates a token for a registered user
def generate_token(username):
"""
Generates a JSON Web Token (JWT) encoded token for a given username
Input: username (str)
Output: JWT-encoded token (str)
"""
# private_key = "SecretKey"
private_key = os.environ['PRIVATE_KEY']
# private_key = "Hamster Wealth is the best website teeheexd"
token = jwt.encode({'username': username}, private_key, algorithm='HS256')
return token
# @app.route('/')
# def hello_world():
# return redirect('/portfolios')
# return dict(session)['token']['id_token']
# @app.route('/login')
# def login():
# google = oauth.create_client('google') # create the google oauth client
# redirect_uri = url_for('authorize', _external=True)
# return google.authorize_redirect(redirect_uri)
# @app.route('/authorize')
# def authorize():
#
# cur = con.cursor()
# google = oauth.create_client('google') # create the google oauth client
# # Access token from google (needed to get user info)
# token = google.authorize_access_token()
# # userinfo contains stuff u specificed in the scrope
# resp = google.get('userinfo')
# user_info = resp.json()
# user = oauth.google.userinfo() # uses openid endpoint to fetch user info
# # Here you use the profile/user data that you got and query your database find/register the user
# # and set ur own data in the session not the profile from google
# session['token'] = token
# session['user'] = user
# # make the session permanant so it keeps existing after broweser gets closed
# session.permanent = True
# cur.execute('BEGIN TRANSACTION;')
# query = """
# INSERT INTO client (token, name) VALUES ('{}', '{}');
# """.format(dict(session)['token']['id_token'], dict(session)['user']['name'])
# cur.execute(query)
# cur.execute('COMMIT;')
# # return dict(session)['token']['id_token']
# return redirect('http://localhost:3000/portfolios')
# # return redirect('http://localhost:3000/gettoken')
@app.route('/auth/login', methods=['POST'])
def auth_login():
cur = con.cursor()
data = request.get_json()
if data['username'] is None or data['password'] is None:
# raise InputError ('Please enter your username and password')
raise InvalidUsage(
'Please enter your username and password', status_code=400)
query = """select token, password from client where username = '{}'; """.format(
data['username'])
cur.execute(query)
x = cur.fetchone()
if x is None:
# raise AccessError ('Invalid username')
raise InvalidUsage('Invalid username', status_code=403)
token, password = x
hashed_password = hasher(data['password'])
if hashed_password != password:
# raise AccessError ('Incorrect password')
raise InvalidUsage('Incorrect password', status_code=403)
return {'token': token}
@app.route('/auth/register', methods=['POST'])
def auth_register():
cur = con.cursor()
data = request.get_json()
if data['username'] is None or data['password'] is None:
# raise InputError ('Please fill in all details')
raise InvalidUsage('Please fill in all details', status_code=400)
# Checks if username is unique
query = """select username from client where username = '{}'; """.format(
data['username'])
cur.execute(query)
x = cur.fetchone()
if x is not None:
raise InvalidUsage('Username already taken', status_code=409)
# # Checks if email is unique
# query = """select email from client u where email = '{}'; """.format(data['email'])
# cur.execute(query)
# x = cur.fetchone()
# if x is not None:
# raise InvalidUsage('Email already taken', status_code=409)
hashed_password = hasher(data['password'])
token = generate_token(data['username'])
cur.execute('BEGIN TRANSACTION;')
cur.execute(
f"""INSERT INTO client (token, username, password) VALUES ('{token}', '{data['username']}', '{hashed_password}');""")
cur.execute('COMMIT;')
query = """select max(p.portfolio_id) from portfolio p;"""
cur.execute(query)
x = cur.fetchone()
if x[0] is None:
portfolio_id_tuple = 0
else:
portfolio_id_tuple = x[0]
portfolio_id = portfolio_id_tuple
portfolio_id += 1
cur.execute('BEGIN TRANSACTION;')
query = """INSERT INTO portfolio (token, portfolio_id, title, balance)
VALUES ('{}', {}, '{}', 0);""".format(token, portfolio_id, 'Virtual Portfolio')
cur.execute(query)
cur.execute('COMMIT;')
return {'token': token}
# @app.route('/gettoken', methods=['GET'])
#
# def hello_world():
# return dict(session)['token']['id_token']
# @app.route('/logout')
# def logout():
# for key in list(session.keys()):
# session.pop(key)
# return redirect('/')
@app.route('/portfolios/create', methods=['POST'])
def portfolios_create():
cur = con.cursor()
parsed_token = request.headers.get('Authorization')
if parsed_token is None:
raise InvalidUsage('Invalid Auth Token', status_code=403)
query = """select max(p.portfolio_id) from portfolio p;"""
cur.execute(query)
x = cur.fetchone()
if x[0] is None:
portfolio_id_tuple = 0
else:
portfolio_id_tuple = x[0]
portfolio_id = portfolio_id_tuple
portfolio_id += 1
# print(portfolio_id)
query = f"""select max(portfolio_id) from portfolio where token = '{parsed_token}';"""
cur.execute(query)
x = cur.fetchone()
if x[0] is None:
priv_portfolio_id_tuple = 0
else:
priv_portfolio_id_tuple = x[0]
priv_portfolio_id = priv_portfolio_id_tuple
priv_portfolio_id += 1
# print(priv_portfolio_id)
title = f'Portfolio {priv_portfolio_id}'
cur.execute('BEGIN TRANSACTION;')
query = """INSERT INTO portfolio (token, portfolio_id, title, balance)
VALUES ('{}', {}, '{}', 0);""".format(parsed_token, portfolio_id, title)
cur.execute(query)
cur.execute('COMMIT;')
return {
'portfolio_id': portfolio_id
}
@app.route('/portfolio/addcash', methods=['POST'])
def portfolio_addcash():
cur = con.cursor()
parsed_token = request.headers.get('Authorization')
if parsed_token is None:
raise InvalidUsage('Invalid Auth Token', status_code=403)
data = request.get_json()
if data is None:
raise InvalidUsage('Malformed Requesta', status_code=400)
if len(data) != 2:
raise InvalidUsage('Malformed Requestb', status_code=400)
portfolio_id = data['portfolio_id']
cash_amt = data['cash_amount']
cur.execute(
f"select token from portfolio where portfolio_id = '{portfolio_id}'")
x = cur.fetchone()
if x is None:
raise InvalidUsage('Invalid Token', status_code=403)
if isinstance(portfolio_id, int) is False:
raise InvalidUsage('Malformed Request', status_code=400)
if isinstance(cash_amt, int) is False and isinstance(cash_amt, float) is False:
raise InvalidUsage('Malformed Request', status_code=400)
cur.execute(
f"select portfolio_id from portfolio where token = '{parsed_token}'")
portfolio_found = 0
x = cur.fetchall()
for pid in x:
if portfolio_id == pid[0]:
portfolio_found = 1
break
if portfolio_found == 0:
raise InvalidUsage('Portfolio not found', status_code=404)
cur.execute(
f"select balance from portfolio where token = '{parsed_token}' and portfolio_id = {portfolio_id}")
balance = cur.fetchone()
cur.execute('BEGIN TRANSACTION;')
query = f"""UPDATE portfolio
SET balance = {balance[0] + cash_amt}
WHERE token = '{parsed_token}' and portfolio_id = {portfolio_id};"""
cur.execute(query)
cur.execute('COMMIT;')
cur.execute(
f"select balance from portfolio where token = '{parsed_token}' and portfolio_id = {portfolio_id}")
balance = cur.fetchone()
return {
'balance': balance[0]
}
@app.route('/portfolio/getbalance', methods=['GET'])
def portfolio_getbalance():
cur = con.cursor()
parsed_token = request.headers.get('Authorization')
data = request.args.get('portfolio_id')
if parsed_token is None:
raise InvalidUsage('Invalid Auth Token', status_code=403)
portfolio_id = int(data)
cur.execute(
f"select token from portfolio where portfolio_id = {portfolio_id}")
x = cur.fetchone()
if x is None:
raise InvalidUsage(f'Invalid Token {parsed_token}', status_code=403)
if data.isnumeric() is False:
raise InvalidUsage('Malformed Request', status_code=400)
cur.execute(
f"select portfolio_id, title, balance from portfolio where token = '{parsed_token}'")
portfolio_found = 0
x = cur.fetchall()
for pid in x:
if portfolio_id == pid[0]:
portfolio_found = 1
if portfolio_found == 0:
raise InvalidUsage(
f'Portfolio not found {portfolio_id}', status_code=404)
cur.execute(
f"select balance from portfolio where token = '{parsed_token}' and portfolio_id = {portfolio_id}")
balance = cur.fetchone()
return {
'balance': balance[0]
}
@app.route('/portfolios/list', methods=['GET'])
def portfolios_list():
cur = con.cursor()
parsed_token = request.headers.get('Authorization')
if parsed_token is None:
raise InvalidUsage('Invalid Auth Token', status_code=403)
cur.execute(
f"select portfolio_id, title from portfolio where token = '{parsed_token}'")
list_of_portfolio = cur.fetchall()
portfolio_list = [
{
"portfolio_id": portfolio_deets[0],
"title": portfolio_deets[1],
}
for portfolio_deets in list_of_portfolio
]
return {'portfolio_list': portfolio_list}
# @app.route('/portfolios/edit')
# def portfolios_edit():
# cur = con.cursor()
# parsed_token = request.headers.get('Authorization')
# if parsed_token is None:
# raise InvalidUsage('Invalid Auth Token', status_code=403)
# data = request.get_json()
# portfolio_id = data['portfolio_id']
# title = data['title']
# cur.execute(
# f"select token from portfolio where portfolio_id = '{portfolio_id}'")
# x = cur.fetchone()
# if x is None:
# raise InvalidUsage('Invalid Token', status_code=403)
# if data['portfolio_id'].isnumeric() is False:
# raise InvalidUsage('Malformed Request', status_code=400)
# cur.execute(
# f'select portfolio_id, title, balance from portfolio where token = {parsed_token}')
# portfolio_found = 0
# x = cur.fetchall()
# for pid in x:
# if portfolio_id == pid[0]:
# portfolio_found = 1
# break
# if portfolio_found == 0:
# raise InvalidUsage('Portfolio not found', status_code=404)
# cur.execute('BEGIN TRANSACTION;')
# query = f"""UPDATE portfolio p
# SET p.title = '{title}',
# WHERE p.token = {parsed_token} and p.portfolio_id = {portfolio_id};"""
# cur.execute(query)
# cur.execute('COMMIT;')
# return {}
@app.route('/portfolios/removeportfolio', methods=['DELETE'])
def portfolios_removeportfolio():
cur = con.cursor()
parsed_token = request.headers.get('Authorization')
if parsed_token is None:
raise InvalidUsage('Invalid Auth Token', status_code=403)
parsed_pid = int(request.args.get('portfolio_id'))
portfolio_id = parsed_pid
cur.execute(
f"select token from portfolio where portfolio_id = '{portfolio_id}'")
x = cur.fetchone()
if x is None:
raise InvalidUsage('Invalid Token', status_code=403)
if isinstance(portfolio_id, int) is False:
raise InvalidUsage('Malformed Request', status_code=400)
cur.execute(
f"select portfolio_id, title, balance from portfolio where token = '{parsed_token}'")
portfolio_found = 0
x = cur.fetchall()
for pid in x:
if portfolio_id == pid[0]:
portfolio_found = 1
break
if portfolio_found == 0:
raise InvalidUsage('Portfolio not found', status_code=404)
cur.execute('BEGIN TRANSACTION;')
query = f"""DELETE FROM portfolio
WHERE portfolio.portfolio_id = {portfolio_id} and portfolio.token = '{parsed_token}';"""
cur.execute(query)
cur.execute('COMMIT;')
return {}
@app.route('/portfolio/buyholding', methods=['POST'])
def portfolio_buyholding():
cur = con.cursor()
parsed_token = request.headers.get('Authorization')
if parsed_token is None:
raise InvalidUsage('Invalid Auth Token', status_code=403)
data = request.get_json()
portfolio_id = int(data['portfolio_id'])
ticker = str(data['ticker'].upper())
avg_price = float(data['avg_price'])
quantity = int(data['quantity'])
query = f"select token from portfolio where portfolio_id = {portfolio_id};"
cur.execute(query)
x = cur.fetchone()
if x is None:
raise InvalidUsage('Invalid Token', status_code=403)
if isinstance(portfolio_id, int) is False and isinstance(quantity, int) is False and isinstance(avg_price, float) is False:
raise InvalidUsage('Malformed Request', status_code=400)
try:
data = get_live_price(f'{ticker}')
except:
raise InvalidUsage('Invalid Ticker', status_code=404)
cur.execute(
f"select portfolio_id from portfolio where token = '{parsed_token}'")
portfolio_found = 0
x = cur.fetchall()
for pid in x:
if portfolio_id == pid[0]:
portfolio_found = 1
break
if portfolio_found == 0:
raise InvalidUsage('Portfolio not found', status_code=404)
# Error trapping ^
# Check is stock owned
cur.execute(
f'select ticker from stock where portfolio_id = {portfolio_id}')
x = cur.fetchall()
ticker_found = 0
for stock in x:
if ticker == stock[0]:
ticker_found = 1
break
if quantity <= 0:
raise InvalidUsage('Invalid quantity', status_code=404)
if avg_price <= 0:
raise InvalidUsage('Invalid price', status_code=404)
# deduct from balance
cur.execute(
f"select balance from portfolio where token = '{parsed_token}' and portfolio_id = {portfolio_id}")
balance = cur.fetchone()
cash_amt = avg_price * quantity
cash_amt = round(cash_amt, 2)
if int(cash_amt > balance[0]):
raise InvalidUsage(
f'Not enough cash in balance', status_code=404)
cur.execute('BEGIN TRANSACTION;')
query = f"""UPDATE portfolio
SET balance = {balance[0] - cash_amt}
WHERE portfolio_id = {portfolio_id};"""
cur.execute(query)
cur.execute('COMMIT;')
# if not owned add to portfolio
if ticker_found == 0:
company = get_quote_data(f'{ticker}')['longName']
cur.execute('BEGIN TRANSACTION;')
query = f"""INSERT INTO stock (portfolio_id, ticker, company, avg_price, units)
VALUES ({portfolio_id}, '{ticker.upper()}', '{company}', {avg_price}, {quantity});"""
cur.execute(query)
cur.execute('COMMIT;')
else:
# if owned, update units and avg price
cur.execute(
f"select avg_price, units from stock where portfolio_id = {portfolio_id} and ticker = '{ticker}'")
x = cur.fetchone()
old_avg_price, old_units = x
new_avg_price = ((old_avg_price * old_units) +
(avg_price * quantity))/(quantity + old_units)
new_avg_price = "{:.2f}".format(new_avg_price)
cur.execute('BEGIN TRANSACTION;')
query = f"""UPDATE stock
SET avg_price = {new_avg_price},
units = {old_units + quantity}
WHERE portfolio_id = {portfolio_id} and ticker = '{ticker}';"""
cur.execute(query)
cur.execute('COMMIT;')
return {}
@app.route('/portfolio/sellholding', methods=['PUT'])
def portfolio_sellholding():
cur = con.cursor()
parsed_token = request.headers.get('Authorization')
if parsed_token is None:
raise InvalidUsage('Invalid Auth Token', status_code=403)
data = request.get_json()
portfolio_id = int(data['portfolio_id'])
ticker = str(data['ticker'].upper())
avg_price = float(data['avg_price'])
quantity = int(data['quantity'])
query = f"select token from portfolio where portfolio_id = {portfolio_id};"
cur.execute(query)
x = cur.fetchone()
if x is None:
raise InvalidUsage('Invalid Token', status_code=403)
if isinstance(portfolio_id, int) is False and isinstance(quantity, int) is False and isinstance(avg_price, float) is False:
raise InvalidUsage('Malformed Request', status_code=400)
if ticker.isalpha() is False:
raise InvalidUsage('Malformed Request', status_code=400)
cur.execute(
f"select portfolio_id from portfolio where token = '{parsed_token}'")
portfolio_found = 0
x = cur.fetchall()
for pid in x:
if portfolio_id == pid[0]:
portfolio_found = 1
break
if portfolio_found == 0:
raise InvalidUsage('Portfolio not found', status_code=404)
# Error trapping ^
# Check is stock owned
cur.execute(
f'select ticker from stock where portfolio_id = {portfolio_id}')
x = cur.fetchall()
ticker_found = 0
for stock in x:
if ticker == stock[0]:
ticker_found = 1
break
if ticker_found == 0:
raise InvalidUsage('Insufficient shares', status_code=404)
if quantity <= 0:
raise InvalidUsage('Invalid quantity', status_code=404)
if avg_price <= 0:
raise InvalidUsage('Invalid price', status_code=404)
cur.execute(
f"select units from stock where portfolio_id = {portfolio_id} and ticker = '{ticker}'")
x = cur.fetchone()
units = x[0]
# if not owned add to portfolio
if quantity == units:
company = get_quote_data(f'{ticker}')['longName']
cur.execute('BEGIN TRANSACTION;')
query = f"""delete from stock where ticker = '{ticker}' and portfolio_id = {portfolio_id};"""
cur.execute(query)
cur.execute('COMMIT;')
elif quantity < units:
# if owned, update units and avg price
cur.execute(
f"select units from stock where portfolio_id = {portfolio_id} and ticker = '{ticker}'")
x = cur.fetchone()
old_units = x
cur.execute('BEGIN TRANSACTION;')
query = f"""UPDATE stock
SET units = {old_units[0] - quantity}
WHERE portfolio_id = {portfolio_id} and ticker = '{ticker}';"""
cur.execute(query)
cur.execute('COMMIT;')
else:
raise InvalidUsage('Insufficient shares', status_code=404)
# add to balance
cur.execute(
f"select balance from portfolio where token = '{parsed_token}' and portfolio_id = {portfolio_id}")
balance = cur.fetchone()
cash_amt = avg_price * quantity
cash_amt = round(cash_amt, 2)
if int(cash_amt > balance[0]):
raise InvalidUsage(
f'Not enough cash in balance', status_code=404)
cur.execute('BEGIN TRANSACTION;')
query = f"""UPDATE portfolio
SET balance = {balance[0] + cash_amt}
WHERE portfolio_id = {portfolio_id};"""
cur.execute(query)
cur.execute('COMMIT;')
return {}
@app.route('/portfolio/deleteholding', methods=['DELETE'])
def portfolio_deleteholding():
cur = con.cursor()
parsed_token = request.headers.get('Authorization')
if parsed_token is None:
raise InvalidUsage('Invalid Auth Token', status_code=403)
data = request.get_json()
portfolio_id = int(data['portfolio_id'])
ticker = str(data['ticker'].upper())
avg_price = float(data['avg_price'])
quantity = int(data['quantity'])
query = f"select token from portfolio where portfolio_id = {portfolio_id};"
cur.execute(query)
x = cur.fetchone()
if x is None:
raise InvalidUsage('Invalid Token', status_code=403)
if isinstance(portfolio_id, int) is False and isinstance(quantity, int) is False and isinstance(avg_price, float) is False:
raise InvalidUsage('Malformed Request', status_code=400)
if ticker.isalpha() is False:
raise InvalidUsage('Malformed Request', status_code=400)
cur.execute(
f"select portfolio_id from portfolio where token = '{parsed_token}'")
portfolio_found = 0
x = cur.fetchall()
for pid in x:
if portfolio_id == pid[0]:
portfolio_found = 1
break
if portfolio_found == 0:
raise InvalidUsage('Portfolio not found', status_code=404)
# Error trapping ^
# Check is stock owned
cur.execute(
f'select ticker from stock where portfolio_id = {portfolio_id}')
x = cur.fetchall()
ticker_found = 0
for stock in x:
if ticker == stock[0]:
ticker_found = 1
break
if ticker_found == 0:
raise InvalidUsage(
'You do not own any shares of this stock', status_code=404)
cur.execute('BEGIN TRANSACTION;')
query = f"""delete from stock where ticker = '{ticker}' and portfolio_id = {portfolio_id};"""
cur.execute(query)
cur.execute('COMMIT;')
return {}
@app.route('/portfolio/holdings', methods=['GET'])
def portfolio_holdings():
cur = con.cursor()
parsed_token = request.headers.get('Authorization')
parsed_pid = int(request.args.get('portfolio_id'))
if parsed_token is None:
raise InvalidUsage('Invalid Auth Token', status_code=403)
cur.execute(
f"select token from portfolio where portfolio_id = {parsed_pid}")
x = cur.fetchone()
if x is None:
raise InvalidUsage('Invalid Token', status_code=403)
cur.execute(
f"select portfolio_id from portfolio where token = '{parsed_token}'")
portfolio_found = 0
x = cur.fetchall()
for pid in x:
if parsed_pid == pid[0]:
portfolio_found = 1
break
if portfolio_found == 0:
raise InvalidUsage('Portfolio not found', status_code=404)
cur.execute(
f"select ticker, company, avg_price, units from stock where portfolio_id = {parsed_pid}")
x = cur.fetchall()
stock_list = []
assets = 0
for stock in x:
ticker, company, avg_price, units = stock
live_price = get_live_price(f'{ticker}')
value = units * live_price
assets += value
for holding in x:
ticker, company, avg_price, units = holding
temp = get_quote_data(f'{ticker}')
live_price = get_live_price(f'{ticker}')
if live_price > 0:
live_price = "{:.2f}".format(live_price)
else:
live_price = "{:.4f}".format(live_price)
change_p = temp['regularMarketChangePercent']
change_d = temp['regularMarketChange']
if float(change_d) > 0:
change_d = "{:.2f}".format(float(change_d))
else:
change_d = "{:.2g}".format(float(change_d))
value = float(live_price) * int(units)
value = "{:.2f}".format(value)
profit_loss_d = float(value) - (units * avg_price)
profit_loss_p = (profit_loss_d/(units * avg_price)) * 100
# else:
# profit_loss_p = -1 * (100 - profit_loss_d/value)
profit_loss_p = "{:.2f}".format(profit_loss_p)
profit_loss_d = "{:.2f}".format(profit_loss_d)
if float(profit_loss_d) < 0:
profit_loss = f'-${-1*float(profit_loss_d)} ({profit_loss_p}%)'
else:
profit_loss = f'${float(profit_loss_d)} ({profit_loss_p}%)'
change_value = float(change_d) * int(units)
if float(change_value) > 0:
change_value = "{:.2f}".format(float(change_value))
else:
change_value = "{:.2g}".format(float(change_value))
weight = float(value)/assets * 100
weight = "{:.2f}".format(weight)
change_p = temp['regularMarketChangePercent']
change_d = temp['regularMarketChange']
change_p = "{:.2f}".format(change_p)
change_d = "{:.5f}".format(change_d)
if float(change_d) < 0:
change = f'-${-1*float(change_d)} ({change_p}%)'
else:
change = f'${float(change_d)} ({change_p}%)'
live_price_str = f'${live_price}'
avg_price_str = f'${avg_price}'
value_str = f'${float(value)}'
if float(change_value) < 0:
change_value_str = f'-${-1*float(change_value)}'
else:
change_value_str = f'${float(change_value)}'
stock = {
'ticker': ticker,
'company': company,
'live_price': live_price_str,
'change': change,
'profit_loss': profit_loss,
'units': units,
'avg_price': avg_price_str,
'value': value_str,
'weight': weight,
'change_value': change_value_str
}
stock_list.append(stock)
return {'holdings': stock_list}
# @app.route('/user/list')
# def user_list():
#
# cur = con.cursor()
# con.execute("""select client.username, portfolio.title, portfolio.portfolio_id from client join portfolio
# where client.token = portfolio.token; """)
# x = cur.fetchall()
# array_list = []
# for portfolio in x:
# name, title, pid = x
# assets = 0
# portfolio_chg = 0
# con.execute(f"""select ticker, avg_price, units from stock where portfolio_id = {pid}""")
# y = cur.fetchall()
# for stock in y:
# ticker, avg_price, units = y
# value = get_live_price(f'{ticker}') * units
# value = "{:.2f}".format(value)
# asset += value
# for stock in y:
# ticker, avg_price, units = y
# value = get_live_price(f'{ticker}') * units
# value = "{:.2f}".format(value)
# weight = value / assets
# temp = get_quote_data(f'{ticker}')
# change_p = temp['regularMarketChangePercent']
# portfolio_chg += change_p * weight
if __name__ == '__main__':
app.run(debug=True, port=4500)