forked from linsyking/CanvasHelper2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
users.py
72 lines (64 loc) · 2.05 KB
/
users.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
import sqlite3
from global_config import DATABASE, pwd_context
def create_user(form_data, canvas_id, canvas_name):
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
# if user_exists(username, cursor):
# return False
# else:
hashed_password = pwd_context.hash(form_data.password)
cursor.execute(
"""
INSERT INTO users (username, hashed_password, url, bid, title, canvas_id, canvas_name, timeformat) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""",
(
form_data.username,
hashed_password,
form_data.url,
form_data.bid,
"Canvas Dashboard",
canvas_id,
canvas_name,
"relative",
),
)
conn.commit()
return True
def get_hashed_password(username):
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
cursor.execute(
"""
SELECT hashed_password FROM users WHERE username = ?
""",
(username, ),
)
result = cursor.fetchone()
return result[0] if result else None
def user_exists(username, cursor=None):
if cursor is None:
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
return user_exists(username, cursor)
else:
cursor.execute(
"""
SELECT id FROM users WHERE username = ?
""",
(username, ),
)
return cursor.fetchone() is not None
def same_user(url, canvas_id, cursor=None):
if cursor is None:
with sqlite3.connect(DATABASE) as conn:
cursor = conn.cursor()
return same_user(url, canvas_id, cursor)
else:
cursor.execute(
# Every user of a Canvas LMS has a unique canvas_id
"""
SELECT id FROM users WHERE url = ? AND canvas_id = ?
""",
(url, canvas_id),
)
return cursor.fetchone() is not None