-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.py
229 lines (189 loc) · 5.6 KB
/
user.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
import hashlib
from database import DB
class User:
def __init__(self, username, email):
self.id = None
self.username = username
self.email = email
self.password = None
self.authenticated = False
def is_anonymous(self):
return False
def is_authenticated(self):
return self.authenticated
def is_active(self):
return True
def create(self, password):
with DB() as db:
encr_password = User.encrypt_password(password)
values = (None, self.username, encr_password, self.email)
db.execute(
'''
INSERT INTO users
VALUES (?, ?, ?, ?)
''', values
)
return self
@staticmethod
def encrypt_password(password):
return hashlib.sha256(password.encode('utf-8')).hexdigest()
@staticmethod
def verify_password(password, username):
with DB() as db:
passwd = db.execute(
'''
SELECT password FROM users
WHERE name = ?
''', (username,)
).fetchone()[0]
return passwd ==\
hashlib.sha256(password.encode('utf-8')).hexdigest()
def get_id(self):
with DB() as db:
return db.execute(
'''
SELECT id FROM users
WHERE name = ?
''', (self.username,)
).fetchone()[0]
@staticmethod
def get_id_by_username(username):
with DB() as db:
row = db.execute(
'''
SELECT * FROM users
WHERE name = ?
''', (username,)
).fetchone()
if row:
with DB() as db:
return db.execute(
'''
SELECT id FROM users
WHERE name = ?
''', (username,)
).fetchone()[0]
@staticmethod
def get_username_by_id(id):
with DB() as db:
return db.execute(
'''
SELECT name FROM users
WHERE id = ?
''', (str(id),)
).fetchone()[0]
@staticmethod
def get_user_by_id(user_id):
if not user_id:
return None
with DB() as db:
row = db.execute(
'''
SELECT name, email FROM users
WHERE id = ?
''', (str(user_id),)
).fetchone()
if row:
return User(*row)
return False
@staticmethod
def get_user_by_username(username):
if not username:
return None
with DB() as db:
row = db.execute(
'''
SELECT name, email FROM users
WHERE name = ?
''', (username,)
).fetchone()
if row:
return User(*row)
return False
@staticmethod
def get_user_by_email(email):
if not email:
return None
with DB() as db:
row = db.execute(
'''
SELECT name, email FROM users
WHERE email = ?
''', (email,)
).fetchone()
if row:
return User(*row)
return False
@staticmethod
def update_password_for_email(email, new_password):
if not email:
return None
with DB() as db:
row = db.execute(
'''
SELECT * FROM users
WHERE email = ?
''', (email,)
).fetchone()
if row:
encr_password = User.encrypt_password(new_password)
db.execute(
'''
UPDATE users
SET password = ?
WHERE email = ?
''', (encr_password, email,)
)
else:
return False
@staticmethod
def update_username_for_email(email, new_username):
if not email:
return None
with DB() as db:
row = db.execute(
'''
SELECT * FROM users
WHERE email = ?
''', (email,)
).fetchone()
if row:
db.execute(
'''
UPDATE users
SET name = ?
WHERE email = ?
''', (new_username, email,)
)
else:
return False
@staticmethod
def update_email_for_email(email, new_email):
if not email:
return None
with DB() as db:
row = db.execute(
'''
SELECT * FROM users
WHERE email = ?
''', (email,)
).fetchone()
if row:
db.execute(
'''
UPDATE users
SET email = ?
WHERE email = ?
''', (new_email, email,)
)
else:
return False
def delete(self):
with DB() as db:
db.execute(
'''
DELETE FROM users
WHERE email = ?
''', (self.email,)
)
return self