/
app.py
49 lines (40 loc) · 1.49 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
import os
from flask import Flask, request, Response, redirect, current_app
from pymongo import Connection
from functools import wraps
from urlparse import urlparse
import helpers
app = Flask(__name__)
MONGO_URL = os.environ['MONGOHQ_URL']
conn = Connection(MONGO_URL)
db = conn[urlparse(MONGO_URL).path[1:]]
@app.route("/states")
@helpers.jsonp
def getstates():
filter = request.args.get('require', '')
if (filter):
if filter not in ('strict', 'id', 'photo'):
message = ('%s is not a valid require value. Use either strict, '
'photo or id.' % filter)
return helpers.bad_request_response(message)
else:
states = db.states.find({'vote.' + filter: True}, { '_id': False })
return helpers.wrap_response(states)
else:
states = db.states.find({}, { '_id' : False })
return helpers.wrap_response(states)
@app.route("/states/<statename>")
@helpers.jsonp
def getstate(statename):
state_length = range(4, 15)
if len(statename) == 2:
state = db.states.find({'info.abbr': statename.upper()}, { '_id': False })
return helpers.wrap_response(state)
elif len(statename) not in state_length:
message = 'That is not a recognizable value for a state.'
return helpers.bad_request_response(message)
else:
state = db.states.find({'name': statename.title()}, { '_id': False })
return helpers.wrap_response(state)
if __name__ == "__main__":
app.run(debug=False)