-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisc_rest_api.py
207 lines (170 loc) · 7.04 KB
/
disc_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
from django.http import HttpResponse
import json
from .models.sqlite_mgr import get_domains
from .models.sqlite_mgr import get_baseline_scores
from .models.sqlite_mgr import get_domain_scores_national
from .models.sqlite_mgr import get_domain_scores_state
from .models.hwbi_calc import HWBICalc
from .models.meta_info import MetaInfo
from .models.meta_info import MetaBase
from .models.sqlite_mgr import get_state_details
from .models.sqlite_mgr import get_county_indicator_data
from django.conf import settings
from django.template import Context
from django.template.loader import get_template
from django.template.loader import render_to_string
from xhtml2pdf import pisa
import os
def get_disc_scores(request):
"""
HWBI DISC score request api endpoint
:param request: POST request dictionary object containing keys ['state', 'county']
:return: baseline and domain scores for the selected county.
"""
if request.method == 'GET':
if 'state' not in request.GET or 'county' not in request.GET or 'state_abbr' not in request.GET:
return HttpResponse(status=400)
else:
state = request.GET['state']
state_abbr = request.GET['state_abbr']
county = request.GET['county']
elif request.method == 'POST':
request_data = json.loads(request.body, encoding='utf-8')
state = request_data['state']
state_abbr = request.GET['state_abbr']
county = request_data['county']
else:
return HttpResponse(status=404)
if state == "" or county == "":
return HttpResponse(status=400)
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)
state_domains = get_domain_scores_state(state_abbr)
for s_domain in state_domains:
for domain in outputs.domains:
if domain.domainID == s_domain.domainID:
domain.stateScore = s_domain.score
break
nation_domains = get_domain_scores_national()
for n_domain in nation_domains:
for domain in outputs.domains:
if domain.domainID == n_domain.domainID:
domain.nationScore = n_domain.score
break
outputs.statehwbi = get_state_details(state).score
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
response = HttpResponse()
try:
result['outputs'] = outputs.get_dict()
rslt = json.dumps(result)
print(rslt)
response.content = rslt
except Exception as e:
s = str(e)
return response
def get_indicator_scores(request):
if request.method == 'GET':
if 'county' not in request.GET or 'state_abbr' not in request.GET:
return HttpResponse(status=400)
else:
state_abbr = request.GET['state_abbr']
county = request.GET['county']
elif request.method == 'POST':
request_data = json.loads(request.body, encoding='utf-8')
state_abbr = request.GET['state_abbr']
county = request_data['county']
else:
return HttpResponse(status=404)
if state_abbr == "" or county == "":
return HttpResponse(status=400)
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_abbr, 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
indicators = get_county_indicator_data(state_abbr=state_abbr, county=county)
response = HttpResponse()
try:
output = []
for indicator in indicators:
output.append(indicator.get_dict())
result['outputs'] = output
rslt = json.dumps(result)
print(rslt)
response.content = rslt
except Exception as e:
s = str(e)
return response
def generate_report(request):
"""
Creates a pdf report from the provided post arguments.
:param request:
:return:
"""
if request.method != "POST":
return HttpResponse(status=400)
# data = json.loads(request.body)
data = {}
html_template = 'disc/hwbi-disc-report-template.html'
report_name = 'disc-report.pdf'
response = HttpResponse(content_type="application/pdf")
response['Content-Disposition'] = 'attachment;filename="' + report_name + '"'
template = get_template(html_template)
html = template.render(data)
# html = render_to_string(html_template)
pisa.CreatePDF(html, dest=response, link_callback=link_callback)
return response
def link_callback(uri, rel):
"""
Convert HTML URIs to absolute system paths so xhtml2pdf can access those
resources
"""
sUrl = settings.STATIC_URL # Typically /static/
root = settings.PROJECT_ROOT # Typically /home/userX/project_static/
if uri.startswith(sUrl):
path = os.path.join(root, uri.replace(sUrl, ""))
else:
return uri
if not os.path.isfile(path):
path = os.getcwd() + uri
if not os.path.isfile(path):
raise Exception('media URI file not found. File path: ' + str(path))
return path