-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.py
917 lines (604 loc) · 23.8 KB
/
server.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
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
from flask import (Flask, render_template, redirect, request, flash, session,
jsonify)
from jinja2 import StrictUndefined
from flask_debugtoolbar import DebugToolbarExtension
import os
from model import (User, Trail, User_Trail, Trip, Trip_User,
Trip_Trail, db, connect_to_db)
from helperfunctions import (call_geocoding_api, call_hiking_project_api,
call_hiking_project_api_trail_id,
seed_trails_into_db, delete_trip_users,
delete_trip_trails, delete_trip)
HIKING_PROJECT_KEY = os.environ['HIKING_PROJECT_KEY']
MAPS_JS_KEY = os.environ['MAPS_JS_KEY']
app = Flask(__name__)
@app.route("/")
def index():
"""Display homepage"""
return render_template("index.html")
# ~~~~~ ACCOUNT-RELATED ROUTES ~~~~~ #
@app.route("/register")
def reg_form():
"""Display registration form"""
return render_template("register.html")
@app.route("/register", methods=["POST"])
def register_user():
"""Creates new user if user does not yet exist"""
username = request.form.get("username")
email = request.form.get("email")
password = request.form.get("password")
# Validate if username or email already exists in
# the users table in database
if not User.query.filter((User.email == email) |
(User.username == username)).all():
user = User(username=username, email=email)
user.set_password(password)
db.session.add(user)
db.session.commit()
flash("User created!")
return redirect("/login")
# If one does exist, flash message to indicate if email or username
else:
if User.query.filter_by(email=email).all():
flash(f"There's already an account associated with {email}")
else:
flash(f"The username {username} is already taken")
return redirect("/register")
@app.route("/login")
def show_login_form():
"""Display login form"""
return render_template("login.html")
@app.route("/login", methods=["POST"])
def log_in_user():
"""Validate user login"""
username = request.form.get("username")
password = request.form.get("password")
# Validate that user's username exists in database
user = User.query.filter_by(username=username).first()
if user and user.check_password(password):
session["user_id"] = user.user_id
flash("Login successful")
return redirect("/")
else:
flash("Incorrect login information")
return redirect("/login")
# No direct hyperlink access here
@app.route("/account")
def show_account_options():
"""Display account options for logged in users"""
if "user_id" in session:
return render_template("account.html")
else:
flash("You need to be logged in to access that page")
return redirect("/login")
@app.route("/account/updateacctinfo")
def show_acct_info_form():
"""Display form for user to update account information"""
if "user_id" in session:
user = User.query.get(session.get("user_id"))
return render_template("/account-userinfo.html", user=user)
else:
flash("You need to be logged in to access that page")
return redirect("/login")
@app.route("/account/updateacctinfo", methods=["POST"])
def update_account_info():
"""Update a user's account information"""
if "user_id" in session:
fname = request.form.get("fname")
lname = request.form.get("lname")
cell = request.form.get("cell")
city = request.form.get("city")
state = request.form.get("state")
zipcode = request.form.get("zipcode")
user = User.query.get(session["user_id"])
# Form fields are not required - only update database if text was
# entered in that field and not the same as what's already in the db
if len(fname) > 0 and fname != user.fname:
user.fname = fname
if len(lname) > 0 and lname != user.lname:
user.lname = lname
if len(cell) > 0 and cell != user.cell:
user.cell = cell
if len(city) > 0 and city != user.city:
user.city = city
if len(state) > 0 and state != user.state:
user.state = state
if len(zipcode) > 0 and zipcode != user.zipcode:
user.zipcode = zipcode
db.session.add(user)
db.session.commit()
flash("Information updated!")
return redirect("/account/updateacctinfo")
else:
flash("You need to be logged in to access that page")
return redirect("/login")
@app.route("/account/changepassword")
def show_change_pass_form():
"""Show form for users to change their password"""
if "user_id" in session:
return render_template("account-changepass.html")
else:
flash("You need to be logged in to access that page")
return redirect("/login")
@app.route("/account/changepassword", methods=["POST"])
def change_user_password():
"""Change a user's password"""
if "user_id" in session:
old_pass = request.form.get("oldpass")
new_pass = request.form.get("newpass")
user = User.query.get(session["user_id"])
if user.check_password(old_pass):
user.set_password(new_pass)
db.session.add(user)
db.session.commit()
flash("Password successfully updated")
return redirect("/account/changepassword")
else:
flash("Incorrect password, please try again")
return redirect("/account/changepassword")
else:
flash("You need to be logged in to access that page")
return redirect("/login")
@app.route("/account/savedtrails")
def display_saved_trails():
"""Display a user's saved trails"""
# Query database to find User_trail objects belonging to user
# & not marked complete
if "user_id" in session:
user_id = session.get("user_id")
saved_trails = User_Trail.query.filter((User_Trail.user_id == user_id)
& (User_Trail.is_completed.is_(False))).all()
return render_template("account-savedlist.html",
saved_trails=saved_trails)
else:
flash("You need to be logged in to access that page")
return redirect("/login")
@app.route("/account/completedtrails")
def display_completed_trails():
"""Display a user's completed trails"""
# Query database to find User_trail objects belonging to user
# & not marked complete
if "user_id" in session:
user_id = session.get("user_id")
completed_trails = User_Trail.query.filter((User_Trail.user_id == user_id)
& (User_Trail.is_completed.is_(True))).all()
return render_template("account-completedlist.html",
completed_trails=completed_trails)
else:
flash("You need to be logged in to access that page")
return redirect("/login")
@app.route("/account/trips")
def display_user_trips():
"""Display a user's trips"""
if "user_id" in session:
user_id = session.get("user_id")
user_trips = Trip_User.query.filter_by(user_id=user_id).all()
active_trips = []
archived_trips = []
for ut in user_trips:
if ut.trip.is_archived is True:
archived_trips.append(ut)
else:
active_trips.append(ut)
return render_template("account-trips.html", active_trips=active_trips,
archived_trips=archived_trips)
else:
flash("You need to be logged in to access that page")
return redirect("/login")
@app.route("/logout")
def log_out_user():
"""Log out user"""
if "user_id" in session:
del session["user_id"]
flash("Logged out")
return redirect("/")
# ~~~~~ SEARCH-RELATED ROUTES ~~~~~ #
@app.route("/search")
def display_search_results():
"""Display search results"""
return render_template("search.html", MAPS_JS_KEY=MAPS_JS_KEY)
# ~~~~~ TRAIL-RELATED ROUTES ~~~~~ #
@app.route("/trail/<int:trail_id>")
def display_trail_info(trail_id):
"""Display trail information page"""
trail = Trail.query.get(trail_id)
user_id = session.get("user_id")
trips = []
if user_id:
trips = Trip_User.query.filter_by(user_id=user_id).all()
if session.get("second_most_recent"):
session["third_most_recent"] = session.get("second_most_recent")
if session.get("most_recent_trail"):
session["second_most_recent"] = session.get("most_recent_trail")
session["most_recent_trail"] = str(trail_id)
return render_template("trail.html", trail=trail,
MAPS_JS_KEY=MAPS_JS_KEY, trips=trips)
# ~~~~~ TRIP-RELATED ROUTES ~~~~~ #
@app.route("/createnewtrip", methods=["GET"])
def show_new_trip_form():
"""Show form for user to create a new Trip instance"""
if "user_id" in session:
return render_template("createnewtrip.html")
else:
flash("You need to be logged in to access that page")
return redirect("/login")
@app.route("/createnewtrip", methods=["POST"])
def create_new_trip():
"""Create a new Trip instance"""
if "user_id" in session:
trip_name = request.form.get("trip-name")
accommodations = request.form.get("accommodations")
creator_id = session.get("user_id")
# Create new trip instance
new_trip = Trip(trip_name=trip_name, creator_id=creator_id,
trip_accommodations=accommodations)
# If accommodations field was filled in, find the lat/long
# and add the values to new_trip
if accommodations:
lat_long = call_geocoding_api(accommodations)
if lat_long != "Invalid search terms":
accomm_long = lat_long["lng"]
accomm_lat = lat_long["lat"]
new_trip.accom_lat = accomm_lat
new_trip.accom_long = accomm_long
db.session.add(new_trip)
db.session.commit()
# Add Trip_User instance for creator of the trip upon creation of the trip
user_id = session.get("user_id")
new_tu = Trip_User(trip_id=new_trip.trip_id, user_id=user_id)
db.session.add(new_tu)
db.session.commit()
return redirect(f"/trip/{new_trip.trip_id}")
else:
flash("You need to be logged in to access that page")
return redirect("/login")
@app.route("/trip/<int:trip_id>")
def show_trip(trip_id):
"""Display Trip instance information"""
trip = Trip.query.get(trip_id)
all_users = User.query.all()
return render_template("trip.html", trip=trip, all_users=all_users,
MAPS_JS_KEY=MAPS_JS_KEY)
@app.route("/trail/<trail_id>/addtotrip/<trip_id>")
def add_trail_to_trip(trail_id, trip_id):
"""Adds a Trail_Trip instance"""
if "user_id" in session:
tt_query = Trip_Trail.query.filter((Trip_Trail.trail_id == trail_id) &
(Trip_Trail.trip_id == trip_id)).first()
if not tt_query:
user_id = session.get("user_id")
tt = Trip_Trail(trail_id=trail_id, trip_id=trip_id,
added_by=user_id)
db.session.add(tt)
db.session.commit()
flash("Trail added to trip!")
else:
flash("Trail added to trip!")
return redirect(f"/trail/{trail_id}")
else:
flash("You need to be logged in to access that page")
return redirect("/login")
# ~~~~~ AJAX REQUEST/JSON ROUTES ~~~~~ #
@app.route("/user/loggedin")
def is_user_logged_in():
"""Check if user is logged in"""
if "user_id" in session:
return "true"
else:
return "false"
@app.route("/json/search-coords")
def get_search_coordinates():
"""Call Google Maps Geocoding API with search terms & return json
of coordinates"""
search_terms = request.args.get("search")
lat_long = call_geocoding_api(search_terms)
if lat_long != "Invalid search terms":
return jsonify(lat_long)
else:
return "Invalid search terms"
@app.route("/json/search")
def return_json_search_results():
"""Search for trails given a location, seed trails into database, and
return json response"""
search_terms = request.args.get("search")
lat_long = call_geocoding_api(search_terms)
if lat_long != "Invalid search terms":
response = call_hiking_project_api(lat_long)
seed_trails_into_db(response)
json_response = jsonify(response["trails"])
return json_response
else:
return "Invalid search terms"
@app.route("/json/latlongbyid/<trail_id>")
def get_lat_long_by_trail_id(trail_id):
"""Return json lat/long coordinates of a trail given its trail id"""
trail = Trail.query.get(trail_id)
lat = trail.lat
long = trail.long
lat_long = {
"lat": lat,
"lng": long
}
return jsonify(lat_long)
@app.route("/user/save-trail", methods=["POST"])
def save_trail_to_user_list():
"""Instantiate a User-Trail instance"""
if "user_id" in session:
trail_id = request.form.get("trail_id")
saved_trail = User_Trail(user_id=session["user_id"], trail_id=trail_id)
db.session.add(saved_trail)
db.session.commit()
return "Trail added"
else:
return "You must sign in to save trails"
@app.route("/user/unsave-trail", methods=["POST"])
def unsave_trail_to_user_list():
"""Remove a User-Trail instance"""
trail_id = request.form.get("trail_id")
if "user_id" in session:
user_id = session.get("user_id")
trail_to_delete = User_Trail.query.filter((User_Trail.user_id == user_id)
& (User_Trail.trail_id == trail_id)).first()
db.session.delete(trail_to_delete)
db.session.commit()
return "Trail removed"
else:
return "You must sign in to edit saved trails"
@app.route("/user/complete-trail", methods=["POST"])
def mark_saved_trail_as_complete():
"""Update a User-Trail's is_completed attribute to True"""
if "user_id" in session:
trail_id = int(request.form.get("trail_id"))
user_id = session["user_id"]
saved_trail = User_Trail.query.filter((User_Trail.user_id == user_id)
& (User_Trail.trail_id == trail_id)).first()
if saved_trail:
saved_trail.is_completed = True
db.session.add(saved_trail)
db.session.commit()
else:
saved_trail = User_Trail(user_id=user_id, trail_id=trail_id,
is_completed=True)
db.session.add(saved_trail)
db.session.commit()
return "Trail marked as complete"
else:
return "You must be signed in to save trails"
@app.route("/user/uncomplete-trail", methods=["POST"])
def unmark_saved_trail_as_complete():
"""Update a User-Trail's is_completed attribute to False"""
if "user_id" in session:
trail_id = int(request.form.get("trail_id"))
saved_trail = User_Trail.query.filter((User_Trail.user_id == session["user_id"])
& (User_Trail.trail_id == trail_id)).first()
saved_trail.is_completed = False
db.session.add(saved_trail)
db.session.commit()
return "Trail unmarked as complete"
@app.route("/user/is-trail-saved/<trail_id>")
def check_if_trail_saved_for_user(trail_id):
"""For a given user, check if a trail is saved in user_trails"""
user_id = session.get("user_id")
if user_id:
ut = User_Trail.query.filter((User_Trail.user_id == user_id) &
(User_Trail.trail_id == trail_id)).first()
response = {}
if ut:
response["saved"] = True
if ut.is_completed:
response["completed"] = True
else:
response["completed"] = False
else:
response["saved"] = False
response["completed"] = False
return jsonify(response)
@app.route("/json/getallusertrips")
def get_users_trips():
"""Gets all trips associated with a given user"""
user_id = session.get("user_id")
all_tu = Trip_User.query.filter_by(user_id=user_id).all()
if all_tu:
tu_dict = {}
for tu in all_tu:
if tu.trip.is_archived is False:
tu_dict[tu.trip_id] = {
"trip_name": tu.trip.trip_name,
"trip_lat": tu.trip.accom_lat,
"trip_lng": tu.trip.accom_long,
"trip_id": tu.trip_id
}
return jsonify(tu_dict)
else:
return "none"
@app.route("/json/gettriptrailinfo")
def get_trip_trail_info():
"""Gets trip_trail information associated with a given trip"""
trip_id = request.args.get("trip_id")
all_tt = Trip_Trail.query.filter_by(trip_id=trip_id).all()
if not all_tt:
return "No tt here"
tt_dict = {}
for tt in all_tt:
tt_dict[tt.trail_id] = {
"trail_name": tt.trail.trail_name,
"trail_lat": tt.trail.lat,
"trail_lng": tt.trail.long,
"trail_id": tt.trail_id,
"trip_id": tt.trip_id,
"trip_name": tt.trip.trip_name,
}
return jsonify(tt_dict)
@app.route("/json/tripinfo")
def get_trip_info():
"""Return JSON of trip coordinates & accom address"""
trip_id = request.args.get("trip_id")
trip = Trip.query.get(trip_id)
trip_trails = []
for trip_trail in trip.trip_trails:
trip_trails.append({
"trail_id": trip_trail.trail_id,
"trail_name": trip_trail.trail.trail_name,
"trail_lat_long": {
"lat": trip_trail.trail.lat,
"lng": trip_trail.trail.long,
}
})
lat_long = {
"lat": trip.accom_lat,
"lng": trip.accom_long}
response = {
"lat_long": lat_long,
"accom_text": trip.trip_accommodations,
"trip_trails": trip_trails,
}
return jsonify(response)
@app.route("/deletetrip", methods=["POST"])
def delete_a_trip():
"""Deletes trip from database"""
if "user_id" in session:
trip_id = request.form.get("trip_id")
delete_trip_users(trip_id)
delete_trip_trails(trip_id)
flash_msg = delete_trip(trip_id)
flash(flash_msg)
return redirect("/account/trips")
else:
return "You need to be logged in to access that page"
@app.route("/istriparchived")
def check_if_trip_archived():
"""Checks whether a trip's is_archived attribute is True or False"""
trip_id = request.args.get("trip_id")
trip = Trip.query.get(trip_id)
if trip.is_archived:
return "true"
else:
return "false"
@app.route("/archivetrip", methods=["POST"])
def archive_a_trip():
"""Archive's a trip in database"""
if "user_id" in session:
trip_id = request.form.get("trip_id")
trip = Trip.query.get(trip_id)
trip.is_archived = True
db.session.add(trip)
db.session.commit()
return "Successfully archived"
else:
return "You need to be logged in to access that page"
@app.route("/unarchivetrip", methods=["POST"])
def unarchive_a_trip():
"""Unarchive's a trip in database"""
if "user_id" in session:
trip_id = request.form.get("trip_id")
trip = Trip.query.get(trip_id)
trip.is_archived = False
db.session.add(trip)
db.session.commit()
return "Successfully unarchived"
else:
return "You need to be logged in to access that page"
@app.route("/updatetripname", methods=["POST"])
def update_trip_name():
"""Update the name for a given trip"""
if "user_id" in session:
trip_id = request.form.get("trip_id")
trip_name = request.form.get("trip_name")
trip = Trip.query.get(trip_id)
if trip:
trip.trip_name = trip_name
db.session.add(trip)
db.session.commit()
return trip_name
else:
return "An error has occurred"
else:
return "You need to be logged in to access that page"
@app.route("/updatetripaccoms", methods=["POST"])
def update_trip_accommodations():
"""Update the accommodations & associated lat/long for a given trip"""
if "user_id" in session:
trip_id = request.form.get("trip_id")
trip_accom = request.form.get("trip_accom")
trip = Trip.query.get(trip_id)
trip.trip_accommodations = trip_accom
lat_long = call_geocoding_api(trip_accom)
if lat_long != "Invalid search terms":
trip.accom_long = lat_long["lng"]
trip.accom_lat = lat_long["lat"]
db.session.add(trip)
db.session.commit()
return f"{trip_accom}"
else:
return "Address could not be read"
else:
return "You need to be logged in to access that page"
@app.route("/addtripusers", methods=["POST"])
def add_trip_users():
"""Add Trip_User instances"""
if "user_id" in session:
trip_id = int(request.form.get("trip_id"))
user_id = int(request.form.get("user_id"))
tu_query = Trip_User.query.filter((Trip_User.user_id == user_id) &
(Trip_User.trip_id == trip_id)).first()
if tu_query:
return "User already added to trip"
else:
tu = Trip_User(trip_id=trip_id, user_id=user_id)
db.session.add(tu)
db.session.commit()
username = tu.user.username
return username
else:
return "You need to be logged in to access that page"
@app.route("/removetripusers", methods=["POST"])
def remove_trip_users():
"""Remove Trip_User instances"""
if "user_id" in session:
trip_id = int(request.form.get("trip_id"))
user_id = int(request.form.get("user_id"))
tu = Trip_User.query.filter((Trip_User.user_id == user_id) &
(Trip_User.trip_id == trip_id)).first()
if tu:
username = tu.user.username
db.session.delete(tu)
db.session.commit()
return username
else:
return "An error has occurred"
else:
return "You need to be logged in to access that page"
@app.route("/removetriptrails", methods=["POST"])
def remove_trip_trail():
"""Removes a Trail_Trip instance"""
if "user_id" in session:
trip_id = request.form.get("trip_id")
trail_id = request.form.get("trail_id")
tt_query = Trip_Trail.query.filter((Trip_Trail.trail_id == trail_id) &
(Trip_Trail.trip_id == trip_id)).first()
if tt_query:
db.session.delete(tt_query)
db.session.commit()
return str(trail_id)
else:
return "An error has occurred"
else:
return "You need to be logged in to access that page"
@app.route("/gettrailbyid", methods=["GET"])
def call_hiking_project_api_single_trail():
trail_id = request.args.get("trail_id")
response = call_hiking_project_api_trail_id(trail_id)
return jsonify(response)
connect_to_db(app)
if __name__ == "__main__":
# We have to set debug=True here, since it has to be True at the
# point that we invoke the DebugToolbarExtension
app.debug = True
app.secret_key = "super secret"
# For getting error messages in Jinja when variables are undefined
app.jinja_env.undefined = StrictUndefined
# make sure templates, etc. are not cached in debug mode
app.jinja_env.auto_reload = app.debug
# Use the DebugToolbar
# DebugToolbarExtension(app)
app.run(port=5000, host='0.0.0.0')