/
server-h.py
539 lines (413 loc) · 18 KB
/
server-h.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
"""Paws Finder. Uses Flask, Jinja, AJAX and JSON"""
# Heroku Version
# Dec. 3, added custom attribute to capture email in new relic
# Dec. 15 removed Twilio info
from jinja2 import StrictUndefined
from flask import Flask, render_template, request, Response, flash, redirect, session, jsonify
from flask_debugtoolbar import DebugToolbarExtension
from model import connect_to_db, db, User, Animal, Shelter, UserAnimal, UserSearch
import os, sys #use for os environ
import petfinder #petfinder API
from sqlalchemy import exc # this handles Integrity Errors
import json
import newrelic.agent
#add password hash using sha256_crypt
from passlib.hash import sha256_crypt
# Google Maps api key
maps_api_key = os.environ["GOOGLEMAPS_API_KEY"]
# Petfinder API credentials
api_key = os.environ["PETFINDER_API_KEY"]
api_secret = os.environ["PETFINDER_API_SECRET"]
# Instantiate petfinder api with my credentials
api = petfinder.PetFinderClient(api_key=api_key,
api_secret=api_secret)
app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Required to use Flask sessions and the debug toolbar
app.secret_key = "LGkjsdFlfkjaBldsmDasVfd36p9!9u0m43qlnXalrCd1f43aB"
# Normally, if you use an undefined variable in Jinja2, it fails silently.
# This is horrible. Fix this so that, instead, it raises an error.
app.jinja_env.undefined = StrictUndefined
@app.route("/")
def index():
"""Homepage."""
animals = ["dog", "cat"]
ages = ["Baby", "Young", "Adult", "Senior"]
sizes = {'S': 'small', 'M': 'medium', 'L': 'large', 'XL': 'xlarge'}
genders = {"F": "female", "M": "male"}
dog_breeds = {"dogs": ["None", "Australian Cattledog", "Australian Shepherd",
"Basset Hound", "Beagle", "Border Collie", "Boston Terrier",
"Boxer", "Bulldog", "Catahoula Leopard Dog", "Cattle Dog",
"Chihuahua", "Cocker Spaniel", "Dachshund", "Dalmatian", "English Bulldog",
"French Bulldog", "German Shepherd", "Golden Retriever", "Great Dane", "Great Pyrenees", "Greyhound",
"Labrador Retriever", "Mixed Breed",
"Pit Bull Terrier", "Pug", "Staffordshire Terrier", "Yorkshire Terrier"]}
cat_breeds = {"cats": ["None", "American Shorthair", "Calico", "Domestic Long Hair",
"Domestic Medium Hair", "Domestic Short Hair", "Siamese", "Tabby",
"Tabby-Brown", "Tabby-Gray","Tabby-Orange", "Tortoiseshell", "Tuxedo"]}
return render_template("home.html",
animals=animals, ages=ages,
sizes=sizes, genders=genders,
dog_breeds=dog_breeds,
cat_breeds=cat_breeds)
@app.route("/process-registration", methods=['POST'])
def register_process():
"""Process registration."""
# Get form variables from reg form
first_name = request.form.get("firstname")
last_name = request.form.get("lastname")
email = request.form.get("email")
password = request.form.get("password")
phone = request.form.get("phone")
#hash the password and store this in DB
password_hash = sha256_crypt.encrypt(password)
new_user = User(first_name=first_name, last_name=last_name,
email=email, password=password_hash,
phone=phone)
# handles registration duplicate, flashes message
try:
db.session.add(new_user)
db.session.commit()
flash("Welcome %s %s! \
Your username is %s."
% (first_name, last_name, email))
except exc.IntegrityError:
flash("User already exists. Please login")
db.session().rollback()
return redirect("/")
@app.route("/login.json", methods=["POST"])
def perform_login():
"""Checks credentials, processes log in"""
# Get credentials from form
email = request.form.get("email")
# print "Capturing email for new relic ", email
newrelic.agent.add_custom_parameter("email", email)
password = request.form.get("password")
user = User.query.filter_by(email=email).first()
#output of password_to_verify is True/False. Check user input vs. database
password_to_verify = sha256_crypt.verify(password, user.password)
if not user:
results = {"success": False,
"message": "No user exists, please register."}
return jsonify(results)
if not password_to_verify:
results = {"success": False,
"message": "Invalid username/password, try again."}
return jsonify(results)
results = {"success": True,
"firstname": user.first_name}
session["user_id"] = user.user_id
return jsonify(results)
@app.route("/logout.json", methods=['POST'])
def logout_json():
"""Log out."""
del session["user_id"]
results = {"success": True,
"message": "Logged out."}
return jsonify(results)
@app.route("/search", methods=["GET"])
def process_search():
"""Process form variables from quick search fields. No account needed."""
genders = {"F": "female", "M": "male"}
# Get form variables
zipcode = request.args.get("zipcode")
city = request.args.get("city")
state = request.args.get("state")
animal = request.args.get("animal")
gender = request.args.get("gender")
# assign location to either zipcode or city, state
if not zipcode:
location = city + " " + state
elif not city or state:
location = zipcode
# Call api and process search, note variables are singular.
pets = api.pet_find(location=location,
animal=animal,
gender=gender,
output="basic",
count=50)
# import pdb; pdb.set_trace()
pet_list = []
# loop through a range from the api call
# append to pet_list
for i in range(20):
pet = pets.next()
pet_list.append(pet)
return render_template("results.html",
location=location,
animal=animal,
genders=genders, #dictionary containing gender info
gender=gender, # user form input
pets=pet_list)
@app.route("/search-complete", methods=["GET"])
def process_complete_search():
"""Process form variables from complete search fields. User is logged in."""
sizes = {'S': 'small', 'M': 'medium', 'L': 'large', 'XL': 'extra large'}
genders = {"F": "female", "M": "male"}
# Get form variables
zipcode = request.args.get("zipcode")
city = request.args.get("city")
state = request.args.get("state")
animal = request.args.get("animal")
age = request.args.get("age")
size = request.args.get("size")
gender = request.args.get("gender")
if animal == "dog":
breed = request.args.get("dog-breeds")
else:
breed = request.args.get("cat-breeds")
search_info = {"zipcode":zipcode,
"animal": animal,
"age": age,
"size": size,
"gender": gender,
"breed": breed}
session["last_search"] = search_info
# assign location to either zipcode or city, state
if not zipcode:
location = city + " " + state
elif not city or not state:
location = zipcode
else:
location = zipcode
liked = db.session.query(UserAnimal.animal_id).filter(UserAnimal.user_id == session['user_id']).subquery()
liked_petid = db.session.query(Animal.pet_id).filter(Animal.animal_id.in_(liked)).all()
liked_petid = [str(liked_pet[0]) for liked_pet in liked_petid]
pets = api.pet_find(location=location,
animal=animal,
age=age,
size=size,
gender=gender,
breed=breed,
output="basic",
count=50)
# import pdb; pdb.set_trace()
pet_list = []
# loop through a range from the api call
# append to pet_list
for i in range(30):
try:
pet = pets.next()
pet_list.append(pet)
except:
break
# print pet_list[2]
return render_template("results_complete.html",
location=location,
animal=animal,
age=age,
sizes=sizes, # dictionary that contain sizes data
size=size, # user form input
genders=genders, # dictionary that contain gender data
gender=gender, # user form input
breed=breed,
pets=pet_list,
liked_petid=liked_petid,
search_info=search_info)
@app.route("/save-search.json", methods=["POST"])
def save_search_results():
"""Save search criteria."""
#get credentials from form
title = request.form.get("title")
description = request.form.get("description")
current_search = UserSearch.query.filter(UserSearch.user_id == session["user_id"],
UserSearch.zipcode == session['last_search']["zipcode"],
UserSearch.animal == session['last_search']["animal"],
UserSearch.age == session['last_search']["age"],
UserSearch.size == session['last_search']["size"],
UserSearch.gender == session['last_search']["gender"],
UserSearch.breed == session['last_search']["breed"]).first()
saved_search = UserSearch(user_id=session["user_id"],
zipcode=session['last_search']['zipcode'],
animal=session['last_search']['animal'],
age=session['last_search']['age'],
size=session['last_search']['size'],
gender=session['last_search']['gender'],
breed=session['last_search']['breed'],
title=title,
description=description)
if current_search == saved_search:
results = {"success": False,
"message": "You already saved this search."}
# so if current search == saved search then hide box
# for 87 call back in savedpaws, inspect results, if results == success
# then search already exists, hide box
db.session.add(saved_search)
db.session.commit()
results = {"success": True,
"message": "Your search is saved!"}
return jsonify(results)
@app.route("/get-saved-searches.json", methods=["GET"])
def get_saved_searches():
"""Retrieve saved searches from dB."""
loggedin_user = session.get("user_id")#session is a dict,
#so .get() enables value of user_id to be returned
if loggedin_user:
searches = UserSearch.query.filter(UserSearch.user_id == loggedin_user).all()
else:
flash("Log in to see your saved searches! Do it NOW!")
return redirect("/")
saved_searches = []
for search in searches:
# another way to do this, but longer
# search_dict = {}
# search_dict["title"] = search.title
# search_dict["description"] = search.description
# search_dict["usersearch_id"] = search.user_search_id
saved_searches.append(search.to_dict())
results = {"results": saved_searches}
return jsonify(results)
@app.route("/shelters-search", methods=["GET"])
def process_search_shelters():
"""Display shelter search form"""
maps_api_key = os.environ["GOOGLEMAPS_API_KEY"]
# Get form variables
location = request.args.get("zipcode")
# Call api and process search, note variables are singular.
shelters = api.shelter_find(location=location,
count=50)
shelter_list = []
# loop through a range from the api call
# append to shelter_list
for i in range(30):
shelter = shelters.next()
shelter_list.append(shelter)
# print shelter_list[2]
return render_template("shelters_search.html",
location=location,
shelters=shelter_list,
key=maps_api_key)
@app.route("/shelter-pets", methods=["GET"])
def show_shelter_pets():
"""Display pets in a shelter"""
# Get shelterID from googlemap infowindow
shelter_id = request.args.get("id")
# print shelter_id
# Get shelter name from googlemap infowindow
# Display name in shelter_pets.html
shelter_name = request.args.get("name")
# print shelter_name
# Call api and process search, note variables are singular.
shelter_pets = api.shelter_getpets(id=shelter_id,
output="full",
count=30)
shelterpet_list = []
# loop through a range from the api call
# append to list
for i in range(10):
shelter_pet = shelter_pets.next()
shelterpet_list.append(shelter_pet)
return render_template("shelter_pets.html",
shelterpets=shelterpet_list,
shelter_name=shelter_name)
@app.route("/like-pets.json", methods=["POST"])
def like_pets():
"""Save liked pets from search results."""
#get credentials from clicking the like button
shelter_id = request.form.get("shelterId")
animal = request.form.get("animal")
name = request.form.get("name")
breed = request.form.get("breeds")
age = request.form.get("age")
gender = request.form.get("gender")
pet_id = request.form.get("petID")
size = request.form.get("size")
description = request.form.get("description")
last_update = request.form.get("lastupdate")
liked_pet = Animal(shelter_id=shelter_id,
animal=animal,
name=name,
breed=breed,
age=age,
gender=gender,
pet_id=pet_id,
size=size,
description=description,
last_update=last_update)
db.session.add(liked_pet)
db.session.commit()
# instantiate useranimal object so users can find liked animals through usersanimals table
new_user_animal = UserAnimal(user_id=session.get("user_id"), animal=liked_pet)
db.session.add(new_user_animal)
db.session.commit()
results = {"success": True,
"message": "Liked!", "pet_id":pet_id}
return jsonify(results)
@app.route("/get-liked-pets.json", methods=["GET"])
def get_liked_pets():
"""Retrieve liked pets from dB."""
loggedin_user = session.get("user_id")#session is a dict,
#so .get() enables value of user_id to be returned
if loggedin_user:
user = User.query.filter(User.user_id == loggedin_user).first()
saved_pets = user.liked_animals
else:
flash("Log in to see your saved paws! Do it NOW!")
return redirect("/")
liked_pets = []
for pet in saved_pets:
# another way to do this, but longer
# search_dict = {}
# search_dict["title"] = search.title
# search_dict["description"] = search.description
# search_dict["usersearch_id"] = search.user_search_id
liked_pets.append(pet.to_dict())
results = {"results": liked_pets}
return jsonify(results)
@app.route("/send-alert", methods=["GET"])
def send_alert():
"""Shelter sends SMS updates about saved pets"""
# TO DO: Insert user phone in the 'to' area
#Create alert message
message = client.messages.create(
to = os.environ["MY_PHONE"],
from_ = os.environ["TWILIO_PHONE"],
body = "There are updates to your saved pets! \
Type 'Yes' if interested.",
media_url = ["http://bit.ly/2tlWPcH"])
return Response("Shelter alert sent!", mimetype="text/plain"), 200
@app.route("/sms", methods=["GET", "POST"])
def respond_to_shelter():
"""User response to alert from shelter"""
# Based on https://www.twilio.com/docs/guides/how-to-receive-and-reply-in-python#custom-responses-to-incoming-sms-messages
# Capture user response
user_text = request.values.get('Body', None)
# Start TWIML response
response = MessagingResponse()
# Based on users response, message is returned
if user_text == "Yes":
response.message("Contact us for an appointment.")
elif user_text == "No":
response.message("Have a great day!")
else:
response.message("Check PAWS Finder for updates.")
return Response(str(response), mimetype="text/xml")
@app.route("/receive-text", methods=["POST"])
def receive_sms():
"""User sends text message to shelter about interest in pet"""
# Refer to: https://www.twilio.com/blog/2016/09/how-to-receive-and-respond-to-a-text-message-with-python-flask-and-twilio.html
# Get data from form
number = request.form['From']
message_body = request.form['Body']
# Acknowledge reciept of sms
response = MessagingResponse()
response.message('Hello {}, thank you for your interest! We got your message, "{}". We will get back to you ASAP. ;-)'
.format(number, message_body))
return str(response)
############### End of routes
if __name__ == "__main__":
# We have to set debug=True here, since it has to be True at the point
# that we invoke the DebugToolbarExtension
# Do not debug for demo
app.debug = False
import logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.DEBUG)
app.jinja_env.auto_reload = app.debug
connect_to_db(app, os.environ.get("DATABASE_URL"))
# connect_to_db(app)
# Use the DebugToolbar
DebugToolbarExtension(app)
DEBUG = "NO_DEBUG" not in os.environ
PORT = int(os.environ.get("PORT", 5000))
app.run(host="0.0.0.0", port=PORT, debug=DEBUG)