-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser_model.py
64 lines (51 loc) · 1.86 KB
/
user_model.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
from datetime import datetime, timedelta
import jwt
from werkzeug.security import generate_password_hash, check_password_hash
from werkzeug.exceptions import BadRequest, NotFound, Unauthorized, Forbidden
import psycopg2
from flask import current_app
# Local Imports
# from .instance.config import key as secret_key
from utils.db_helper import init_db
class User():
"""This class contains the functions of the user model"""
password = ''
def __init__(self, password='pass', email='mail@mail.com', role='normal'):
"""initialize the user model"""
User.password = generate_password_hash(password)
self.email = email
self.role = role
self.db = init_db()
def save_user(self):
"""Saves User Object to Database"""
new_user = dict(
role=self.role,
email=self.email,
password=self.password,
)
# check if user exists
if self.check_if_user_exists(new_user['email']):
raise Forbidden("User already exists.Please log in")
curr = self.db.cursor()
sql = """INSERT INTO users (role,email, password) \
VALUES ( %(role)s, %(email)s, %(password)s);
"""
curr.execute(sql, new_user)
self.db.commit()
curr.close()
def check_if_user_exists(self, email):
database = self.db
curr = database.cursor()
curr.execute("select * from users where email = (%s);", (email,))
result = curr.fetchone()
if result:
return True
return False
def get_user_by_email(self, email):
"""return user from the db given an email"""
curr = self.db.cursor()
curr.execute(
"SELECT role, password, registered_on FROM users WHERE email = (%s);", (email,))
data = curr.fetchone()
curr.close()
return data