/
flask_os_data.py
131 lines (105 loc) · 4.59 KB
/
flask_os_data.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
import sys
import math
import datetime
from bson import ObjectId
from flask import Flask, render_template,request,jsonify
from parse_fields_values import parse_fields_values
#import ../config.py
sys.path.append('..')
import config
app = Flask(__name__)
app.debug=True
def request_wants_json():
best = request.accept_mimetypes.best_match(['application/json', 'text/html'])
return best == 'application/json' and \
request.accept_mimetypes[best] > \
request.accept_mimetypes['text/html']
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@app.route('/1/<string:region>/<string:fields_values>',methods=['GET'])
def query_region(region,fields_values):
request_dict = parse_fields_values(fields_values)
#search data for request dictionary
results = [x for x in config.sandy_collection.find(request_dict).limit(50000)]
#convert date and object ID to string
for r in results:
for x,y in r.items():
if type(y) in (datetime.datetime,ObjectId):
r[x] = str(y)
if request_wants_json():
return jsonify(items=results)
return jsonify(items=results)
#return render_template('nonexistent.html', items=items)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@app.route('/2/',methods=['GET'])
@app.route('/2/<string:fields_values>',methods=['GET'])
def query_structure(fields_values = False):
if fields_values:
request_dict = parse_fields_values(fields_values)
#search data for request dictionary
results = [x for x in config.sandy_collection.find(request_dict).limit(50000)]
else:
results = [x for x in config.sandy_collection.limit(50000)]
outbound = []
#convert date and object ID to string
for r in results:
for x,y in r.items():
if type(y) in (datetime.datetime,ObjectId):
r[x] = str(y)
structure = {
'id':r.pop('gid',None),
'timestamp':r.pop('timestamp',None),
'date':r.pop('date',None),
'contact':{
'first-name':r.pop('first-name',None),
'last-name':r.pop('last-name',None),
'phone':r.pop('phone',None),
'email':r.pop('email',None),
'contact-lang':r.pop('contact-lang',None),
'contact-origin':r.pop('contact-origin',None)
},
'sandy':{
'resident-during-sandy':r.pop('resident-during-sandy',None)
},
'location':{
'street':r.pop('street',None),
'city':r.pop('city',None),
'state':r.pop('state',None),
'zip':r.pop('zip',None)
},
'home':{
'occupant-count':r.pop('occupant-count',None),
'rent-or-own-1':r.pop('rent-or-own-1',None),
'rent-or-own-2':r.pop('rent-or-own-2',None),
'resident':r.pop('resident',None),
'residence-other':r.pop('residence-other',None),
'have-children':r.pop('have-children',None),
'have-seniors':r.pop('have-seniors',None),
'have-disabled':r.pop('have-disabled',None),
'damage':{
'house-has-damage':r.pop('house-has-damage',None),
'need-help-repair':r.pop('need-help-repair',None),
'house-has-mold':r.pop('house-has-mold',None)
},
'notes':{
'note-contact':r.pop('note-contact',None),
'note-info':r.pop('note-info',None),
'note-fema-sba':r.pop('note-fema-sba',None),
'note-insurance':r.pop('note-insurance',None),
'note-housing':r.pop('note-housing',None),
'note-other':r.pop('note-other',None)
}
}
}
structure['other'] = {}
for x,y in r.items():
structure['other'][x] = y
# Remove None values
structure.update((k, v) for k, v in structure.iteritems() if v is not None)
outbound.append(structure)
if request_wants_json():
return jsonify(items=outbound)
return jsonify(count=str(len(results)),items=outbound)
#return render_template('nonexistent.html', items=items)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if __name__ == '__main__':
app.run(config.query_f_server,config.query_f_port)