-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
108 lines (92 loc) · 2.86 KB
/
models.py
File metadata and controls
108 lines (92 loc) · 2.86 KB
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
from flask import make_response, request
from bson.objectid import ObjectId
import pymongo, jwt, hashlib
conn = pymongo.MongoClient("localhost", 27017)
db = conn.thoth_db
def verify():
col = db.user
encoded = request.cookies.get('accessToken')
decoded = jwt.decode(encoded, 'JEfWefI0E1qlnIz06qmob7cZp5IzH/i7KwOI2xqWfhE=', algorithms=["HS256"])
result = col.find_one({'_id': ObjectId(decoded['uid'])})
return result
def get_uid():
col = db.user
encoded = request.cookies.get('accessToken')
decoded = jwt.decode(encoded, 'JEfWefI0E1qlnIz06qmob7cZp5IzH/i7KwOI2xqWfhE=', algorithms=["HS256"])
return decoded['uid']
def note_exists(nid):
col = db.note
data = col.find_one({"_id": ObjectId(nid)})
return data
def scrab_exists(nid):
col = db.scrab
data = col.find_one({"_id": ObjectId(nid)})
return data
def scrab_already_exists(nid):
col = db.note
result = col.find_one({"_id": ObjectId(nid)})
col = db.scrab
data = col.find_one({"uid": get_uid(), "onid":str(result['_id'])})
return data
def email_already_exists(email):
col = db.user
data = col.find_one({"email": email})
return data
def sign_in(user):
col = db.user
m = hashlib.sha256()
pwd = user['password']
m.update(pwd.encode('utf-8'))
user['password'] = m.hexdigest()
result = col.find_one(user)
if result:
encoded = jwt.encode({"uid":str(result['_id'])}, 'JEfWefI0E1qlnIz06qmob7cZp5IzH/i7KwOI2xqWfhE=', algorithm='HS256')
resp = make_response('{"error": null}')
resp.set_cookie('accessToken', encoded, max_age=60*60*2)
return resp
else:
return None
def sign_up(user):
col = db.user
m = hashlib.sha256()
pwd = user['password']
m.update(pwd.encode('utf-8'))
user['password'] = m.hexdigest()
col.insert_one(user)
def get_notes(param):
col = db.note
count = int(param.get('count'))*10
data = list(col.find().skip(count).limit(10))
return data
def post_note(note):
col = db.note
note['uid'] = get_uid()
col.insert_one(note)
def update_note(note):
col = db.note
nid = note['nid']
result = col.update_one({"_id": ObjectId(nid)}, {"$set":note})
def delete_note(nid):
col = db.note
col.delete_one({"_id": ObjectId(nid)})
def get_mynote(param):
col = db.scrab
uid = get_uid()
count = int(param.get('count'))*10
data = list(col.find({"uid": uid}).skip(count).limit(10))
return data
def post_mynote(nid):
col = db.note
result = col.find_one({"_id": ObjectId(nid)})
data = {}
data['title'] = result['title']
data['code'] = result['code']
data['tag'] = result['tag']
data['ref'] = result['ref']
data['uid'] = get_uid()
data['onid'] = str(result['_id'])
col = db.scrab
col.insert(data)
def delete_mynote(nid):
col = db.scrab
col.delete_one({"_id": ObjectId(nid)})