-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.py
23 lines (19 loc) · 974 Bytes
/
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
from database import CursorFromConnectionFromPool
class User:
def __init__(self, email, first_name, last_name, id):
self.email = email
self.first_name = first_name
self.last_name = last_name
self.id = id
def __repr__(self):
return "<User details: {} {} {}>".format(self.email,self.first_name,self.last_name)
def save_to_db(self):
#establish db connection
with CursorFromConnectionFromPool() as cursor:
cursor.execute('INSERT INTO psy.users (email,first_name,last_name) VALUES (%s,%s,%s)',(self.email,self.first_name,self.last_name))
@classmethod
def load_from_db_by_email(cls,email):
with CursorFromConnectionFromPool() as cursor:
cursor.execute('SELECT * FROM psy.users where email = %s',(email,))
user_data = cursor.fetchone()
return cls(email = user_data[0], first_name = user_data[1], last_name = user_data[2], id = user_data[3])