-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
194 lines (160 loc) · 5.76 KB
/
app.py
File metadata and controls
194 lines (160 loc) · 5.76 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
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
from bottle import route, run, template, static_file, post, get, request, response, delete, jinja2_view, redirect
import json
import pymysql
from uuid import uuid4
import hashlib
import functools
connection = pymysql.connect(
host="localhost",
user="root",
password="root",
db="paint",
charset="utf8",
cursorclass=pymysql.cursors.DictCursor
)
view = functools.partial(jinja2_view, template_lookup=['./'])
@route('/static/css/<filename:re:.*\.css>')
def getCss(filename):
return static_file(filename, root='static/css')
@get('/static/js/<filename:re:.*\.js>')
def javascripts(filename):
return static_file(filename, root='static/js')
@get('/static/lib/<filename:re:.*\.js>')
def lib(filename):
return static_file(filename, root='static/lib')
@get('/')
def index():
username = request.get_cookie("username")
session_id = request.get_cookie("session_id")
res = static_file("/index.html", root="")
if not is_user_logged_in(username, session_id):
res.set_cookie('session_id', '', expires=0)
res.set_cookie('username', '', expires=0)
return res
@post('/save')
def save_painting():
username = request.get_cookie("username")
name = request.json.get("name")
painting = json.dumps(request.json.get("painting"))
try:
with connection.cursor() as cursor:
query = "INSERT INTO paintings (username, name, painting) values ('{}', '{}', '{}' )".format(
username,
name,
painting
)
cursor.execute(query)
connection.commit()
return {"saved": True}
except Exception as e:
print(e)
response.status = 500
response["status__line"] = "error saving painting in the DB"
return response
@delete('/delete')
def delete_painting():
username = request.get_cookie("username")
name = request.query.get("name")
try:
with connection.cursor() as cursor:
query = "DELETE FROM paintings WHERE username='{}' and name='{}' ".format(username, name)
cursor.execute(query)
connection.commit()
return response
except Exception as e:
print(e)
response.status = 500
response["status__line"] = "error writing to DB on save"
return response
@get('/paintings')
def get_paintings():
username = request.get_cookie("username")
try:
with connection.cursor() as cursor:
query = "SELECT name from paintings where username='{}'".format(username)
cursor.execute(query)
paintings = cursor.fetchall()
return json.dumps({"paintings": paintings})
except Exception as e:
print(e)
return json.dumps("error writing to DB")
@get('/painting')
def get_painting():
username = request.get_cookie("username")
name = request.query.name
try:
with connection.cursor() as cursor:
query = "SELECT painting from paintings where username='{}' and name='{}'".format(username, name)
cursor.execute(query)
painting = cursor.fetchone()
return json.dumps(painting)
except Exception as e:
print(e)
return json.dumps("error writing to DB")
@post('/login')
def signup():
username = request.forms.get("username")
password = request.forms.get("password")
hashed_password = hash_password(password)
session_id = request.get_cookie("session_id")
new_session_id = get_user_session_id()
if is_user_logged_in(username, session_id):
return template('index.html')
if not is_user_exist(username, hashed_password):
add_new_user(username, hashed_password, new_session_id)
update_user_session(username, hashed_password, new_session_id)
response.set_cookie("session_id", new_session_id)
response.set_cookie("username", username)
redirect('/')
def get_user_session_id():
return uuid4().hex[:8]
def hash_password(password):
salt = "13Ebu54"
return hashlib.md5((salt + password).encode('utf-8')).hexdigest()
def is_user_logged_in(username, session_id):
if not session_id:
return False
try:
with connection.cursor() as cursor:
query = "SELECT * FROM users WHERE username = '{}' AND sessionId = '{}'".format(
username, session_id)
cursor.execute(query)
result = cursor.fetchone()
return result is not None
except Exception as e:
print(e)
return False
def is_user_exist(username, password):
try:
with connection.cursor() as cursor:
query = "SELECT * FROM users WHERE username = '{}' AND password = '{}'".format(
username, password)
cursor.execute(query)
result = cursor.fetchone()
return result is not None
except Exception:
return False
def add_new_user(username, password, session_id):
try:
with connection.cursor() as cursor:
query = "INSERT INTO users (username, password, sessionId) values ('{}', '{}', '{}' )".format(
username,
password,
session_id
)
cursor.execute(query)
connection.commit()
except Exception as e:
print(e)
return json.dumps("error writing to DB")
def update_user_session(username, password, session_id):
try:
with connection.cursor() as cursor:
query = "UPDATE users SET sessionId = '{}' WHERE username = '{}' AND password = '{}'".format(
session_id, username, password)
cursor.execute(query)
connection.commit()
except Exception as e:
print(e)
if __name__ == "__main__":
run(host='localhost', port=8000, reloader=True)