forked from linsyking/CanvasHelper2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_mgr.py
139 lines (122 loc) · 4.67 KB
/
config_mgr.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
import sqlite3
from global_config import DATABASE
class ConfigMGR:
def __init__(self):
pass # No action is needed in constructor for now
def get_conf(self, username):
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
cursor.execute(
"""
SELECT * FROM users WHERE username = ?
""",
(username, ),
)
user_row = cursor.fetchone()
if user_row:
# Convert the row to a dictionary
columns = [desc[0] for desc in cursor.description]
user_db = [dict(zip(columns, user_row))][0]
user_id = user_db["id"]
courses = self.get_courses(user_id, cursor)
courses_list = [
dict(
zip(
[
"course_id",
"course_name",
"type",
"maxshow",
"order",
"msg",
],
course,
)) for course in courses
]
checks = self.get_checks(user_id, cursor)
checks_list = [
dict(zip(["type", "item_id"], check)) for check in checks
]
user_conf = {
"username": user_db["username"],
"semester_begin": user_db["semester_begin"],
"url": user_db["url"],
"bid": user_db["bid"],
"title": user_db["title"],
"timeformat": user_db["timeformat"],
"background_image": user_db["background_image"],
"courses": courses_list,
"checks": checks_list,
}
return user_conf
else:
return {"version": 1}
def remove_key(self, username, key):
self.set_key_value(username, key, None)
def set_key_value(self, username, key, value):
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
# Get the user_id for the given username
cursor.execute("SELECT id FROM users WHERE username = ?",
(username, ))
user_id = cursor.fetchone()[0]
if key == "courses":
for course in value:
cursor.execute(
"""
INSERT OR REPLACE INTO courses (
user_id, course_id, course_name, type, maxshow, display_order, msg
) VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(
user_id,
course["course_id"],
course["course_name"],
course["type"],
course["maxshow"],
course["order"],
course["msg"],
),
)
elif key == "checks":
for check in value:
cursor.execute(
"""
INSERT OR REPLACE INTO checks (
user_id, type, item_id
) VALUES (?, ?, ?)
""",
(user_id, check["type"], check["item_id"]),
)
elif key in [
"title",
"semester_begin",
"url",
"bid",
"timeformat",
"background_image",
]:
if not (key == "bid" and value == "********"):
cursor.execute(
f"UPDATE users SET {key} = ? WHERE id = ?",
(value, user_id),
)
else:
raise Exception("Invalid key")
conn.commit()
def get_checks(self, user_id, cursor):
cursor.execute(
"""
SELECT type, item_id, type FROM checks WHERE user_id = ?
""",
(user_id, ),
)
return cursor.fetchall()
def get_courses(self, user_id, cursor):
cursor.execute(
"""
SELECT course_id, course_name, type, maxshow, display_order, msg FROM courses WHERE user_id = ?
""",
(user_id, ),
)
return cursor.fetchall()