-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchlists_utils.py
85 lines (77 loc) · 3.26 KB
/
watchlists_utils.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
from utils import logger
from configs import SCHEMA
import json
import sys
import uuid
def user_watchlist_update(connection, cursor, watchlist_id, watchlist, username):
cursor.execute("""
UPDATE {}.user_watch_list
SET watchlist = jsonb_set(
cast(watchlist as jsonb), %s, %s, true
)
WHERE
username = %s
""".format(SCHEMA), (watchlist_id, watchlist, username))
connection.commit()
def create_watchlist_checks(watchlist):
check_res = {
"status": False,
"msg": ""
}
checks_failed = False
if sys.getsizeof(watchlist) > 5000:
check_res["msg"] = "Watchlist is too large"
checks_failed = True
if "wathclistName" not in watchlist.keys():
check_res["msg"] = f'{check_res["msg"]},Watchlist name is not present!'
checks_failed = True
if "selectedSymbols" not in watchlist.keys():
check_res["msg"] = f'{check_res["msg"]},Symbols are not present!'
checks_failed = True
if "selectedSymbols" in watchlist.keys() and len(watchlist["selectedSymbols"]) > 30:
check_res["msg"] = f'{check_res["msg"]},Cannot accept more than 30 symbols!'
checks_failed = True
if checks_failed is False:
check_res["status"] = True
return check_res
def create_watchlist(request, cursor, conn):
watchlist_create_res = {
"status": False,
"msg": ""
}
try:
watchlist_dict = request.get_json()
check_res = create_watchlist_checks(watchlist_dict)
if check_res["status"] is False:
return check_res
watchlist_id = str(uuid.uuid4())
user_watchlist_update(conn, cursor, f"{{{watchlist_id}}}", json.dumps({"name": watchlist_dict["wathclistName"], "symbols": watchlist_dict["selectedSymbols"]}), watchlist_dict["username"])
watchlist_create_res["status"] = True
watchlist_create_res["msg"] = "Watchlist added!"
watchlist_create_res["watchlist_id"] = watchlist_id
except Exception as e:
logger(request, f"Watchlist create error: {e}")
watchlist_create_res["msg"] = "Something went wront, unable to create watchlist!"
return watchlist_create_res
def update_watchlist(request, cursor, conn):
watchlist_update_res = {
"status": False,
"msg": ""
}
try:
watchlist_dict = request.get_json()
check_res = create_watchlist_checks(watchlist_dict)
if "watchlistId" not in watchlist_dict.keys():
check_res["msg"] = f'{check_res["msg"]},Watchlist id is not present!'
check_res["status"] = False
if check_res["status"] is False:
return check_res
watchlist_id = watchlist_dict["watchlistId"]
user_watchlist_update(conn, cursor, f"{{{watchlist_id}}}", json.dumps({"name": watchlist_dict["wathclistName"], "symbols": watchlist_dict["selectedSymbols"]}), watchlist_dict["username"])
watchlist_update_res["status"] = True
watchlist_update_res["msg"] = "Watchlist updated!"
watchlist_update_res["watchlist_id"] = watchlist_id
except Exception as e:
logger(request, f"Watchlist update error: {e}")
watchlist_update_res["msg"] = "Something went wront, unable to update watchlist!"
return watchlist_update_res