Advertisement
Guest User

application.py

a guest
Jan 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.86 KB | None | 0 0
  1. import os
  2. import re
  3. from flask import Flask, jsonify, render_template, request
  4.  
  5. from cs50 import SQL
  6. from helpers import lookup
  7.  
  8. # Configure application
  9. app = Flask(__name__)
  10.  
  11. # Configure CS50 Library to use SQLite database
  12. db = SQL("sqlite:///mashup.db")
  13.  
  14.  
  15. # Ensure responses aren't cached
  16. @app.after_request
  17. def after_request(response):
  18.     response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
  19.     response.headers["Expires"] = 0
  20.     response.headers["Pragma"] = "no-cache"
  21.     return response
  22.  
  23.  
  24. @app.route("/")
  25. def index():
  26.     """Render map"""
  27.     if not os.environ.get("API_KEY"):
  28.         raise RuntimeError("API_KEY not set")
  29.     return render_template("index.html", key=os.environ.get("API_KEY"))
  30.  
  31.  
  32. @app.route("/articles")
  33. def articles():
  34.     """Look up articles for geo"""
  35.  
  36.     # TODO
  37.     return jsonify([])
  38.  
  39.  
  40. @app.route("/search")
  41. def search():
  42.     """Search for places that match query"""
  43.  
  44.     # TODO
  45.     return jsonify([])
  46.  
  47.  
  48. @app.route("/update")
  49. def update():
  50.     """Find up to 10 places within view"""
  51.  
  52.     # Ensure parameters are present
  53.     if not request.args.get("sw"):
  54.         raise RuntimeError("missing sw")
  55.     if not request.args.get("ne"):
  56.         raise RuntimeError("missing ne")
  57.  
  58.     # Ensure parameters are in lat,lng format
  59.     if not re.search("^-?\d+(?:\.\d+)?,-?\d+(?:\.\d+)?$", request.args.get("sw")):
  60.         raise RuntimeError("invalid sw")
  61.     if not re.search("^-?\d+(?:\.\d+)?,-?\d+(?:\.\d+)?$", request.args.get("ne")):
  62.         raise RuntimeError("invalid ne")
  63.  
  64.     # Explode southwest corner into two variables
  65.     sw_lat, sw_lng = map(float, request.args.get("sw").split(","))
  66.  
  67.     # Explode northeast corner into two variables
  68.     ne_lat, ne_lng = map(float, request.args.get("ne").split(","))
  69.  
  70.     # Find 10 cities within view, pseudorandomly chosen if more within view
  71.     if sw_lng <= ne_lng:
  72.  
  73.         # Doesn't cross the antimeridian
  74.         rows = db.execute("""SELECT * FROM places
  75.                          WHERE :sw_lat <= latitude AND latitude <= :ne_lat AND (:sw_lng <= longitude AND longitude <= :ne_lng)
  76.                          GROUP BY country_code, place_name, admin_code1
  77.                          ORDER BY RANDOM()
  78.                          LIMIT 10""",
  79.                           sw_lat=sw_lat, ne_lat=ne_lat, sw_lng=sw_lng, ne_lng=ne_lng)
  80.  
  81.     else:
  82.  
  83.         # Crosses the antimeridian
  84.         rows = db.execute("""SELECT * FROM places
  85.                          WHERE :sw_lat <= latitude AND latitude <= :ne_lat AND (:sw_lng <= longitude OR longitude <= :ne_lng)
  86.                          GROUP BY country_code, place_name, admin_code1
  87.                          ORDER BY RANDOM()
  88.                          LIMIT 10""",
  89.                           sw_lat=sw_lat, ne_lat=ne_lat, sw_lng=sw_lng, ne_lng=ne_lng)
  90.  
  91.     # Output places as JSON
  92.     return jsonify(rows)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement