-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
executable file
·206 lines (179 loc) · 7.31 KB
/
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
from bottle import run,route,request,abort,app
import datetime
import get_data
import api_key
import helper_functions
base_url="/api"
@route(base_url+"/locations/hiv")
def locations_hiv():
return {"locations":get_data.locations(database="openmrs_aggregation")}
@route(base_url+"/locations/mch")
def locations_hiv():
return {"locations":get_data.locations(database="mch_aggregation")}
@route(base_url+'/hiv/<req>')
def hiv(req):
return get_data.dashboard(req)
@route(base_url+'/mch/<req>')
def mch(req):
return get_data.mch_dashboard(req)
@route(base_url+'/hiv/total_patients')
def hiv_total_patients():
return {"total_patients":get_data.total_patients(database="openmrs_aggregation")}
@route(base_url+'/mch/total_patients')
def mch_total_patients():
return {"total_patients":get_data.total_patients(database="mch_aggregation")}
@route(base_url+'/performance/hiv')
def performace_hiv():
if api_key.check_key(request.query.api_key,"hiv","performace"):
api_key.accessed(request.query.api_key)
return get_data.performance(database="openmrs_aggregation")
else:
abort(401, "Access denied.")
@route(base_url+'/performance/hiv/<date>')
def performace_hiv(date):
if api_key.check_key(request.query.api_key,"hiv","performace"):
api_key.accessed(request.query.api_key)
if request.query.type=="csv":
data=get_data.performance_by_week(date,database="openmrs_aggregation")
return helper_functions.weeks_to_csv(data)
else:
return get_data.performance_by_week(date)
else:
abort(401, "Access denied.")
@route(base_url+'/performance/mch')
def performace_mch():
if api_key.check_key(request.query.api_key,"mch","performance"):
api_key.accessed(request.query.api_key)
return get_data.performance(database="mch_aggregation")
else:
abort(401, "Access denied.")
@route(base_url+'/performance/mch/<date>')
def performace_mch(date):
if api_key.check_key(request.query.api_key,"mch","performance"):
api_key.accessed(request.query.api_key)
print request.query.type,"csv"
if request.query.type=="csv":
data=get_data.performance_by_week(date,database="mch_aggregation")
return helper_functions.weeks_to_csv(data)
else:
return get_data.performance_by_week(date,database="mch_aggregation")
else:
abort(401, "Access denied.")
@route(base_url+'/patients/mch')
def patients_mch():
if api_key.check_key(request.query.api_key,"mch","patients"):
api_key.accessed(request.query.api_key)
if request.query.type=="csv":
data=get_data.patients(database="mch_aggregation")
return helper_functions.patients_to_csv(data)
else:
return get_data.patients(database="mch_aggregation")
else:
abort(401, "Access denied.")
@route(base_url+'/patients/hiv')
def patients_hiv():
if api_key.check_key(request.query.api_key,"hiv","patients"):
api_key.accessed(request.query.api_key)
if request.query.type=="csv":
data=get_data.patients(database="openmrs_aggregation")
return helper_functions.patients_to_csv(data)
else:
return {'patients':get_data.patients(database="openmrs_aggregation")}
else:
abort(401, "Access denied.")
@route(base_url+'/report/hiv/<location>')
def report_hiv(location):
if request.query.start:
start=request.query.start
start=datetime.datetime.strptime(start,"%d/%m/%Y")
else:
start=datetime.datetime(1990,1,1)
if request.query.end:
end=request.query.end
end=datetime.datetime.strptime(end,"%d/%m/%Y")
else:
end=datetime.datetime.now()
return get_data.report_hiv(start,end,location)
@route(base_url+'/report/mch/<location>')
def report_hiv(location):
if request.query.start:
start=request.query.start
start=datetime.datetime.strptime(start,"%d/%m/%Y")
else:
start=datetime.datetime(1990,1,1)
if request.query.end:
end=request.query.end
end=datetime.datetime.strptime(end,"%d/%m/%Y")
else:
end=datetime.datetime.now()
return get_data.report_mch(start,end,location)
@route(base_url+'/verification/who/hiv')
def verification_who():
if api_key.check_key(request.query.api_key,"hiv","verification_who"):
api_key.accessed(request.query.api_key)
if request.query.type=="csv":
data=get_data.verification_who(database="openmrs_aggregation")
return helper_functions.verification_to_csv(data)
else:
return get_data.verification_who(database="openmrs_aggregation")
else:
abort(401, "Access denied.")
@route(base_url+'/verification/cd4/hiv')
def verification_cd4():
if api_key.check_key(request.query.api_key,"hiv","verification_cd4"):
api_key.accessed(request.query.api_key)
if request.query.type=="csv":
data=get_data.verification_cd4(database="openmrs_aggregation")
return helper_functions.verification_to_csv(data)
else:
return get_data.verification_cd4(database="openmrs_aggregation")
else:
abort(401, "Access denied.")
@route(base_url+'/verification/followup/hiv')
def verification_followup():
if api_key.check_key(request.query.api_key,"hiv","verification_followup"):
api_key.accessed(request.query.api_key)
if request.query.type=="csv":
data=get_data.verification_followup(database="openmrs_aggregation")
return helper_functions.verification_to_csv(data)
else:
return get_data.verification_followup(database="openmrs_aggregation")
else:
abort(401, "Access denied.")
@route(base_url+'/verification/missedappointment/hiv')
def verification_missed_appointment():
if api_key.check_key(request.query.api_key,"hiv","verification_missed_appointment"):
api_key.accessed(request.query.api_key)
if request.query.type=="csv":
data=get_data.verification_missed_appointment(database="openmrs_aggregation")
return helper_functions.verification_to_csv(data)
else:
return get_data.verification_missed_appointment(database="openmrs_aggregation")
else:
abort(401, "Access denied.")
@route(base_url+'/verification/eligible/hiv')
def verification_eligible():
if api_key.check_key(request.query.api_key,"hiv","verification_eligible"):
api_key.accessed(request.query.api_key)
if request.query.type=="csv":
data=get_data.verification_eligible(database="openmrs_aggregation")
return helper_functions.verification_to_csv(data)
else:
return get_data.verification_eligible(database="openmrs_aggregation")
else:
abort(401, "Access denied.")
@route(base_url+'/neel/hiv')
def verification_eligible():
if api_key.check_key(request.query.api_key,"hiv","neel"):
api_key.accessed(request.query.api_key)
if request.query.type=="csv":
data=get_data.neel(database="openmrs_aggregation")
return helper_functions.patients_to_csv(data)
else:
return get_data.neel(database="openmrs_aggregation")
else:
abort(401, "Access denied.")
if __name__=="__main__":
run(host="localhost",port="8080",debug=True)
else:
application = app()