-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.py
216 lines (185 loc) · 4.56 KB
/
helpers.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import sqlite3
import pdb
import json
from flask import Flask
from itsdangerous import (TimedJSONWebSignatureSerializer as Serializer,
BadSignature,
SignatureExpired)
from config import get_db
from exceptions import UserRegistrationError
from exceptions import UserLoginError
from passlib.apps import custom_app_context as pwd_context
secret_key = 'secret_key'
def auto_commit(func):
def inner(*args, **kw):
db = get_db()
try:
func(*args, db=db, **kw)
db.commit()
except Exception as inst:
raise inst
else:
db.close()
return inner
def is_authenticated():
return True
def get_user_id(email):
db = get_db()
try:
user = db.execute('''SELECT user_id FROM user where email=?''', (email,))
except sqlite3.IntegrityError as inst:
raise inst
else:
return user.fetchone()
def validate_user(user):
db = get_db()
valid_user = None
try:
valid_user = db.execute('''
SELECT password FROM user
WHERE email=?
''', (user['email'],)).fetchone()
#pdb.set_trace()
if valid_user:
return pwd_context.verify(user['password'], valid_user[0])
except Exception:
raise UserLoginError('Email or password is incorrect')
else:
return False
@auto_commit
def insert_into_db(user, db=None):
if db is None:
db = get_db()
if not user['email'] or not user['password']:
raise UserRegistrationError('Email or Password not provided')
try:
password = pwd_context.encrypt(user['password'])
result = db.execute('''
INSERT INTO user(email, password) VALUES(?,?)
''', (user['email'], password))
except sqlite3.IntegrityError:
raise UserRegistrationError('Email already exists')
else:
return True
def get_marks_for(user_id):
db = get_db()
keys = ['mark_id','mark_author', 'mark_name', 'notes', 'location', 'user_id']
marks = None
print(user_id)
#pdb.set_trace()
try:
marks = db.execute('''
SELECT * FROM mark
WHERE user_id=?
''', (user_id,))
marks = marks.fetchall()
db.close()
except Exception as inst:
raise inst
else:
result = []
for mark in marks:
temp = dict(zip(keys, mark))
result.append(temp)
return result
@auto_commit
def add_mark(data,db=None):
if db is None:
db = get_db()
values = None
try:
values = list(map(lambda x: data.get(x), data.keys()))
except Exception as inst:
raise inst
try:
db.execute('''
INSERT INTO mark(mark_author, mark_name, notes, location, user_id)
VALUES(
?, ?, ?, ?, ?
)
''',values)
except sqlite3.IntegrityError as inst:
raise inst
else:
return True
@auto_commit
def delete_marks_for(user_id, db=None):
if db is None:
db = get_db()
try:
db.execute('''
DELETE FROM mark
where user_id=?
''', (user_id,))
except sqlite3.IntegrityError as inst:
raise inst
else:
return True
def get_mark(mark_id):
db = get_db()
mark = None
keys = ['mark_id','mark_author', 'mark_name', 'notes', 'location', 'user_id']
try:
mark = db.execute('''
SELECT * FROM mark
WHERE mark_id=?
''', (mark_id,))
mark = mark.fetchall()
db.close()
except sqlite3.IntegrityError as inst:
raise inst
else:
result = dict(zip(keys,list(mark[0])))
return result
@auto_commit
def update_mark(updates, mark_id, db=None):
if db is None:
db = get_db()
try:
values = list(map(lambda x: updates.get(x), updates.keys()))
values.append(mark_id)
db.execute('''
UPDATE mark
SET mark_author=?,
mark_name=?,
notes=?,
location=?
WHERE mark_id=?
''',values)
except sqlite3.IntegrityError as inst:
raise inst
else:
return True
@auto_commit
def delete_mark(mark_id, db=None):
if db is None:
db = get_db()
try:
db.execute('''
DELETE FROM mark
WHERE mark_id=?
''', (mark_id,))
except sqlite3.IntegrityError as inst:
raise inst
else:
return True
def get_serializer(expiration_time=24 * 60 * 60):
return Serializer(secret_key, expires_in=expiration_time)
def create_token(user_id, expiration_time=24 * 60 * 60):
s = get_serializer()
return s.dumps({'user_id': user_id})
def user_from_token(token):
s = get_serializer()
try:
data = s.loads(token)
except SignatureExpired:
return None
except BadSignature:
return None
else:
u_id = data['user_id']
#print(u_id)
#pdb.set_trace()
db = get_db()
user = db.execute('SELECT * FROM user WHERE user_id=?',(u_id[0],))
return user.fetchone()