/
application.py
463 lines (373 loc) · 15.6 KB
/
application.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#####################
# PixelGram Backend #
#####################
# application.py #
#####################
# Includes
import os, uuid, re
from flask import Flask, render_template, json, redirect, session, request
from flask.ext.mysqldb import MySQL
from werkzeug import generate_password_hash, check_password_hash, secure_filename
import resizr
application = Flask(__name__)
mysql = MySQL()
# 504 bit session key
application.secret_key = '?KzNp>j0-7ec;4c9zG]@tjrBy3uCZNeEsDFm*!%buG7A97?#3ANL*97;D?(jpe9'
# Config MySQL
# Don't run as root in production!
application.config['MYSQL_USER'] = 'root'
application.config['MYSQL_PASSWORD'] = 'pythondev'
application.config['MYSQL_DB'] = 'PixelGram'
application.config['MYSQL_HOST'] = 'localhost'
mysql.init_app(application)
# Define uploads folder and allowed file types - Should not be changed for now!
application.config['UPLOAD_FOLDER'] = 'static/uploads'
application.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024
ALLOWED_EXTENSIONS = set(['jpg'])
# Check the uploads folder exists, otherwise create it
print("INFO: Checking if upload folder exists")
if not os.path.exists(application.config['UPLOAD_FOLDER']):
try:
print("WARN: Upload folder does not exist, creating it")
os.makedirs(application.config['UPLOAD_FOLDER'])
except Exception as e:
print(e)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
# App routing
@application.route('/')
def main():
# If already logged in, redirect to the feed
if session.get('user'):
return redirect('/showFeed')
# Otherwise show the homepage
else:
return render_template('index.html')
@application.route('/showSignUp')
def showSignUp():
return render_template('signup.html')
@application.route('/signUp', methods=['POST'])
def signUp():
try:
# Read values posted from page
_name = request.form['inputName']
_email = request.form['inputEmail']
_password = request.form['inputPassword']
# Validate received values
if _name and _email and _password:
print(_email)
if re.match(r"[^@\s]+@[^@\s]+\.[a-zA-Z0-9]+$", _email):
print("Matched regex!")
# Contact MySQL and set cursor
# MySQLdb handles opening and closing the connection as needed
cur = mysql.connection.cursor()
# We need to hash a salted password to store it securely
_hashed_password = generate_password_hash(_password)
# Call MySQL procedure to create user
cur.callproc('sp_createUser', (_name, _email, _hashed_password))
# Fetch from cursor
rv = cur.fetchall()
cur.close()
if len(rv) is 0:
mysql.connection.commit()
return json.dumps({'success':'User create success! You may now log in.'})
else:
return json.dumps({'error':str(rv[0])})
else:
print("Didn't match regex!")
return json.dumps({'error':'Error: Enter the required fields'})
else:
return json.dumps({'error':'Error: Enter the required fields'})
except Exception as e:
return json.dumps({'error':str(e)})
@application.route('/showSignIn')
def showSignIn():
return render_template('signin.html')
@application.route('/validateLogin', methods=['POST'])
def validateLogin():
# Read values posted from page
try:
_username = request.form['inputEmail']
_password = request.form['inputPassword']
# Connect to MySQL, set cursor and call proc
cur = mysql.connection.cursor()
cur.callproc('sp_validateLogin', (_username,))
# Fetch from cursor
rv = cur.fetchall()
# If entry exists, check password matches stored hash
if len(rv) > 0:
if check_password_hash(str(rv[0][3]), _password):
# Set user session id and redirect
session['user'] = rv[0][0]
return redirect('/showFeed')
else:
return render_template('error.html', error = 'Invalid Email/Password combination.')
else:
return render_template('error.html', error = 'Invalid Email/Password combination.')
except Exception as e:
return render_template('error.html', error = str(e))
finally:
cur.close()
@application.route('/userHome')
def userHome():
# Only allow logged in users
if session.get('user'):
return render_template('userHome.html')
else:
return render_template('error.html', error = 'Please Login to access the user home.')
@application.route('/logout')
def logout():
# Set user session to null
session.pop('user', None)
return redirect('/')
@application.route('/showNewPost')
def showNewPost():
return render_template('newPost.html')
@application.route('/addNewPost', methods=['POST'])
def addNewPost():
#try:
if(session.get('user')):
_title = request.form['inputTitle']
_description = request.form['inputDescription']
_user = session.get('user')
_filePath = request.form['filePath']
if _title and _filePath:
print (_title, " ", _description, " ", _user)
# Connect to MySQL, set cursor and call proc
cur = mysql.connection.cursor()
cur.callproc('sp_newPost', (_title, _description, _user, _filePath))
# Fetch from cursor
rv = cur.fetchall()
if len(rv) is 0:
# Commit the post
mysql.connection.commit()
# Resize uploaded image
devices = resizr.ResizeForAll(_filePath)
# Add resized image paths to database
cur.execute("SELECT post_id FROM tbl_post WHERE post_file_path = %s", (_filePath,))
postid = cur.fetchall()
for device in devices:
cur.execute("INSERT INTO tbl_postdevices (post_id, device_id) VALUES (%s, %s)", (postid[0][0], device))
mysql.connection.commit()
return redirect('/userHome')
else:
return render_template('error.html', error = 'An error occurred!')
else:
return render_template('error.html', error = 'Please enter a title and choose an image to upload.')
else:
return render_template('error.html', error = 'Unauthorised Access!')
#except Exception as e:
# print (str(e))
# return render_template('error.html', error = str(e))
#finally:
# cur.close()
@application.route('/getPost')
def getPost():
try:
if session.get('user'):
_user = session.get('user')
# Connect to MySQL and fetch all posts for user
cur = mysql.connection.cursor()
cur.callproc('sp_getPostByUser', (_user,))
posts = cur.fetchall()
posts_dict = []
for post in posts:
post_dict = {
'Id': post[0],
'Title': post[1],
'Description': post[2],
'Date': post[4]}
posts_dict.append(post_dict)
return json.dumps(posts_dict)
else:
# This is a poor way to return an error, need to return JSON and have ajax check for it
return render_template('error.html', error = 'Unauthorised access')
except Exception as e:
return render_template('error.html', error = str(e))
finally:
cur.close()
@application.route('/getPostById', methods=['POST'])
def getPostById():
try:
if session.get('user'):
_id = request.form['id']
_user = session.get('user')
cur = mysql.connection.cursor()
cur.callproc('sp_getPostById', (_id, _user))
rv = cur.fetchall()
# Convert returned post to list
post = []
post.append({'Id':rv[0][0],'Title':rv[0][1],'Description':rv[0][2],'FilePath':rv[0][5]})
return json.dumps(post)
else:
return render_template('error.html', error = 'Unauthorised access')
except Exception as e:
return render_template('error.html', error = str(e))
finally:
cur.close()
@application.route('/updatePost', methods=['POST'])
def updatePost():
try:
if session.get('user'):
_user = session.get('user')
_title = request.form['title']
_description = request.form['description']
_post_id = request.form['id']
_filePath = request.form['filePath']
cur = mysql.connection.cursor()
cur.callproc('sp_updatePost', (_title, _description, _post_id, _user, _filePath))
rv = cur.fetchall()
if len(rv) is 0:
mysql.connection.commit()
# Resize uploaded image
devices = resizr.ResizeForAll(_filePath)
# Clear any existing entries
cur.execute("DELETE FROM tbl_postdevices WHERE post_id = %s", (_post_id,))
mysql.connection.commit()
for device in devices:
cur.execute("INSERT INTO tbl_postdevices (post_id, device_id) VALUES (%s, %s)", (_post_id, device))
mysql.connection.commit()
return json.dumps({'status':'OK'})
else:
return json.dumps({'status':'ERROR'})
except Exception as e:
return json.dumps({'error':str(e)})
finally:
# TODO: Fix this so that an error doesn't kill the server
cur.close()
@application.route('/deletePost', methods=['POST'])
def deletePost():
if session.get('user'):
_post_id = request.form['id']
_user = session.get('user')
cur = mysql.connection.cursor()
cur.callproc('sp_deletePost', (_post_id, _user))
rv = cur.fetchall()
if len(rv) is 0:
mysql.connection.commit()
return json.dumps({'status':'OK'})
else:
return json.dump({'status': 'Error'})
cur.close()
else:
return render_template('error.html', error = "Unauthorised access!")
@application.route('/upload', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
# Determine the extension of the file
extension = os.path.splitext(file.filename)[1]
# Generate unique filename
f_name = str(uuid.uuid4()) + extension
# Check the upload folder exists, otherwise create it
# Check the uploads folder exists, otherwise create it
print("INFO: Checking if upload folder exists")
if not os.path.exists(application.config['UPLOAD_FOLDER']):
try:
print("WARN: Upload folder does not exist, creating it")
os.makedirs(application.config['UPLOAD_FOLDER'])
except Exception as e:
print(e)
# Save file
file.save(os.path.join(application.config['UPLOAD_FOLDER'], f_name))
# Resize file
#devices = resizr.ResizeForAll(application.config['UPLOAD_FOLDER'] + "/" + f_name)
#print(devices)
return json.dumps({'filename':f_name})
else:
return json.dumps({'error':'Invalid filetype, JPG only!'})
@application.route('/showFeed')
def showFeed():
if session.get('user'):
return render_template('feed.html')
else:
return render_template('error.html', error = 'Please Login to access the feed.')
@application.route('/getAllPosts')
def getAllPosts():
try:
if session.get('user'):
_user = session.get('user')
# Open MySQL connnection and call SP
cur = mysql.connection.cursor()
cur.callproc('sp_getAllPosts', (_user,))
rv = cur.fetchall()
# Create data structure for posts from returned data
posts_dict = []
for post in rv:
post_dict = {
'Id': post[0],
'Title': post[1],
'Description': post[2],
'FilePath': post[3],
'Like': post[4],
'HasLiked': post[5]
}
posts_dict.append(post_dict)
cur.close()
# Return data struct to browser
return json.dumps(posts_dict)
else:
return render_template('error.html', error = "Unauthorised Access")
except Exception as e:
return render_template('error.html', error = str(e))
@application.route('/addUpdateLike', methods=['POST'])
def addUpdateLike():
if session.get('user'):
_postId = request.form['post']
_like = request.form['like']
_user = session.get('user')
cur = mysql.connection.cursor()
cur.callproc('sp_AddUpdateLikes', (_postId, _user, _like))
rv = cur.fetchall()
if len(rv) is 0:
mysql.connection.commit()
cur.close()
else:
cur.close()
return render_template('error.html', error = "An error occurred!")
cur = mysql.connection.cursor()
cur.callproc('sp_getLikeStatus', (_postId, _user))
rv = cur.fetchall()
cur.close()
return json.dumps({'status':'OK', 'total':rv[0][0], 'likeStatus':rv[0][1]})
else:
return render_template('error.html', error = "Unauthorised access")
@application.route('/getImageSizes', methods=['POST'])
def getImageSizes():
if session.get('user'):
# Request the image ID to query
_post_id = request.form['post']
# Return the UUID of the post's image
cur = mysql.connection.cursor()
cur.execute("SELECT post_file_path FROM tbl_post WHERE post_id = %s", (_post_id,))
rv = cur.fetchall()
if len(rv) > 0:
uuid = os.path.split(rv[0][0])[1]
else:
cur.close()
return json.dumps({'error':'Post does not exist in database'})
# Return available device IDs for post
cur.execute("SELECT device_id FROM tbl_postdevices WHERE post_id = %s", (_post_id,))
availableDevices = cur.fetchall()
# Query devices.json to match device id to device name
with open('devices.json', 'r') as data_file:
json_obj = json.load(data_file)
# Create data structure for posts from returned data
post_devices_dict = []
for post in availableDevices:
# Generate device name and file path
deviceName = json_obj['device'][post[0]]['name']
filePath = application.config['UPLOAD_FOLDER'] + "/" + str(post[0]) + "/" + uuid
post_dict = {
'Device': deviceName,
'FilePath': filePath
}
post_devices_dict.append(post_dict)
cur.close()
# Return data struct to browser
return json.dumps(post_devices_dict)
# If running from Python CLI, run on local debugging server
if __name__ == "__main__":
application.run(debug=True)