-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
41 lines (34 loc) · 1.16 KB
/
models.py
File metadata and controls
41 lines (34 loc) · 1.16 KB
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
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
from pymongo import MongoClient
import os
client = MongoClient(os.getenv('MONGO_URI'))
db = client.users
users = db.user_info
class User(UserMixin):
def __init__(self, username, email, password_hash=None):
self.username = username
self.email = email
self.password_hash = password_hash
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
def save(self):
users.insert_one({
'username': self.username,
'email': self.email,
'password_hash': self.password_hash
})
@staticmethod
def get(username):
user_data = users.find_one({'username': username})
if user_data:
return User(
username=user_data['username'],
email=user_data['email'],
password_hash=user_data['password_hash']
)
return None
def get_id(self):
return self.username