-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhwbi_rest_api.py
executable file
·392 lines (293 loc) · 11.1 KB
/
hwbi_rest_api.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
from django.http import HttpRequest, HttpResponse
from django.views.decorators.csrf import csrf_exempt
import json
from .models.sqlite_mgr import get_services
from .models.sqlite_mgr import get_domains
from .models.sqlite_mgr import get_baseline_scores
from .models.meta_info import MetaInfo
from .models.meta_info import Link
from .models.meta_info import ComplexEncoder
from .models.meta_info import MetaInput
from .models.meta_info import MetaBase
from .models.scores import Scores
# from .models.domain_weights import DomainWeights
from .models.hwbi_calc import HWBICalc
version = 1.0
@csrf_exempt
def get_hwbi(request):
"""
HWBI Get hwbi
"""
print('Inside get_hwbi')
result = {}
mi = MetaInfo()
mi.description = 'The Human Well-Being Index (HWBI) was generated by researchers at the ' \
'U.S. Environmental Protection Agency using over one hundred ' \
'nationally-available data layers to assess economic, social, and ' \
'ecosystem services and calculate human well-being across the entire ' \
'U.S. at the regional, state, and county levels for years 2000-2010.'
mi.url.href = request.get_full_path()
result['metaInfo'] = mi.get_dict()
links = []
link1 = Link('calc')
links.append(link1.get_dict())
link2 = Link('locations')
links.append(link2.get_dict())
link3 = Link('html')
links.append(link3.get_dict())
result['links'] = links
response = HttpResponse()
rslt = json.dumps(result, cls=ComplexEncoder)
print(rslt)
response.content = rslt
return response
@csrf_exempt
def get_calc(request):
"""
HWBI Get calc
"""
print('Inside get_calc')
result = {}
mi = MetaInfo()
mi.description = "The Human Well-Being Index (HWBI) model calculator (calc) " \
"uses 22 economic, ecosystem, and social services values to " \
"calculate eight 'domains of well-being': Connection to Nature, " \
"Cultural Fulfillment, Education, Health, Leisure Time, Living Standards, " \
"Safety & Security, and Social Cohesion. These domains of well-being " \
"are then weighed based on user-supplied 'relative importance values' " \
"and are used to determine the overall HWBI score."
mi.url.href = request.get_full_path()
result['metaInfo'] = mi.get_dict()
links = []
link1 = Link('inputs','calc/inputs')
links.append(link1.get_dict())
link2 = Link('outputs', 'calc/outputs')
links.append(link2.get_dict())
link3 = Link('run', 'calc/run')
links.append(link3.get_dict())
link4 = Link('html', 'calc/hwbi')
links.append(link4.get_dict())
result['links'] = links
response = HttpResponse()
rslt = json.dumps(result, cls=ComplexEncoder)
print(rslt)
response.content = rslt
return response
@csrf_exempt
def get_calc_inputs(request):
"""
HWBI Get calc inputs
"""
print('Inside get_calc_inputs')
result = {}
mi = MetaInfo()
mi.description = "The Human Well-Being Index (HWBI) model calculator requires " \
"twenty-two economic; ecosystem; and social service values " \
"and a 'relative importance value' for each of the eight domains " \
"of well-being."
mi.url.href = request.get_full_path()
result['metaInfo'] = mi.get_dict()
result['metaInputs'] = get_services()
links = []
link1 = Link('inputs', 'calc/inputs')
links.append(link1.get_dict())
link2 = Link('outputs', 'calc/outputs')
links.append(link2.get_dict())
link3 = Link('run', 'calc/run')
links.append(link3.get_dict())
link4 = Link('html', 'calc/hwbi')
links.append(link4.get_dict())
result['links'] = links
response = HttpResponse()
rslt = json.dumps(result, cls=ComplexEncoder)
print(rslt)
response.content = rslt
return response
@csrf_exempt
def get_calc_outputs(request):
"""
HWBI Get calc outputs
"""
print('Inside get_calc_outputs')
result = {}
mi = MetaInfo()
mi.description = "The Human Well-Being Index (HWBI) model calculator provides " \
"eight 'domain of well-being' scores and an overall HWBI score."
mi.url.href = request.get_full_path()
result['metaInfo'] = mi.get_dict()
result['metaOutputs'] = get_domains()
response = HttpResponse()
rslt = json.dumps(result, cls=ComplexEncoder)
print(rslt)
response.content = rslt
return response
@csrf_exempt
def get_calc_run(request):
"""
HWBI Get calc run
"""
if request.method == 'GET':
return HttpResponse(status=404)
data = json.loads(request.body, encoding='utf-8')
dct_scores = data['scores']
dct_scores_lower = dict()
for k, v in dct_scores.items():
dct_scores_lower[k.lower()] = v
services = get_services()
for svc in services:
svc.score = dct_scores_lower[svc.name.lower()]
domain_weights = data['domainWeights']
dct_domain_weight_lower = dict()
for k, v in domain_weights.items():
dct_domain_weight_lower[k.lower()] = v
domains = get_domains()
for domain in domains:
domain.weight = dct_domain_weight_lower[domain.name.lower()]
inputs = list()
for svc in services:
inputs.append(svc.get_input_metadata())
for domain in domains:
inputs.append(domain.get_input_metadata())
calc = HWBICalc()
hwbi_out = calc.hwbi_run(services, domains)
result = dict()
mi = MetaInfo()
mi.description = "The Human Well-Being Index (HWBI) model calculator (calc) " \
"uses 22 economic, ecosystem, and social services values to " \
"calculate eight 'domains of well-being': Connection to Nature, " \
"Cultural Fulfillment, Education, Health, Leisure Time, Living " \
"Standards, Safety & Security, and Social Cohesion. These domains " \
"of well-being are then weighed based on user-supplied 'relative " \
"importance values' and are used to determine the overall HWBI score."
mi.url.href = request.get_full_path()
result['metaInfo'] = mi.get_dict()
result['inputs'] = inputs
result['outputs'] = hwbi_out.get_dict()
response = HttpResponse()
rslt = json.dumps(result, cls=ComplexEncoder)
print(rslt)
response.content = rslt
return response
@csrf_exempt
def get_locations(request):
"""
HWBI Get Location
"""
print('Inside get_locations')
result = dict()
mi = MetaInfo()
mi.description = "The Human Well-Being Index (HWBI) model locations endpoint " \
"uses state and county names to provide values for 22 economic, " \
"ecosystem, and social services, 8 domains of well-being, " \
"and a total HWBI score for the county."
mi.url.href = request.get_full_path()
result['metaInfo'] = mi.get_dict()
links = []
link1 = Link('inputs', 'locations/inputs')
links.append(link1.get_dict())
link2 = Link('outputs', 'locations/outputs')
links.append(link2.get_dict())
link3 = Link('run', 'locations/run')
links.append(link3.get_dict())
# link4 = Link('html', 'locations/hwbi')
# links.append(link4.get_dict())
result['links'] = links
response = HttpResponse()
rslt = json.dumps(result, cls=ComplexEncoder)
print(rslt)
response.content = rslt
return response
@csrf_exempt
def get_locations_inputs(request):
"""
HWBI Get Locations inputs
"""
print('Inside get_locations')
result = dict()
mi = MetaInfo()
mi.description = "The Human Well-Being Index (HWBI) model locations " \
"endpoint requires USA state and county names."
mi.url.href = request.get_full_path()
result['metaInfo'] = mi.get_dict()
inputs = list()
meta_state = MetaInput('State', 'United States of America state name', type='text', required=True)
meta_county = MetaInput('County', 'United States of America county name', type='text', required=True)
inputs.append(meta_state.get_dict())
inputs.append(meta_county.get_dict())
result['metaInputs'] = inputs
response = HttpResponse()
rslt = json.dumps(result, cls=ComplexEncoder)
print(rslt)
response.content = rslt
return response
@csrf_exempt
def get_locations_outputs(request):
"""
HWBI Get calc outputs
"""
print('Inside get_locations_outputs')
result = {}
mi = MetaInfo()
mi.description = "The Human Well-Being Index (HWBI) model locations endpoint " \
"uses state and county names to provide values for 22 economic, " \
"ecosystem, and social services, 8 domains of well-being, and a " \
"total HWBI score for the county."
mi.url.href = request.get_full_path()
result['metaInfo'] = mi.get_dict()
result['metaOutputs'] = get_domains()
response = HttpResponse()
rslt = json.dumps(result, cls=ComplexEncoder)
print(rslt)
response.content = rslt
return response
@csrf_exempt
def get_locations_run(request):
"""
HWBI Get locations run
"""
response = HttpResponse()
data = None
state = None
county = None
if request.method == 'GET':
return HttpResponse(status=404)
data = json.loads(request.body,encoding='utf-8')
state = data['state']
county = data['county']
services = list()
base_line_scores = get_baseline_scores(state, county)
for base_score in base_line_scores:
services.append(base_score.get_service_out())
domains = list()
db_domains = get_domains()
for domain in db_domains:
domains.append(domain.get_domain_out())
calc = HWBICalc()
outputs = calc.hwbi_run(services, domains)
result = dict()
mi = MetaInfo()
mi.description = "The Human Well-Being Index (HWBI) model calculator (calc) " \
"uses 22 economic, ecosystem, and social services values to " \
"calculate eight 'domains of well-being': Connection to Nature, " \
"Cultural Fulfillment, Education, Health, Leisure Time, Living " \
"Standards, Safety & Security, and Social Cohesion. These domains " \
"of well-being are then weighed based on user-supplied 'relative " \
"importance values' and are used to determine the overall HWBI score."
mi.url.href = request.get_full_path()
result['metaInfo'] = mi.get_dict()
# build inputs
inputs = list()
meta_state = MetaBase('state',value=state, description='US State')
meta_county = MetaBase('county', value=county, description='County')
inputs.append(meta_state.get_dict())
inputs.append(meta_county.get_dict())
result['inputs'] = inputs
try:
result['outputs'] = outputs.get_dict()
response = HttpResponse()
rslt = json.dumps(result)
print(rslt)
response.content = rslt
except Exception as e:
s = str(e)
return response