-
Notifications
You must be signed in to change notification settings - Fork 5
/
denver_streets.py
45 lines (37 loc) · 1.51 KB
/
denver_streets.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
from flask import Flask, Response, url_for, request, json
from jinja2 import *
import os
import urllib2
import datetime
import database
import models
from models import *
def shutdown_session(exception=None):
session.remove()
app = Flask(__name__)
env = Environment(loader=PackageLoader('denver_streets', 'templates'))
@app.route('/')
def index():
items = ['closings']
response = { 'items' : [] }
[ response['items'].append({item+"_href": url_for(item)}) for item in items]
response_data = json.dumps(response)
response = Response(response_data, status=200, mimetype='application/json', headers={'Access-Control-Allow-Origin':'*'})
return response
@app.route('/closings')
def closings():
if request.args.get('on_date'):
date_param = datetime.datetime.strptime(request.args.get('on_date'), "%Y-%m-%d")
else:
date_param = datetime.datetime.now()
closings = database.session.query(Closing).filter(Closing.start_date <= date_param, Closing.end_date >= date_param).all()
closings_array = []
for closing in closings:
closing_dict = closing.to_dict()
closing_dict['geometry'] = json.loads(database.session.scalar(closing.geom.ST_AsGeoJSON()))
closings_array.append(closing_dict)
response = Response(json.dumps({'items': closings_array}), status=200, mimetype='application/json', headers={'Access-Control-Allow-Origin':'*'})
return response
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)