-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.py
62 lines (53 loc) · 1.95 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
import os
from sqlalchemy import create_engine, text
from werkzeug.security import generate_password_hash, check_password_hash
engine = create_engine(os.getenv("DATABASE_URL", "sqlite+pysqlite:///user.db"), echo=True)
def init_userDB():
creation_users = text('''
CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
password_hash TEXT
);
''')
with engine.connect() as conn:
conn.execute(creation_users)
class User:
def __init__(self, id=None, password=None, password_hash=None):
self.id = id
if password:
self.password_hash = generate_password_hash(password)
else:
self.password_hash = password_hash
self.is_authenticated = True
self.is_active = True
self.is_anonymous = False
# Flask-Login compatibility
def get_id(self):
return self.id
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
if not self.password_hash:
return False
return check_password_hash(self.password_hash, password)
def set_authenticated(self, auth = True):
self.is_authenticated = auth
# Database interactions
@classmethod
def get(cls, user_id):
sql = text("SELECT id, password_hash FROM users WHERE id = :user_id")
with engine.connect() as conn:
result = conn.execute(sql, {"user_id": user_id}).fetchone()
if result:
return cls(id=result[0], password_hash=result[1])
else:
return None
def save(self):
sql = text(
"""
INSERT INTO users (id, password_hash) VALUES (:id, :password_hash)
ON CONFLICT(id) DO UPDATE SET password_hash = :password_hash
"""
)
with engine.connect() as conn:
conn.execute(sql, { "id" : self.id, "password_hash" : self.password_hash})