-
Notifications
You must be signed in to change notification settings - Fork 3
/
users_db_methods.py
178 lines (153 loc) · 6 KB
/
users_db_methods.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
import sqlite3
import os
from werkzeug.security import generate_password_hash, check_password_hash
from uuid import uuid4
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DB_PATH = os.path.join(BASE_DIR, "chat.sqlite")
def insert_row_in_users_db(username, firstname, lastname, email, password):
"""
Creates a row in chat.sqlite's users table
:param uid: a string, the unique identifier of the user
:param username: a string, the user's chosen username
:param firstname: a string, the first name of the user
:param lastname: a string, the last name of the user
:param email: a string, the email of the user
:param password: a string, the plaintext password of the user
"""
uid = uuid4().hex
pwd_hash = generate_password_hash(password)
login_time = set_lastlogin(uid)
row_data = (uid, username, firstname, lastname, email, pwd_hash, login_time)
with sqlite3.connect(DB_PATH) as conn:
c = conn.cursor()
c.execute('''INSERT INTO users (uid, username, firstname, lastname, email, passwordhash,
lastlogin) VALUES (?, ?, ?, ?, ?, ?, ?);''', row_data)
def get_uid_from_username(username):
"""
Gets a user's uid from the table given a username
:param uid: a string, the unique username of the user
:return: a string, the unique id of the user. Else,
returns None if unable to extract username from database
"""
with sqlite3.connect(DB_PATH) as conn:
c = conn.cursor()
c.execute('SELECT uid FROM users WHERE username=?;', (username,))
result = c.fetchone()
if result is None:
return None
return result[0]
def get_username(uid):
"""
Gets a user's username from the table given a uid
:param uid: a string, the unique identifier of the user
:return: a string, the unique username of the user. Else,
returns None if unable to extract username from database
"""
with sqlite3.connect(DB_PATH) as conn:
c = conn.cursor()
c.execute('SELECT username FROM users WHERE uid=?;', (uid,))
result = c.fetchone()
if result is None:
return None
return result[0]
def get_firstname(uid):
"""
Gets a user's first name from the table given a uid
:param uid: a string, the unique identifier of the user
:return: a string, the first name of the user. Else, returns
None if unable to extract first name from database
"""
with sqlite3.connect(DB_PATH) as conn:
c = conn.cursor()
c.execute('SELECT firstname FROM users WHERE uid=?;', (uid,))
result = c.fetchone()
if result is None:
return None
return result[0]
def get_lastname(uid):
"""
Gets a user's last name from the table given a uid
:param uid: a string, the unique identifier of the user
:return: a string, the last name of the user. Else, returns
None if unable to extract last name from database
"""
with sqlite3.connect(DB_PATH) as conn:
c = conn.cursor()
c.execute('SELECT lastname FROM users WHERE uid=?;', (uid,))
result = c.fetchone()
if result is None:
return None
return result[0]
def get_email(uid):
"""
Gets a user's email address from the table given a uid
:param uid: a string, the unique identifier of the user
:return: a string, the email of the user. Else, returns
None if unable to extract email from database
"""
with sqlite3.connect(DB_PATH) as conn:
c = conn.cursor()
c.execute('SELECT email FROM users WHERE uid=?;', (uid,))
result = c.fetchone()
if result is None:
return None
return result[0]
def get_password(uid):
"""
Gets a user's last name from the table given a uid
:param uid: a string, the unique identifier of the user
:return: a string, the password hash of the user. Else, returns
None if unable to extract password from database
"""
with sqlite3.connect(DB_PATH) as conn:
c = conn.cursor()
c.execute('SELECT passwordhash FROM users WHERE uid=?;', (uid,))
result = c.fetchone()
if result is None:
return None
return result[0]
def check_password(uid, password):
"""
Checks if a users password matches the inputted, plaintext one
:param uid: a string, the unique id of the user
:param password: a string, the plaintext password of the user
:return: a boolean, true if the passwords match
"""
pwd_hash = get_password(uid)
return check_password_hash(pwd_hash, password)
def get_lastlogin(uid):
"""
Gets a user's last login time from the table given a uid
:param uid: a string, the unique identifier of the user
:return: a string, the date of the user's last login. Else,
returns None if unable to extract the date from database
"""
with sqlite3.connect(DB_PATH) as conn:
c = conn.cursor()
c.execute('SELECT lastlogin FROM users WHERE uid=?;', (uid,))
result = c.fetchone()
if result is None:
return None
return result[0]
def get_login_time(uid):
"""
Gets a user's last login time from the table given a uid
:param uid: a string, the unique identifier of the user
:return: a boolean, true if the user is logged in. Else, returns
None if unable to extract the information from database
"""
with sqlite3.connect(DB_PATH) as conn:
c = conn.cursor()
c.execute('SELECT loggedin FROM users WHERE uid=?;', (uid,))
result = c.fetchone()
if result is None:
return None
return result[0]
def set_lastlogin(uid):
"""
Sets the login time of the user to be the time when the function is called
:param uid: a string, the unique identifier of the user
"""
with sqlite3.connect(DB_PATH) as conn:
c = conn.cursor()
c.execute("UPDATE users SET lastlogin=DATETIME('NOW') WHERE uid=?;", (uid,))