forked from DanielleYasso/where-the-truck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routing.py
990 lines (717 loc) · 23.9 KB
/
routing.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
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
from flask import render_template, redirect, request
from flask import session, g, make_response, flash, url_for
from flask_mail import Mail, Message
from flask.ext.cors import CORS, cross_origin
import requests
import os
import json
from passlib.hash import pbkdf2_sha256
from datetime import datetime
import rauth
import twilio.twiml
from flaskapp import app
import model
from forms import PasswordForm, PasswordFormSettings, ForgotPassword
from flask.ext.login import LoginManager, login_required
from flask.ext.login import login_user, logout_user, current_user
from itsdangerous import URLSafeTimedSerializer
# Create database connection
DATABASE_URL = os.environ.get("DATABASE_URL", "postgresql:///danielleyasso")
app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URL
# Cross-origin resource sharing
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
# update app config with mail data for Flask-Mail
app.config.update(dict(
# Email settings
MAIL_SERVER = 'smtp.gmail.com',
MAIL_PORT = 465,
MAIL_USE_SSL = True,
MAIL_USERNAME= 'wherethetruck@gmail.com',
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
))
# secret key for session
SECRET_KEY = os.environ.get('FLASK_SECRET_KEY', 'forking_placeholder')
app.secret_key = SECRET_KEY
# update Flask-Mail instance with new app config settings
mail = Mail(app)
# Create serializer for generating tokens for user email confirmation
ts = URLSafeTimedSerializer(app.config['SECRET_KEY'])
# google maps api key
API_KEY = os.environ.get('GOOGLE_API_KEY')
# Set up Flask-Login LoginManager
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.session_protection = "strong"
#######################################
# FLASK-LOGIN USER LOADER FOR SESSION #
#######################################
@login_manager.user_loader
def load_user(user_id):
""" For a given logged in user id, returns the user object """
return model.User.query.get(user_id)
##################
# BEFORE REQUEST #
##################
@app.before_request
def check_login():
""" Assigns g.user the current user object """
g.user = current_user
##################
# HOMEPAGE ROUTE #
##################
@app.route("/")
def home():
""" Loads the homepage, sending it all the food trucks in the attractions
table """
attractions = model.db.session.query(model.Attraction).all()
return render_template("index.html", API_KEY=API_KEY, attractions=attractions)
###############
# USER LOGOUT #
###############
@app.route("/logout")
def logout():
""" Logs out the currently logged in user """
logout_user()
return redirect("/")
##############
# USER LOGIN #
##############
@app.route("/login", methods=["POST"])
def login():
""" Gets user login data from the login form, and logs the user in """
# get email and password from form inputs
email = request.form.get("email")
password = request.form.get("password")
remember_me = request.form.get("rememberMe")
# get user with that email address
user = model.db.session.query(model.User).filter_by(email=email).first()
# check if user exists in database
if not user:
return convert_to_JSON("incorrect")
# check if password is right
if not pbkdf2_sha256.verify(password, user.password):
return convert_to_JSON("incorrect")
model.db.session.add(user)
model.db.session.commit()
login_user(user,remember=remember_me)
return ""
####################
# RECOVER PASSWORD #
####################
@app.route("/forgot_password", methods=["GET", "POST"])
def forgot_password():
""" Renders the forgot_password.html webpage. Gets user email from forgot
password page, and creates a unique token for that user, sending it to them
via email as part of a unique link """
#get form data
form = ForgotPassword()
if form.validate_on_submit():
user_email = form.email.data
# check if user email exists
user = model.db.session.query(model.User)\
.filter_by(email=user_email).first()
if not user:
flash("No user found with that email address.")
return redirect("/forgot_password")
# Create reset password email
subject = "Password reset requested"
token = ts.dumps(user.email, salt="recover-key")
recover_url = url_for(
"reset_with_token",
token=token,
_external=True)
html = render_template(
"emails/recover_password.html",
recover_url=recover_url)
# Create email message to send
msg = Message(subject,
sender="dbyasso@gmail.com",
recipients=[user.email])
msg.html = html
mail.send(msg)
flash("Password reset instructions sent to your email address.")
return render_template("forgot_password.html", form=form)
##################
# RESET PASSWORD #
##################
@app.route("/reset/<token>", methods=["GET", "POST"])
def reset_with_token(token):
""" Resets a user's password, verifying that their token is correct, and
then encrypting their new password and logging them in. """
try:
email = ts.loads(token, salt="recover-key", max_age=86400)
except:
abort(404)
#get form data
form = PasswordForm()
if form.validate_on_submit():
user = model.User.query.filter_by(email=email).first_or_404()
password = form.password.data
# securely store password
password_hash = pbkdf2_sha256.encrypt(
password,
rounds=200000,
salt_size=16)
user.password = password_hash
model.db.session.add(user)
model.db.session.commit()
# login user
login_user(user)
return redirect("/")
else:
return render_template("/reset_with_token.html", form=form, token=token)
#################
# USER SETTINGS #
#################
@app.route("/user_settings", methods=["GET", "POST"])
@login_required
def user_settings():
""" Renders user settings page, and allows user to reset their password, and
encrypts their new password. """
#get form data
form = PasswordFormSettings()
if form.validate_on_submit():
# get password from form
current_password = form.current_password.data
# check if password was entered correctly
# prevents someone from changing a logged in users password
if not pbkdf2_sha256.verify(current_password, g.user.password):
flash("Incorrect current password.")
return render_template("/user_settings.html", form=form)
new_password = form.new_password.data
# securely store new password
password_hash = pbkdf2_sha256.encrypt(
new_password,
rounds=200000,
salt_size=16)
g.user.password = password_hash
model.db.session.commit()
return redirect("/")
else:
return render_template("/user_settings.html", form=form)
###############
# USER SIGNUP #
###############
@app.route("/signup", methods=["POST"])
def signup():
""" Signs up a new user, making sure their email is unique.
Sends a confirmation email to the user with a unique token/url """
# get user info
username = request.form.get("username")
email = request.form.get("email")
password = request.form.get("password")
remember_me = request.form.get("rememberMe")
# check that user email doesn't already exist in database
user = model.db.session.query(model.User).filter_by(email=email).first()
if user:
return convert_to_JSON("userExists")
# securely store password
password_hash = pbkdf2_sha256.encrypt(password, rounds=200000, salt_size=16)
# add new user to database
new_user = model.User(
username=username,
email=email,
password=password_hash)
model.db.session.add(new_user)
model.db.session.commit()
# add default empty user preferences
new_user.preferences = {}
model.db.session.commit()
# email confirmation
if "@" in email:
subject = "Confirm your email"
token = ts.dumps(new_user.email, salt="email-confirm-key")
confirm_url = url_for(
"confirm_email",
token=token,
_external=True)
html = render_template(
"emails/activate.html",
confirm_url=confirm_url)
msg = Message(subject,
sender="dbyasso@gmail.com",
recipients=[new_user.email])
msg.html = html
mail.send(msg)
# add user to session --> LOGIN USER
login_user(new_user,remember=remember_me)
return ""
########################
# SIGNUP CONFIRM EMAIL #
########################
@app.route("/confirm/<token>")
def confirm_email(token):
""" Confirms a user's email based on their token, from their unique link """
try:
email = ts.loads(token, salt="email-confirm-key", max_age=86400)
except:
abort(404)
user = model.User.query.filter_by(email=email).first_or_404()
user.confirmed_at = datetime.now()
model.db.session.add(user)
model.db.session.commit()
return redirect("/")
#################################
# SAVE USER DISPLAY PREFERENCES #
#################################
@app.route("/save_preferences", methods=["POST"])
def save_preferences():
""" Saves a user's display preferences in a dictionary, based on form data,
and stores it in the user.preferences Pickletype """
checked_attractions = request.form.getlist("attractionsDisplayed")
show_old = request.form.get("showOldCheckins", False)
show_bad = request.form.get("showBadRatings", False)
show_untrusted = request.form.get("showUntrusted", False)
show_non_users = request.form.get("showNonUserCheckins", False)
# add checked attractions to user preferences
p = {}
for attraction in checked_attractions:
p[int(attraction)] = True
all_attractions = model.db.session.query(model.Attraction).all()
for attraction in all_attractions:
if not attraction.id in p:
p[attraction.id] = False
p["show_old"] = show_old
p["show_bad"] = show_bad
p["show_untrusted"] = show_untrusted
p["show_non_users"] = show_non_users
if g.user.is_authenticated():
g.user.preferences = p
model.db.session.commit()
return redirect("/")
#################################
# GET MARKERS TO DISPLAY ON MAP #
#################################
@app.route("/get_markers")
def get_markers():
"""Get's all of the attractions with checkins to be displayed as markers"""
# get all attractions that have checkins
attractions = model.db.session.query(model.Attraction).all()
if not attractions:
return convert_to_JSON("noMarkers")
attraction_list = []
for attraction in attractions:
if attraction.checkin_id:
checkin = model.db.session.query(model.Checkin)\
.get(attraction.checkin_id)
checkin_data = getCheckinData(checkin)
attraction_list.append(checkin_data)
if attraction_list == []:
return convert_to_JSON("noMarkers")
return convert_to_JSON(attraction_list)
###################
# GET MARKER DATA #
###################
def getLastGood(attraction):
""" Gets the last good checkin (good = made by a user, not badly rated) and
returns all associated data in a dictionary """
if attraction.last_good_checkin_id:
last_good = model.db.session.query(model.Checkin)\
.get(attraction.last_good_checkin_id)
else:
return False
if not last_good:
return False
timeout = getTimeout(last_good)
lgDict = {
"lat": last_good.lat,
"lng": last_good.lng,
"timestamp": dump_datetime(last_good.timestamp),
"timeout": timeout,
"checkin_id": last_good.id,
"bad_rating": False,
"non_user_checkin": False,
"trusted_user": True
}
return lgDict
# return False
def getTimeout(this):
""" Gets the timeout for a given checkin object, by comparing that
checkin's timeestamp with datetime.now() """
# check how old timestamp is
time_diff = datetime.now() - this.timestamp
# older than a day?
if time_diff.days >= 1:
timeout = "old"
# older than 3 hours?
elif time_diff.seconds >= 10800:
timeout = "three_hours"
# older than 2 hours?
elif time_diff.seconds >= 7200:
timeout = "two_hours"
# older than 1 hour?
elif time_diff.seconds >= 3600:
timeout = "one_hour"
# # TEMP FOR DEMO PURPOSES ONLY
# elif time_diff.seconds >= 30:
# timeout = "three_hours"
else:
timeout = False
return timeout
def getCheckinData(checkin):
""" Gets the data for a specific checkin and returns it as a dictionary """
timeout = getTimeout(checkin)
# get yelp ratings if they exist
ratings_img = None
ratings_count = None
url = None
if checkin.attraction.biz_id:
ratings_data = get_yelp_ratings(checkin.attraction.biz_id)
if ratings_data:
ratings_img = str(ratings_data["rating_img_url_small"])
ratings_count = str(ratings_data["review_count"])
url = "http://www.yelp.com/biz/{0}".format(checkin.attraction.biz_id)
last_good_checkin = False
# Check if attraction checkin has a really bad rating
bad_rating = False
# COMPARISON VALUES TO CHANGE --> set low for demo purposes
if checkin.downvotes > checkin.upvotes:
bad_rating = True
last_good_checkin = getLastGood(checkin.attraction)
non_user_checkin = True
trusted_user = True
# made by logged in and trusted user?
if checkin.user_id != None:
non_user_checkin = False
# trusted user?
trusted_user = checkin.user.is_trusted()
current_checkin = {
"lat": checkin.lat,
"lng": checkin.lng,
"timestamp": dump_datetime(checkin.timestamp),
"timeout": timeout,
"checkin_id": checkin.id,
"bad_rating": bad_rating,
"non_user_checkin": non_user_checkin,
"trusted_user": trusted_user
}
checkin_data = {
"id": checkin.attraction.id,
"name": checkin.attraction.name,
"type": checkin.attraction.att_type,
"ratings_img": ratings_img,
"ratings_count": ratings_count,
"url": url,
"current": current_checkin,
"previous": last_good_checkin
}
return checkin_data
########################
# CREATE A NEW CHECKIN #
########################
@app.route("/checkin", methods=["POST"])
def checkin():
"""Checkin user's current location as location of selected item"""
# get attraction id
attraction_id = request.form.get("attraction_id")
# get geolocation data from user
lat = request.form.get("latitude")
lng = request.form.get("longitude")
print "Attraction %r, lat %r, lng %r" % (attraction_id, lat, lng)
# Add checkin to database checkins table
if g.user.is_authenticated():
new_checkin = model.Checkin(
attraction_id=attraction_id,
lat=lat,
lng=lng,
user_id=g.user.id)
else:
new_checkin = model.Checkin(
attraction_id=attraction_id,
lat=lat,
lng=lng)
model.db.session.add(new_checkin)
model.db.session.commit()
# Use checkin record to update attraction's checkin_id
attraction = new_checkin.attraction
print attraction.id
print attraction.name
print attraction.checkin_id
# UPDATE LAST_GOOD_CHECKIN_ID
if attraction.checkin_id:
previous_checkin = model.db.session.query(model.Checkin)\
.get(attraction.checkin_id)
# made by a user? a trusted user?
if previous_checkin.user_id and previous_checkin.user.is_trusted():
# did it have a rating? that wasn't bad?
if previous_checkin.calculated_rating != None and \
previous_checkin.calculated_rating > 0.1:
attraction.last_good_checkin_id = attraction.checkin_id
# unrated, but made by a trusted user, so add it
else:
attraction.last_good_checkin_id = attraction.checkin_id
# update attraction's checkin_id with the new_checkin
attraction.checkin_id = new_checkin.id
# Get marker details
checkin_data = getCheckinData(new_checkin)
model.db.session.commit()
return convert_to_JSON(checkin_data)
################
# UPDATE VOTES #
################
############
# DOWNVOTE #
############
@app.route("/downvote/<int:checkin_id>", methods=["POST"])
def downvote(checkin_id):
"""Gets a user's down vote and updates checkins table record"""
vote = "down"
return update_vote(checkin_id, vote)
##########
# UPVOTE #
##########
@app.route("/upvote/<int:checkin_id>", methods=["POST"])
def upvote(checkin_id):
"""Gets a user's up vote and updates checkins table record"""
vote = "up"
return update_vote(checkin_id, vote)
###############
# UPDATE VOTE #
###############
def update_vote(checkin_id, vote):
"""Gets a user's up or down vote and updates checkins table record"""
# get attraction's checkin
checkin = model.db.session.query(model.Checkin).get(checkin_id)
# ONLY LOGGED IN USERS CAN VOTE --> taken care of in map.js
# update user vote status so popovers don't show anymore
if not g.user.has_voted:
g.user.has_voted = True
# IF NO RATINGS YET
if checkin.users_who_rated == None or checkin.users_who_rated == {}:
checkin.users_who_rated = {}
# add user to dictionary
checkin.users_who_rated[g.user.id] = vote
model.db.session.commit()
# increment vote
add_votes(checkin, vote)
# IF USER IS ALREADY IN THE CHECKINS DATABASE
elif g.user.id in checkin.users_who_rated:
# WITH NO RATING, aka 0 (due to deleted rating)
if checkin.users_who_rated[g.user.id] == 0:
# add user vote to dictionary
checkin.users_who_rated[g.user.id] = vote
model.db.session.commit()
#increment vote
add_votes(checkin, vote)
# else user HAS rated this
else:
# get existing user vote
existing_vote = checkin.users_who_rated[g.user.id]
# USER IS UNDOING THEIR VOTE
if existing_vote == vote:
# delete rating
checkin.users_who_rated[g.user.id] = 0
model.db.session.commit()
# decrement vote count
remove_votes(checkin, vote)
# RATINGS EXIST, BUT NOT BY THIS USER
else:
# add user to dictionary
checkin.users_who_rated[g.user.id] = vote
model.db.session.commit()
# increment vote
add_votes(checkin, vote)
# update calculated rating if checkin isn't older than 1 hour, so that
# checkin's user's average rating isn't hurt by downvotes long after checkin
timeout = getTimeout(checkin)
if not timeout:
checkin.calculate_rating()
model.db.session.commit()
# update checkin user's rating based on new calculated rating
if checkin.user:
checkin.user.set_average_rating()
model.db.session.commit()
return redirect("/")
################
# REMOVE VOTES #
################
def remove_votes(checkin, vote):
""" Decrements either the upvote count or the downvote count in the
checkins table for a given checkin object """
# update database
if vote == "up":
# remove upvote
checkin.upvotes -= 1
elif vote == "down":
# remove downvote
checkin.downvotes -= 1
# commit changes to database
model.db.session.commit()
#############
# ADD VOTES #
#############
def add_votes(checkin, vote):
""" Increments either the upvote count or the downvote count in the
checkins table for a given checkin object """
# update checkin votes
if vote == "up":
checkin.upvotes += 1
elif vote == "down":
checkin.downvotes += 1
model.db.session.commit()
############################
# GET VOTES FOR INFOWINDOW #
############################
@app.route("/get_votes/<int:checkin_id>")
def get_votes(checkin_id):
"""Gets all of the votes for a given checkin"""
# get checkin from db
checkin = model.db.session.query(model.Checkin).get(checkin_id)
votes_data = [checkin.upvotes, checkin.downvotes] # [0, 1]
# is a user signed in?
if g.user.is_authenticated():
votes_data.append(True) # [2], user is signed in
if checkin.user_id == g.user.id:
votes_data.append(True) # [3], this is the user's own checkin
else:
votes_data.append(False) # [3], not this user's checkin
# get user vote (if it exists)
if checkin.users_who_rated != None:
if g.user.id in checkin.users_who_rated:
# vote_type is "up" or "down" or 0
vote_type = checkin.users_who_rated[g.user.id]
votes_data.append(vote_type) # [4]
else:
votes_data.append(False) # [4] placeholder vote_type
else:
votes_data.append(False) # [4], placeholder for vote_type
# has user ever voted?
votes_data.append(g.user.has_voted) # [5], has user ever voted?
return convert_to_JSON(votes_data)
#############
# API CALLS #
#############
##########################
# YELP API FUNCTION CALL #
##########################
def get_yelp_ratings(business_id):
""" Returns yelp ratings and related yelp information for a food truck
with a given business id """
# OAuth credentials
CONSUMER_KEY = os.environ.get('YELP_CONSUMER_KEY')
CONSUMER_SECRET = os.environ.get('YELP_CONSUMER_SECRET')
TOKEN = os.environ.get('YELP_TOKEN')
TOKEN_SECRET = os.environ.get('YELP_TOKEN_SECRET')
session = rauth.OAuth1Session(
consumer_key = CONSUMER_KEY,
consumer_secret = CONSUMER_SECRET,
access_token = TOKEN,
access_token_secret = TOKEN_SECRET)
url = "http://api.yelp.com/v2/business/{0}".format(business_id)
print "**** url", url
try:
request = session.get(url)
# transform json api response into dictionary
data = request.json()
except ValueError:
return False
session.close()
return data
###################
# TWILIO API CALL #
###################
@app.route("/twilio", methods=["GET", "POST"])
def twilio_response():
""" Twilio API call. Sends text message either the link to the application.
If the user texted the name of an attraction, also includes a google map
link with the lat and lng of that attraction's current checkin """
# # Respond to an incoming text message with a static response
resp = twilio.twiml.Response()
# URL TO CHANGE UPON DEPLOYMENT AND BASED ON DIFFERENT NGROK TUNNELS
message = "Find it in San Francisco at https://4d3d4b3a.ngrok.com/"
new_message = ""
# get user input
user_input = request.values.get("Body")
attractions = model.db.session.query(model.Attraction).all()
for attraction in attractions:
# did user request a known attraction?
if str(user_input).lower() == str(attraction.name).lower():
# check if that attraction has a checkin
if attraction.checkin_id:
# get the checkin
checkin = model.db.session.query(model.Checkin)\
.get(attraction.checkin_id)
# check how old timestamp is
time_diff = datetime.now() - checkin.timestamp
# older than a day?
if time_diff.days >= 1:
time_ago = "more than 1 day ago"
# older than 6 hours?
elif time_diff.seconds >= 21600:
time_ago = "more than 6 hours ago"
# older than 3 hours?
elif time_diff.seconds >= 10800:
time_ago = "more than 3 hours ago"
# older than 2 hours?
elif time_diff.seconds >= 7200:
time_ago = "more than 2 hours ago"
# older than 1 hour?
elif time_diff.seconds >= 3600:
time_ago = "more than 1 hour ago"
elif time_diff.seconds >= 1800:
time_ago = "more than 30 minutes ago"
else:
time_ago = "less than 30 minutes ago"
new_message = "{0} was last checked in {1}"\
.format(attraction.name,time_ago)
new_message += " at http://maps.google.com/maps/place/{0},{1}"\
.format(checkin.lat,checkin.lng)
new_message += "\n\n"
response_message = new_message + message
resp.message(response_message)
return str(resp)
##################################################
# CORS (CROSS-ORIGIN RESOURCE SHARING) API CALLS #
##################################################
#####################
# GEONAMES API CALL #
#####################
@app.route("/api/geonames")
def api_geonames():
""" For a specific latitude and longitude, calls the geonames ocean api
and returns the name of the ocean that coordinate is in, if applicable. """
lat = request.args.get("lat")
lng = request.args.get("lng")
username = "username=dbyasso"
url = "http://api.geonames.org/oceanJSON?lat={0}&lng={1}&{2}".format(
lat,lng, username)
r = requests.get(url)
response = json.loads(r.text)
return convert_to_JSON(response)
######## END API CALLS ########
##################
###### JSON ######
##################
###################
# CONVERT TO JSON #
###################
def convert_to_JSON(result):
"""Convert result object to a JSON web request."""
response = make_response(json.dumps(result))
response.mimetype = "application/json"
return response
##########################
# DUMP DATETIME FOR JSON #
##########################
def dump_datetime(value):
"""Deserialize datetime object into string form for JSON processing.
For example, converting a date and a time:
>>> import datetime
>>> dump_datetime(datetime.datetime(1970, 12, 25, 15, 0, 0))
['1970-12-25', '15:00:00']
But also should work if given just a date:
>>> import datetime
>>> dump_datetime(datetime.datetime(1970, 12, 25))
['1970-12-25', '00:00:00']
"""
if value is None:
return None
return [value.strftime("%Y-%m-%d"), value.strftime("%H:%M:%S")]
###### END JSON ######
if __name__=="__main__":
import doctest
# heroku
PORT = int(os.environ.get("PORT", 5000))
DEBUG = "NO_DEBUG" not in os.environ
if doctest.testmod().failed == 0:
app.run(debug=DEBUG, host="0.0.0.0", port=PORT)