-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser.py
60 lines (46 loc) · 2 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
from flask_login import UserMixin
from db import get_db
class User(UserMixin):
def __init__(self, id_, netid, fname, lname, email, uin, phone):
self.id = id_
self.netid = netid
self.fname = fname
self.lname = lname
self.email = email
self.uin = uin
self.phone = phone
@staticmethod
def get(user_id):
conn = get_db()
cur = conn.cursor()
cur.execute("SELECT * FROM USERS WHERE id = ?", (user_id,))
user_record = cur.fetchone()
if not user_record:
return None
user = User(id_=user_record['id'], netid=user_record['netid'], fname=user_record['fname'],
lname=user_record['lname'], email=user_record['email'], uin=user_record['uin'],
phone=user_record['phone'])
return user
@staticmethod
def search(netid):
conn = get_db()
cur = conn.cursor()
# get the current user id
cur.execute("SELECT * FROM USERS WHERE netid = (?)", (netid,))
user_record = cur.fetchone()
if not user_record:
return None
user = User(id_=user_record['id'], netid=user_record['netid'], fname=user_record['fname'],
lname=user_record['lname'], email=user_record['email'], uin=user_record['uin'],
phone=user_record['phone'])
return user
@staticmethod
def create(netid, fname, lname, email, uin, phone):
conn = get_db()
cur = conn.cursor()
cur.execute("INSERT INTO USERS (netid, fname, lname, email, uin, phone) VALUES (?,?,?,?,?,?)", (netid, fname,
lname, email,
uin, phone,))
conn.commit()
user = User(id_=cur.lastrowid, netid=netid, fname=fname, lname=lname, email=email, uin=uin, phone=phone)
return user