-
Notifications
You must be signed in to change notification settings - Fork 0
/
app backup.py
325 lines (272 loc) · 8.61 KB
/
app backup.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
import os
from flask import Flask, render_template, json, request, redirect, session
from flask.ext.mysql import MySQL
from werkzeug import generate_password_hash, check_password_hash
mysql = MySQL()
app = Flask(__name__)
app.secret_key = 'nope'
#MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'balloon'
app.config['MYSQL_DATABASE_DB'] = 'BucketList'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
app.config['MYSQL_DATABASE_CHARSET'] = 'utf8mb4'
app.config['MYSQL_USE_UNICODE'] = 'True'
mysql.init_app(app)
@app.route("/")
def main():
if session.get('user'):
return redirect('/showDashboard')
else:
return render_template('index.html')
@app.route("/showSignin")
def showSignin():
return render_template('signin.html')
@app.route('/userHome')
def userHome():
if session.get('user'):
return render_template('userHome.html')
else:
return render_template('error.html',error= 'Unauthorized Access')
@app.route("/showAddWish")
def showAddWish():
return render_template('addWish.html')
@app.route('/showSignUp')
def showSignUp():
return render_template('signup.html')
@app.route('/logout')
def logout():
session.pop('user',None)
return redirect('/')
@app.route('/showDashboard')
def showDashboard():
return render_template('dashboard.html',user= session.get('username'))
@app.route('/signUp',methods=['POST','GET'])
def signUp():
try:
#read the posted values from the UI
_name = request.form['inputName']
_email = request.form['inputEmail']
_password = request.form['inputPassword']
#validate the received values
if _name and _email and _password:
# all good, let's call MySQL
conn = mysql.connect()
cursor = conn.cursor()
#salt the password using method from Werkzeug
_hashed_password = generate_password_hash(_password)
#call the database procedure
cursor.callproc('sp_createUser',(_name,_email,_hashed_password))
#check if it went through, if so commit
data = cursor.fetchall()
if len(data) is 0:
conn.commit()
return json.dumps({'message':'User created successfully !'})
else:
return json.dumps({'error':str(data[0])})
else:
return json.dumps({'html':'<span>Enter the required fields</span>'})
except Exception as e:
return json.dumps({'error':str(e)})
finally:
cursor.close()
conn.close()
return render_template('signin.html')
@app.route('/validateLogin',methods=['POST'])
def validateLogin():
try:
_username = request.form['inputEmail']
_password = request.form['inputPassword']
conn = mysql.connect()
cursor = conn.cursor()
#cursor.callproc('sp_validateLogin',(_username,))
cursor.execute("SELECT * FROM tbl_user where user_username='" + _username + "'")
data = cursor.fetchall()
if len(data) > 0:
row = data[0]
if check_password_hash(str(data[0][3]),_password):
session['user'] = row[0]
session['username'] = row[1]
return redirect('/showDashboard')
else:
return render_template('error.html',error = 'Wrong Email address or Password!')
else:
return render_template('error.html',error = 'Len not 0 Wrong Email address or Password!')
except Exception as e:
return render_template('error.html',error = str(e))
finally:
cursor.close()
conn.close()
@app.route('/addWish',methods=['POST'])
def addWish():
try:
if session.get('user'):
_title = request.form['inputTitle']
_description = request.form['inputDescription']
_user = session.get('user')
conn = mysql.connect()
cursor = conn.cursor()
cursor.callproc('sp_addWait',(_title,_description,_user))
data = cursor.fetchall()
if len(data) is 0:
conn.commit()
return redirect('/userHome')
else:
return render_template('error.html',error = 'An error occurred!')
#return json.dumps({'error':str(data[0])})
else:
return render_template('error.html',error = 'Unauthorized Access')
except Exception as e:
return render_template('error.html',error = str(e))
finally:
cursor.close()
conn.close()
@app.route('/getWait',methods=['GET'])
def getWait():
try:
if session.get('user'):
_user = session.get('user')
con = mysql.connect()
cursor = con.cursor()
cursor.callproc('sp_GetWaitByUser',(_user,))
waits = cursor.fetchall()
waits_dict = []
for wait in waits:
wait_dict = {
'Id': wait[0],
'Title': wait[1],
'Description': wait[2],
'Date': wait[4]}
waits_dict.append(wait_dict)
return json.dumps(waits_dict)
else:
return render_template('error.html', error = "Unauthorized Access")
except Exception as e:
return render_template('error.html', error = str(e))
@app.route('/getWaitById',methods=['POST'])
def getWaitById():
try:
if session.get('user'):
_id = request.form['id']
_user = session.get('user')
conn = mysql.connect()
cursor = conn.cursor()
cursor.callproc('sp_GetWaitById',(_id,_user,))
result = cursor.fetchall()
wait = []
wait.append({'Id':result[0][0],'Title':result[0][1],'Description':result[0][2]})
return json.dumps(wait)
else:
return render_template('error.html', error = 'Unauthorized Access')
except Exception, e:
return render_template('error.html', error = str(e))
finally:
cursor.close()
conn.close()
@app.route('/getAllWaits')
def getAllWaits():
try:
if session.get('user'):
_user = session.get('user')
conn = mysql.connect()
cursor = conn.cursor()
cursor.execute("SELECT tbl_wait.wait_id,tbl_wait.wait_title,tbl_wait.wait_description,getSum(wait_id),hasLiked(wait_id, %s),tbl_user.user_name,(TIMESTAMPDIFF(MINUTE, NOW(), wait_date) * - 1) FROM tbl_wait, tbl_user WHERE tbl_wait.wait_user_id = tbl_user.user_id ORDER BY wait_date DESC",(_user,))
# cursor.callproc('sp_GetAllWaitsTwo',(_user,))
result = cursor.fetchall()
waits_dict = []
for wait in result:
wait_dict = {
'Id': wait[0],
'Title': wait[1],
'Description': wait[2],
'Like': wait[3],
'HasLiked': wait[4],
'Poster': wait[5],
'TimeElapsed': wait[6]
}
waits_dict.append(wait_dict)
return json.dumps(waits_dict)
else:
return render_template('error.html', error = 'Unauthorized Access')
except Exception, e:
return render_template('error.html', error = str(e))
finally:
cursor.close()
conn.close()
@app.route('/updateWait', methods=['POST'])
def updateWish():
try:
if session.get('user'):
_user = session.get('user')
_title = request.form['title']
_description = request.form['description']
_wait_id = request.form['id']
conn = mysql.connect()
cursor = conn.cursor()
cursor.callproc('sp_updateWait',(_title,_description,_wait_id,_user))
data = cursor.fetchall()
if len(data) is 0:
conn.commit()
return json.dumps({'status':'OK'})
else:
return json.dumps({'status':'ERROR'})
except Exception as e:
return json.dumps({'status':'Unauthorized access'})
finally:
cursor.close()
conn.close()
@app.route('/deleteWait', methods=['POST'])
def deleteWait():
try:
if session.get('user'):
_user = session.get('user')
_id = request.form['id']
conn = mysql.connect()
cursor = conn.cursor()
cursor.callproc('sp_deleteWait',(_id,_user))
result = cursor.fetchall()
if len(result) is 0:
conn.commit()
return json.dumps({'status':'OK'})
else:
return json.dumps({'status':'An Error occured'})
else:
return render_template('error.html',error = 'Unauthorized Access')
except Exception, e:
return json.dumps({'status':str(e)})
finally:
cursor.close()
conn.close()
@app.route('/addUpdateLike',methods=['POST'])
def addUpdateLike():
try:
if session.get('user'):
_waitId = request.form['wait']
_user = session.get('user')
_like = request.form['like']
conn = mysql.connect()
cursor = conn.cursor()
cursor.callproc('sp_AddUpdateLikes',(_waitId,_user,_like))
result = cursor.fetchall()
if len(result) is 0:
conn.commit()
cursor.close()
conn.close()
conn = mysql.connect()
cursor = conn.cursor()
cursor.callproc('sp_getLikeStatus',(_waitId,_user,))
result = cursor.fetchall()
return json.dumps({'status':'OK','total':result[0][0],'likeStatus':result[0][1]})
else:
return render_template('error.html',error = 'An error occurred!')
else:
return render_template('error.html',error = 'Unauthorized Access')
except Exception, e:
return json.dumps({'status':str(e)})
finally:
cursor.close()
conn.close()
if __name__ == "__main__":
# Bind to PORT if defined, otherwise default to 5000.
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)