-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
371 lines (288 loc) · 11.9 KB
/
app.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
from flask import Flask, render_template, request, json, redirect, session, jsonify
from flaskext.mysql import MySQL
from werkzeug.security import generate_password_hash, check_password_hash
import os, uuid
app = Flask(__name__)
app.secret_key = 'Famous'
mysql = MySQL()
# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'vnfdjqhk66'
app.config['MYSQL_DATABASE_DB'] = 'BucketList'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
app.config['UPLOAD_FOLDER'] = 'static/Uploads'
mysql.init_app(app)
@app.errorhandler(404)
def not_found(error=None):
return render_template('error.html', error='Not Found(404) {0}'.format(request.url))
@app.route('/')
def main():
return render_template('index.html')
@app.route('/userHome')
def user_home():
if session.get('user'):
return render_template('userHome.html')
else:
return render_template('error.html', error='Unauthorized Access')
@app.route('/updateWish/<int:wish_id>', methods=['PUT'])
def update_wish(wish_id):
conn = mysql.connect()
try:
if session.get('user'):
_user = session.get('user')
_title = request.form['title']
_description = request.form['description']
_wish_id = wish_id
_file_path = request.form['filePath']
_is_private = request.form['isPrivate']
_is_done = request.form['isDone']
with conn.cursor() as cursor:
sql = 'update tbl_wish set wish_title = %s, wish_description = %s, wish_file_path = %s, wish_private = %s, wish_accomplished = %s where wish_id = %s and wish_user_id = %s'
cursor.execute(sql, (_title, _description, _file_path, _is_private, _is_done, _wish_id, _user))
data = cursor.fetchall()
if len(data) == 0:
conn.commit()
return json.dumps({'status': 'OK'})
else:
return json.dumps({'status': 'ERROR'})
else:
return render_template('error.html', error='Unauthorized Access')
except Exception as e:
return render_template('error.html', error=str(e))
finally:
conn.close()
@app.route('/deleteWish/<int:wish_id>', methods=['DELETE'])
def delete_wish(wish_id):
conn = mysql.connect()
try:
if session.get('user'):
_id = wish_id
_user = session.get('user')
with conn.cursor() as cursor:
sql = 'delete from tbl_wish where wish_id = %s and wish_user_id = %s'
cursor.execute(sql, (_id, _user))
result = cursor.fetchall()
if len(result) is 0:
conn.commit()
return json.dumps({'status': 'OK'})
else:
return json.dumps({'status': 'Error'})
else:
return render_template('error.html', error='Unauthorized Access')
except Exception as e:
return render_template('error.html', error=str(e))
finally:
conn.close()
@app.route('/getWishById/<int:wish_id>')
def get_wish_by_id(wish_id):
try:
if session.get('user'):
_id = wish_id
_user = session.get('user')
with mysql.connect().cursor() as cursor:
sql = 'select * from tbl_wish where wish_id = %s and wish_user_id = %s'
cursor.execute(sql, (_id, _user))
result = cursor.fetchall()
wish = [{'Id': result[0][0], 'Title': result[0][1], 'Description': result[0][2],
'FilePath': result[0][5], 'Done': result[0][6], 'Private': result[0][7]}]
return json.dumps(wish)
else:
return render_template('error.html', error='Unauthorized Access')
except Exception as e:
return render_template('error.html', error=str(e))
@app.route('/getWish/<int:offset>')
def get_wish(offset):
try:
if session.get('user'):
_user = session.get('user')
_limit = int(request.args['itemPerPage'])
_offset = offset
with mysql.connect().cursor() as cursor:
number_sql = '(select count(*), 1, 2, 3, 4, 5, 6, 7 from tbl_wish where wish_user_id = %s)'
data_sql = '(select * from tbl_wish where wish_user_id = %s order by wish_date desc limit %s offset %s)'
sql = ' union '.join([number_sql, data_sql])
cursor.execute(sql, (_user, _user, _limit, _offset))
wishes = cursor.fetchall()
response = [{'total': wishes[0][0]}]
wishes = wishes[1:]
wishes_dict = []
for wish in wishes:
wishes_dict.append({
'Id': wish[0],
'Title': wish[1],
'Description': wish[2],
'Date': wish[4]
})
response.append(wishes_dict)
return json.dumps(response)
else:
return render_template('error.html', error='Unauthorized Access')
except Exception as e:
return render_template('error.html', error=str(e))
@app.route('/showAddWish')
def show_add_wish():
return render_template('addWish.html')
@app.route('/addWish', methods=['POST'])
def add_wish():
conn = mysql.connect()
try:
if session.get('user'):
_title = request.form['inputTitle']
_description = request.form['inputDescription']
_user = session.get('user')
_file_path = '' if request.form.get('filePath') is None \
else request.form.get('filePath')
_done = 0 if request.form.get('done') is None else 1
_private = 0 if request.form.get('private') is None else 1
with conn.cursor() as cursor:
sql = 'insert into tbl_wish(wish_title, wish_description, wish_user_id, wish_file_path, wish_accomplished, wish_private) values(%s, %s, %s, %s, %s, %s)'
cursor.execute(sql, (_title, _description, _user, _file_path, _done, _private))
cursor.fetchone()
conn.commit()
return redirect('/userHome')
else:
return render_template('error.html', error='Unauthorized Access')
except Exception as e:
return render_template('error.html', error=str(e))
finally:
conn.close()
@app.route('/logout')
def logout():
session.pop('user', None)
return redirect('/')
@app.route('/showSignUp')
def show_sign_up():
return render_template('signup.html')
@app.route('/showSignIn')
def show_sign_in():
return render_template('signin.html')
@app.route('/validateLogin', methods=['POST'])
def validate_login():
conn = mysql.connect()
try:
with conn.cursor() as cursor:
_username = request.form['inputEmail']
_password = request.form['inputPassword']
sql = 'select * from tbl_user where user_username = %s'
cursor.execute(sql, (_username,))
data = cursor.fetchall()
if len(data) > 0:
if check_password_hash(str(data[0][3]), _password):
session['user'] = data[0][0]
return redirect('/userHome')
else:
return render_template('error.html', error='Wrong Email address or Password.')
else:
render_template('error.html', error='Wrong Email address or Password.')
except Exception as e:
return render_template('error.html', error=str(e))
finally:
conn.close()
@app.route('/signUp', methods=['POST'])
def sign_up():
_name = request.form['inputName']
_email = request.form['inputEmail']
_password = request.form['inputPassword']
if _name and _email and _password:
conn = mysql.connect()
try:
with conn.cursor() as cursor:
_hash_password = generate_password_hash(_password)
sql = 'select * from tbl_user where user_username = %s'
cursor.execute(sql, (_email,))
data = cursor.fetchall()
if len(data) != 0:
resp = jsonify('{0} is already exist!!'.format(_email))
resp.status_code = 400
return resp
sql = 'insert into tbl_user(user_name, user_username, user_password) values(%s, %s, %s)'
cursor.execute(sql, (_name, _email, _hash_password))
conn.commit()
return redirect('/userHome.html')
except Exception as e:
return render_template('error.html', error=str(e))
finally:
conn.close()
else:
resp = jsonify('Enter the required field!!')
resp.status_code = 40
return resp
@app.route('/upload', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
file = request.files['file']
extension = os.path.splitext(file.filename)[1]
f_name = str(uuid.uuid4()) + extension
file.save(os.path.join(app.config['UPLOAD_FOLDER'], f_name))
return json.dumps({'filename': f_name})
@app.route('/showDashboard')
def show_dashboard():
return render_template('dashboard.html')
@app.route('/getAllWishes')
def get_all_wishes():
conn = mysql.connect()
try:
if not session.get('user'):
return render_template('error.html', error='Unauthorized Access')
with conn.cursor() as cursor:
sql = 'select wish_id as id, wish_title, wish_description, wish_file_path, ' \
'(select sum(wish_like) from tbl_likes where wish_id=id), ' \
'(select exists (select 1 from tbl_likes where wish_id=id and user_id=%s)) from tbl_wish where wish_private = 0;'
cursor.execute(sql, (session.get('user'),))
result = cursor.fetchall()
wishes_dict = []
for wish in result:
wishes_dict.append({
'Id': wish[0],
'Title': wish[1],
'Description': wish[2],
'FilePath': wish[3],
'Like': 0 if not wish[4] else int(wish[4]),
'HasLiked': wish[5]
})
return json.dumps(wishes_dict)
except Exception as e:
return render_template('error.html', error=str(e))
finally:
conn.close()
@app.route('/addUpdateLike', methods=['POST'])
def add_update_like():
conn = mysql.connect()
try:
if not session.get('user'):
return render_template('error.html', error='Unauthorized Access')
_wish_id = request.form['wish']
_user = session.get('user')
has_liked = False
with conn.cursor() as cursor:
sql = 'select like_id, user_id, wish_like from tbl_likes where wish_id=%s'
cursor.execute(sql, (_wish_id,))
data = cursor.fetchall()
like_id = None
total = len(data)
for row in data:
if row[1] == _user:
has_liked = True
like_id = row[0]
break
if not has_liked:
sql = 'insert into tbl_likes(wish_like, wish_id, user_id) values(1, %s, %s)'
cursor.execute(sql, (_wish_id, _user))
total += 1
has_liked = True
else:
sql = 'delete from tbl_likes where like_id=%s'
cursor.execute(sql, (like_id,))
total -= 1
has_liked = False
cursor.fetchone()
conn.commit()
return json.dumps({'status': 'OK',
'total': total,
'likeStatus': has_liked})
except Exception as e:
return render_template('error.html', error=str(e))
finally:
conn.close()
if __name__ == '__main__':
app.run()