-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.py
113 lines (97 loc) · 2.98 KB
/
users.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import hashlib
from database import DB
from itsdangerous import (
TimedJSONWebSignatureSerializer as Serializer,
BadSignature,
SignatureExpired
)
SECRET_KEY = 'kAo@iNwkkm^vT.ic$^x0qxNppsm3ou$*Gxje4j1^Yg0alrrmyqfy0c!8euc@t*e!e'
class User:
def __init__(self, id, email, password, name, address, telephone):
self.id = id
self.email = email
self.password = password
self.name = name
self.address = address
self.telephone = telephone
def create(self):
with DB() as db:
db.execute(
'''
INSERT INTO users (email, password, name, address, telephone)
VALUES ({}, {}, {}, {}, {})
'''.format(
self.email,
self.password,
self.name,
self.address,
self.telephone))
return self
@staticmethod
def all():
with DB() as db:
rows = db.execute('SELECT * FROM users').fetchall()
return [User(*row) for row in rows]
@staticmethod
def to_dict(user):
return {
'id': user.id,
'email': user.email,
'name': user.name,
'address': user.address,
'telephone': user.telephone
}
@staticmethod
def find_by(column, data):
if not data:
return None
with DB() as db:
row = db.execute(
'SELECT * FROM users WHERE {} = {}'.format(column, data)
).fetchone()
if row:
return User(*row)
@staticmethod
def update(id, column, data):
with DB() as db:
db.execute(
'''
UPDATE users
SET {} = "{}"
WHERE id = {}
'''.format(column, data, id))
return
@staticmethod
def delete(id):
with DB() as db:
db.execute(
'''
DELETE FROM users
WHERE id = {}
'''.format(id))
return
@staticmethod
def get_bought_ads(id):
with DB() as db:
db.execute(
"SELECT * FROM ads WHERE owner_id={} AND is_active=0".format(id))
bought_ads = db.fetchall()
return bought_ads
@staticmethod
def hash_password(password):
return hashlib.sha256(password.encode('utf-8')).hexdigest()
def verify_password(self, password):
return self.password == hashlib.sha256(password.encode('utf-8')).hexdigest()
def generate_token(self):
s = Serializer(SECRET_KEY, expires_in=604800)
return s.dumps({'email': self.email})
@staticmethod
def verify_token(token):
s = Serializer(SECRET_KEY)
try:
s.loads(token)
except SignatureExpired:
return None
except BadSignature:
return None
return User.find_by('email', s.loads(token)['email'])