-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
691 lines (515 loc) · 18.3 KB
/
main.py
File metadata and controls
691 lines (515 loc) · 18.3 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
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
# Made with ❤️ by Sameeksha Khadka
from flask import Flask, jsonify, render_template, request, session, redirect
from werkzeug.security import generate_password_hash, check_password_hash
from os import path, environ
from dotenv import load_dotenv
# for database
from flaskext.mysql import MySQL
from pymysql.err import IntegrityError
from flask_session import Session
# for unique tokens
import uuid
app = Flask(__name__)
# databse setting for mysql
# app.config['MYSQL_DATABASE_HOST'] = 'localhost'
# app.config['MYSQL_DATABASE_PORT'] = 3306
# app.config["MYSQL_DATABASE_USER"] = "samee"
# app.config["MYSQL_DATABASE_PASSWORD"] = "****"
# app.config["MYSQL_DATABASE_DB"] = "dbase_trekapp"
basedir = path.abspath(path.dirname(__file__))
load_dotenv(path.join(basedir, ".env"))
SECRET_KEY = environ.get("SECRET_KEY") or "hard to guess string"
DEBUG = True
MYSQL_HOST = environ.get("MYSQL_HOST")
MYSQL_USER = environ.get("MYSQL_USER")
MYSQL_PASSWORD = environ.get("MYSQL_PASSWORD")
MYSQL_DB = environ.get("MYSQL_DB")
app.config["SECRET_KEY"] = SECRET_KEY
app.config["MYSQL_DATABASE_USER"] = MYSQL_USER
app.config["MYSQL_DATABASE_PASSWORD"] = MYSQL_PASSWORD
app.config["MYSQL_DATABASE_DB"] = MYSQL_DB
mysql = MySQL(app)
# session settings
# false means no time limit on login
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
@app.route("/")
def home():
logged_in_user = None
if session.get("email"):
logged_in_user = session["email"]
return render_template("index.html", result={"logged_in_user": logged_in_user})
@app.route("/register")
def register():
return render_template("register.html")
@app.route("/login")
def login():
return render_template("login.html")
# methods = is added only for post method
@app.route("/doLogin", methods=["POST"])
def doLogin():
# 'email' is name="email" of login.html in <input of email field
email = request.form["email"]
password = request.form["psw"]
# to give place to hit query of mysql->cursor
cursor = mysql.get_db().cursor()
resp = cursor.execute(
"""SELECT id,email ,full_name FROM Users WHERE email=%s and password=%s;""",
(email, password),
)
# fetchall() is for select
user = cursor.fetchone()
cursor.close()
# print(user)
# if email/password entered and email/password in database match response=1
if resp == 1:
session["email"] = email
session["userId"] = user[0]
logged_in_user = session.get("email")
# session['userId'] = user[0]
return render_template("home.html", result={"logged_in_user": logged_in_user})
else:
return render_template("login.html", result="Invalid Credentials")
@app.route("/doRegister", methods=["POST"])
def doRegister():
full_name = request.form["full_name"]
email = request.form["email"]
phone_number = request.form["phone_number"]
address = request.form["address"]
password = request.form["psw"]
hashed_password = generate_password_hash(password)
cursor = mysql.get_db().cursor()
resp = cursor.execute(
"""INSERT INTO `Users` (`id`, `full_name`, `address`, `email`, `phone_number`, `password`) VALUES (NULL, %s, %s, %s, %s, %s);""",
(full_name, address, email, phone_number, hashed_password),
)
# .commit() is needed to be done for all the sql quries where you add some data to table like insert and update but not select
mysql.get_db().commit()
cursor.close()
# return "Hello"
if resp == 1:
return render_template("login.html")
else:
return render_template(
"register.html", result="User not registered successfully"
)
# for trek destinations
@app.route("/treks")
def allTreks():
cursor = mysql.get_db().cursor()
cursor.execute(
"""SELECT td.id as 'SNO', td.title as 'Title',
td.days AS 'Days' , td.difficulty as 'Difficulty'
,td.total_cost as 'Total cost', td.upvotes as 'Upvotes',
u.full_name as 'Full name',u.id as 'userId' FROM `Trek_Destinations`
as td JOIN Users as u ON td.user_id=u.id;"""
)
treks = cursor.fetchall()
cursor.close()
logged_in_user = None
if session.get("email"):
logged_in_user = session["email"]
if session.get("userId"):
userId = session.get("userId")
return render_template(
"listing.html",
result={"treks": treks, "logged_in_user": logged_in_user, "userId": userId},
)
# to render trek destination details
@app.route("/trek/<int:trekId>")
def getTrekbyId(trekId):
cursor = mysql.get_db().cursor()
cursor.execute(
"""SELECT td.id as 'SNO', td.title as 'Title',
td.days AS 'Days' , td.difficulty as 'Difficulty'
,td.total_cost as 'Total cost', td.upvotes as 'Upvotes',
u.full_name as 'Full name' FROM `Trek_Destinations`
as td JOIN Users as u ON td.user_id=u.id where td.id=%s;""",
(trekId,),
)
trek = cursor.fetchone()
cursor.close()
# for Itineraries start a new cursor
cursor = mysql.get_db().cursor()
cursor.execute(
"""SELECT * FROM `Itineraries` WHERE `trek_destination_id`=%s;""", (trekId,)
)
Itineraries = cursor.fetchall()
cursor.close()
return render_template(
"trekdetail.html", result={"trek": trek, "Itineraries": Itineraries}
)
@app.route("/logout")
def logout():
session["email"] = None
session["userId"] = None
return redirect("/")
@app.route("/addTrek")
def addTrek():
if session.get("email"):
logged_in_user = session["email"]
return render_template("addtrek.html", result={"logged_in_user": logged_in_user})
@app.route("/doAddTrek", methods=["POST"])
def doAddTrek():
logged_in_user = None
if session.get("email"):
logged_in_user = session["email"]
title = request.form["title"]
days = request.form["days"]
difficulty = request.form["difficulty"]
total_cost = request.form["total_cost"]
upvotes = 0
cursor = mysql.get_db().cursor()
cursor.execute("""SELECT id FROM `Users` WHERE `email`=%s;""", (logged_in_user,))
user = cursor.fetchone()
cursor.close()
# pulling id from database
userId = user[0]
cursor = mysql.get_db().cursor()
cursor.execute(
"""INSERT INTO `Trek_Destinations` VALUES (NULL, %s, %s, %s, %s, %s,%s);""",
(title, days, difficulty, total_cost, upvotes, userId),
)
mysql.get_db().commit()
cursor.close()
return redirect("/treks")
# to edit treks
@app.route("/editTrek/<int:trekId>")
def editTrek(trekId):
logged_in_user = None
if session.get("email"):
logged_in_user = session["email"]
cursor = mysql.get_db().cursor()
cursor.execute(
"""SELECT td.id as 'SNO', td.title as 'Title',
td.days AS 'Days' , td.difficulty as 'Difficulty'
,td.total_cost as 'Total cost', td.upvotes as 'Upvotes',
u.full_name as 'Full name' FROM `Trek_Destinations`
as td JOIN Users as u ON td.user_id=u.id where td.id=%s;""",
(trekId,),
)
trek = cursor.fetchone()
cursor.close()
return render_template(
"editTrek.html", result={"trek": trek, "logged_in_user": logged_in_user}
)
@app.route("/doUpdateTrek", methods=["POST"])
def doUpdateTrek():
logged_in_user = None
if session.get("email"):
logged_in_user = session["email"]
title = request.form["title"]
days = request.form["days"]
difficulty = request.form["difficulty"]
total_cost = request.form["total_cost"]
trekId = request.form["trekId"]
cursor = mysql.get_db().cursor()
cursor.execute(
"""UPDATE `Trek_Destinations` SET `title`=%s,`days`=%s,`difficulty`=%s,`total_cost`=%s WHERE`id`=%s;""",
(title, days, difficulty, total_cost, trekId),
)
mysql.get_db().commit()
cursor.close()
return redirect("/treks")
# to delete the trek
@app.route("/doDeleteTrek/<int:trekId>")
def doDelete(trekId):
cursor = mysql.get_db().cursor()
cursor.execute("""DELETE from `Trek_Destinations` WHERE `id`=%s;""", (trekId,))
mysql.get_db().commit()
cursor.close()
return redirect("/treks")
@app.route("/addIternary")
def addIternary():
logged_in_user = None
if session.get("email"):
logged_in_user = session["email"]
cursor = mysql.get_db().cursor()
# pulling id from session without going to database
userId = None
if session.get("userId"):
userId = session.get("userId")
cursor.execute(
"""SELECT id,title from `Trek_Destinations` where user_id=%s ;""", (userId,)
)
treks = cursor.fetchall()
cursor.close()
return render_template(
"addIternary.html", result={"treks": treks, "logged_in_user": logged_in_user}
)
@app.route("/doAddIternary", methods=["POST"])
def doAddIternary():
logged_in_user = None
if session.get("email"):
logged_in_user = session["email"]
title = request.form["title"]
day = request.form["day"]
start_place = request.form["start_place"]
end_place = request.form["end_place"]
description = request.form["description"]
duration = request.form["duration"]
cost = request.form["cost"]
trek_destination_id = request.form["trek_destination_id"]
cursor = mysql.get_db().cursor()
cursor.execute(
"""INSERT INTO `Itineraries` VALUES (NULL,%s, %s, %s, %s, %s,%s,%s,%s)""",
(
title,
day,
start_place,
end_place,
description,
duration,
cost,
trek_destination_id,
),
)
mysql.get_db().commit()
cursor.close()
return redirect("/treks")
@app.route("/iternary/<int:trekId>")
def getIternaryByTrekId(trekId):
# for Itineraries start a new cursor
cursor = mysql.get_db().cursor()
cursor.execute(
"""SELECT * FROM `Itineraries` WHERE `trek_destination_id`=%s;""", (trekId,)
)
itineraries = cursor.fetchall()
print(itineraries)
cursor.close()
return render_template(
"iternary.html", result={"trekId": trekId, "itineraries": itineraries}
)
# afno trek destinations matra dekhauney
@app.route("/myTreks/<string:param>")
def getTreksbyUser(param):
userId = None
if session.get("email"):
logged_in_user = session["email"]
if session.get("userId"):
userId = session.get("userId")
cursor = mysql.get_db().cursor()
if param == "user":
cursor.execute(
"""SELECT * FROM `Trek_Destinations` WHERE `user_id`=%s;""", (userId,)
)
else:
cursor.execute("""SELECT * FROM `Trek_Destinations`;""")
treks = cursor.fetchall()
cursor.close()
return render_template("mytreks.html", result={"treks": treks, "userId": userId})
# for searching
@app.route("/search/treks")
def search():
keyword = request.args.get("keyword")
cursor = mysql.get_db().cursor()
searchString = "%" + keyword + "%"
cursor.execute(
"""SELECT * FROM `Trek_Destinations`
WHERE `title` Like %s;""",
(searchString,),
)
treks = cursor.fetchall()
cursor.close()
logged_in_user = None
if session.get("email"):
logged_in_user = session["email"]
if session.get("userId"):
userId = session.get("userId")
result = {"treks": treks, "logged_in_user": logged_in_user}
return render_template("mytreks.html", result={"treks": treks, "userId": userId})
# """
# API INTERFACES DEFINED FROM HERE
# """
@app.route("/api/doRegister", methods=["POST"])
def doRegisterAPI():
print(request.json)
full_name = request.json["full_name"]
email = request.json["email"]
phone_number = request.json["phone_number"]
address = request.json["address"]
password = request.json["psw"]
hashed_password = generate_password_hash(password)
token = uuid.uuid4()
cursor = mysql.get_db().cursor()
try:
cursor.execute(
"""INSERT INTO `Users` (`id`, `full_name`, `address`, `email`, `phone_number`, `password`, `token`) VALUES (NULL, %s, %s, %s, %s, %s, %s);""",
(full_name, address, email, phone_number, hashed_password, token),
)
# .commit() is needed to be done for all the sql quries where you add some data to table like insert and update but not select
mysql.get_db().commit()
cursor.close()
except IntegrityError:
return jsonify({"result": "Duplicate email. Please enter different email."})
return jsonify({"result": "Registered User Successfully"})
@app.route("/api/doLogin", methods=["POST"])
def doLoginApi():
# 'email' is name="email" of login.html in <input of email field
email = request.json["email"]
password = request.json["password"]
cursor = mysql.get_db().cursor()
resp = cursor.execute("""SELECT password FROM Users WHERE email=%s;""", (email,))
hashpassword = cursor.fetchone()[0]
# print(hashpassword)
checkHashPassword = check_password_hash(hashpassword, password)
# print(checkHashPassword)
if not checkHashPassword:
return jsonify({"result": "Email and password didn't match"})
# to give place to hit query of mysql->cursor
resp = cursor.execute(
"""SELECT id,email ,full_name FROM Users WHERE email=%s and password=%s;""",
(email, hashpassword),
)
# fetchall() is for select
user = cursor.fetchone()
cursor.close()
# print(user)
# if email/password entered and email/password in database match response=1
# token=""
if resp == 1:
session["email"] = email
session["userId"] = user[0]
logged_in_user = session.get("email")
token = str(uuid.uuid4())
# put that token to database
cursor = mysql.get_db().cursor()
resp = cursor.execute(
"""UPDATE Users set token=%s WHERE email=%s ;""", (token, email)
)
mysql.get_db().commit()
# fetchall() is for select
user = cursor.fetchone()
cursor.close()
return jsonify(
{"message": "Successful Login", "loggedIn": True, "token": token}
)
else:
return jsonify(
{
"result": "Login Failed ,Please check your username and password",
"loggedIn": False,
}
)
# REST APIS are defined from here
def _validate_token(token):
cursor = mysql.get_db().cursor()
cursor.execute("""SELECT id FROM Users WHERE token=%s;""", (token,))
# fetchall() is for select
user = cursor.fetchone()
cursor.close()
userId = 0
if user is not None:
userId = user[0]
return userId
@app.route("/rest/treks")
def allAPITreks():
cursor = mysql.get_db().cursor()
cursor.execute(
"""SELECT td.id as 'SNO', td.title as 'Title',
td.days AS 'Days' , td.difficulty as 'Difficulty'
,td.total_cost as 'Total cost', td.upvotes as 'Upvotes',
u.full_name as 'Full name' FROM `Trek_Destinations`
as td JOIN Users as u ON td.user_id=u.id;"""
)
treks = cursor.fetchall()
cursor.close()
logged_in_user = None
if session.get("email"):
logged_in_user = session["email"]
result = {"treks": treks, "logged_in_user": logged_in_user}
return jsonify(result)
@app.route("/rest/treks", methods=["POST"])
def doAddTrekApi():
logged_in_user = None
if session.get("email"):
logged_in_user = session["email"]
title = request.json["title"]
days = request.json["days"]
difficulty = request.json["difficulty"]
total_cost = request.json["total_cost"]
token = request.json["token"] or None
userId = _validate_token(token)
if userId == 0:
return jsonify({"message": "Please enter a valid token"})
upvotes = 0
cursor = mysql.get_db().cursor()
cursor.execute(
"""INSERT INTO `Trek_Destinations` VALUES (NULL, %s, %s, %s, %s, %s,%s);""",
(
title,
days,
difficulty,
total_cost,
upvotes,
userId,
),
)
mysql.get_db().commit()
cursor.close()
return jsonify({"message": "Trek has been added Successfully"})
@app.route("/rest/treks", methods=["PUT"])
def doUpdateTrekApi():
title = request.json["title"]
days = request.json["days"]
difficulty = request.json["difficulty"]
total_cost = request.json["total_cost"]
trekId = request.json["trekId"]
token = request.json["token"] or None
userId = _validate_token(token)
if userId == 0:
return jsonify({"message": "Please enter a valid token"})
cursor = (
mysql.get_db().cursor()
) # execute statements to communicate with the MySQL database.
resp = cursor.execute(
"""UPDATE `Trek_Destinations` SET `title`=%s,`days`=%s,`difficulty`=%s,`total_cost`=%s WHERE`id`=%s and `user_id`=%s ;""",
(title, days, difficulty, total_cost, trekId, userId),
)
if resp == 0:
return jsonify({"message": "you cannot update someone else's trek"})
mysql.get_db().commit() # changes in database
cursor.close()
return jsonify({"message": "Trek has been updated Successfully"})
# to delete the trek
@app.route("/rest/treks", methods=["DELETE"])
def doDeleteTrekApi():
trekId = request.json["trekId"]
token = request.json["token"] or None
userId = _validate_token(token)
if userId == 0:
return jsonify({"message": "Please enter a valid token"})
cursor = mysql.get_db().cursor()
resp = cursor.execute(
"""DELETE from `Trek_Destinations` WHERE `id`=%s and `user_id`=%s ;""",
(trekId, userId),
)
if resp == 0:
return jsonify({"message": "you cannot delete someone else's trek"})
mysql.get_db().commit()
cursor.close()
return jsonify({"message": "Trek has been deleted Successfully"})
# method for search=trek
# @app.route('/api/treks/search/<string:keyword>',)
# def search(keyword):
# another method for ?keyword=trek
@app.route("/api/search/treks")
def searchApi():
keyword = request.args.get("keyword")
cursor = mysql.get_db().cursor()
searchString = "%" + keyword + "%"
# print(searchString)
cursor.execute(
"""SELECT * FROM `Trek_Destinations`
WHERE `title` Like %s;""",
(searchString,),
)
treks = cursor.fetchall()
cursor.close()
logged_in_user = None
if session.get("email"):
logged_in_user = session["email"]
result = {"treks": treks, "logged_in_user": logged_in_user}
return jsonify(result)
app.run(debug=True)