-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.py
73 lines (60 loc) · 2.09 KB
/
app.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
import os
import json
from flask import Flask, render_template, request, abort, Response
import db
import cities
from roster import Person, Facts
from utils import safe_float, safe_int
app = Flask(__name__)
@app.route('/cities.json')
def cities_json():
MIN_QUERY_LENGTH = 2
NUM_RESULTS = 5
query = request.args.get('q', '')
if len(query) < MIN_QUERY_LENGTH: abort(400)
return Response(json.dumps([
{'name': city.full_name,
'lat': city.latitude,
'long': city.longitude}
for city in cities.find(query)[:NUM_RESULTS]
]), mimetype='application/json')
@app.route('/')
def home():
latitude = safe_float(request.args.get('city_lat'))
longitude = safe_float(request.args.get('city_long'))
radius = safe_int(request.args.get('radius'))
closest = safe_int(request.args.get('closest'))
python = bool(request.args.get('python'))
r = bool(request.args.get('r'))
people = db.get_session().query(Person).join(Person.facts).\
join(Facts.airport).\
filter(Facts.active == True)
if python:
people = people.filter(Facts.python == True)
if r:
people = people.filter(Facts.r == True)
if latitude is not None and longitude is not None and radius:
people = (
person for person in people
if person.facts.airport.is_within_radius_of(
radius,
latitude,
longitude,
units='km'
)
)
elif latitude is not None and longitude is not None and closest:
# not searching radius but rather closest people
people = sorted(people,
key=lambda x: x.facts.airport.distance_from(latitude, longitude))[:closest]
return render_template('index.html', people=people)
def create_dbs():
cities.create_db()
if not os.path.exists(db.ROSTER_DB_PATH):
db.create_roster_db()
if __name__ == '__main__':
create_dbs()
from config import config
config_name = os.environ.get('FLASK_CONFIG', 'default')
app.config.from_object(config[config_name])
app.run()