14
14
15
15
16
16
class DHIS2 :
17
- def __init__ (self , connection : DHIS2Connection , cache_dir : Union [str , Path ] = None ):
17
+ def __init__ (self , connection : DHIS2Connection = None , cache_dir : Union [str , Path ] = None , ** kwargs ):
18
18
"""Initialize a new DHIS2 instance.
19
19
20
20
Parameters
21
21
----------
22
- connection : openhexa DHIS2Connection
22
+ connection : openhexa DHIS2Connection, optional
23
23
An initialized openhexa dhis2 connection
24
+ kwargs:
25
+ Additional arguments to pass to initialize openhexa dhis2 connection, such as `url`, `username`, `password`
24
26
cache_dir : str, optional
25
27
Cache directory. Actual cache data will be stored under a sub-directory
26
28
named after the DHIS2 instance domain.
27
29
"""
28
30
if isinstance (cache_dir , str ):
29
31
cache_dir = Path (cache_dir )
30
- self .api = Api (connection , cache_dir )
32
+ self .api = Api (connection , cache_dir , ** kwargs )
31
33
self .meta = Metadata (self )
32
34
self .version = self .meta .system_info ().get ("version" )
33
35
self .data_value_sets = DataValueSets (self )
@@ -69,11 +71,13 @@ def organisation_unit_levels(self) -> List[dict]:
69
71
)
70
72
return levels
71
73
72
- def organisation_units (self , filter : str = None ) -> List [dict ]:
74
+ def organisation_units (self , fields : str = "id,name,level,path,geometry" , filter : str = None ) -> List [dict ]:
73
75
"""Get organisation units metadata.
74
76
75
77
Parameters
76
78
----------
79
+ fields: str, optional
80
+ DHIS2 fields to include in the response, where default value is "id,name,level,path,geometry"
77
81
filter: str, optional
78
82
DHIS2 query filter
79
83
@@ -82,7 +86,7 @@ def organisation_units(self, filter: str = None) -> List[dict]:
82
86
list of dict
83
87
Id, name, level, path and geometry of all org units.
84
88
"""
85
- params = {"fields" : "id,name,level,path,geometry" }
89
+ params = {"fields" : fields }
86
90
if filter :
87
91
params ["filter" ] = filter
88
92
org_units = []
@@ -93,18 +97,22 @@ def organisation_units(self, filter: str = None) -> List[dict]:
93
97
for ou in page ["organisationUnits" ]:
94
98
org_units .append (
95
99
{
96
- "id" : ou .get ("id" ),
97
- "name" : ou .get ("name" ),
98
- "level" : ou .get ("level" ),
99
- "path" : ou .get ("path" ),
100
- "geometry" : json .dumps (ou .get ("geometry" )) if ou .get ("geometry" ) else None ,
100
+ key : ou .get (key )
101
+ if key != "geometry"
102
+ else json .dumps (ou .get ("geometry" ))
103
+ if ou .get ("geometry" )
104
+ else None
105
+ for key in fields .split ("," )
101
106
}
102
107
)
103
108
return org_units
104
109
105
- def organisation_unit_groups (self ) -> List [dict ]:
110
+ def organisation_unit_groups (self , fields : str = "id,name,organisationUnits" ) -> List [dict ]:
106
111
"""Get organisation unit groups metadata.
107
-
112
+ Parameters
113
+ ----------
114
+ fields: str, optional
115
+ DHIS2 fields to include in the response, where default value is "id,name,organisationUnits"
108
116
Return
109
117
------
110
118
list of dict
@@ -113,23 +121,29 @@ def organisation_unit_groups(self) -> List[dict]:
113
121
org_unit_groups = []
114
122
for page in self .client .api .get_paged (
115
123
"organisationUnitGroups" ,
116
- params = {"fields" : "id,name,organisationUnits" },
124
+ params = {"fields" : fields },
117
125
):
118
126
groups = []
119
127
for group in page .get ("organisationUnitGroups" ):
120
128
groups .append (
121
129
{
122
- "id" : group .get ("id" ),
123
- "name" : group .get ("name" ),
124
- "organisation_units" : [ou .get ("id" ) for ou in group ["organisationUnits" ]],
130
+ key : group .get (key )
131
+ if key != "organisationUnits"
132
+ else [ou .get ("id" ) for ou in group ["organisationUnits" ]]
133
+ for key in fields .split ("," )
125
134
}
126
135
)
127
136
org_unit_groups += groups
128
137
return groups
129
138
130
- def datasets (self ) -> List [dict ]:
139
+ def datasets (self , fields : str = "id,name,dataSetElements,indicators,organisationUnits" ) -> List [dict ]:
131
140
"""Get datasets metadata.
132
141
142
+ Parameters
143
+ ----------
144
+ fields: str, optional
145
+ DHIS2 fields to include in the response, where default value is
146
+ "id,name,dataSetElements,indicators,organisationUnits"
133
147
Return
134
148
------
135
149
list of dict
@@ -139,23 +153,35 @@ def datasets(self) -> List[dict]:
139
153
for page in self .client .api .get_paged (
140
154
"dataSets" ,
141
155
params = {
142
- "fields" : "id,name,dataSetElements,indicators,organisationUnits" ,
156
+ "fields" : fields ,
143
157
"pageSize" : 10 ,
144
158
},
145
159
):
146
160
for ds in page ["dataSets" ]:
147
- row = {"id" : ds .get ("id" ), "name" : ds .get ("name" )}
148
- row ["data_elements" ] = [dx ["dataElement" ]["id" ] for dx in ds ["dataSetElements" ]]
149
- row ["indicators" ] = [indicator ["id" ] for indicator in ds ["indicators" ]]
150
- row ["organisation_units" ] = [ou ["id" ] for ou in ds ["organisationUnits" ]]
161
+ fields_list = fields .split ("," )
162
+ row = {}
163
+ if "data_elements" in fields_list :
164
+ row ["data_elements" ] = [dx ["dataElement" ]["id" ] for dx in ds ["dataSetElements" ]]
165
+ fields_list .remove ("data_elements" )
166
+ if "indicators" in fields_list :
167
+ row ["indicators" ] = [indicator ["id" ] for indicator in ds ["indicators" ]]
168
+ fields_list .remove ("indicators" )
169
+ if "organisation_units" in fields_list :
170
+ row ["organisation_units" ] = [ou ["id" ] for ou in ds ["organisationUnits" ]]
171
+ fields_list .remove ("organisation_units" )
172
+ row .update ({key : ds .get (key ) for key in fields_list })
151
173
datasets .append (row )
152
174
return datasets
153
175
154
- def data_elements (self , filter : str = None ) -> List [dict ]:
176
+ def data_elements (
177
+ self , fields : str = "id,name,aggregationType,zeroIsSignificant" , filter : str = None
178
+ ) -> List [dict ]:
155
179
"""Get data elements metadata.
156
180
157
181
Parameters
158
182
----------
183
+ fields: str, optional
184
+ DHIS2 fields to include in the response, where default value is "id,name,aggregationType,zeroIsSignificant"
159
185
filter: str, optional
160
186
DHIS2 query filter
161
187
@@ -164,20 +190,24 @@ def data_elements(self, filter: str = None) -> List[dict]:
164
190
list of dict
165
191
Id, name, and aggregation type of all data elements.
166
192
"""
167
- params = {"fields" : "id,name,aggregationType,zeroIsSignificant" }
193
+ params = {"fields" : fields }
168
194
if filter :
169
195
params ["filter" ] = filter
170
196
elements = []
171
197
for page in self .client .api .get_paged (
172
198
"dataElements" ,
173
199
params = params ,
174
200
):
175
- elements += page ["dataElements" ]
201
+ for element in page ["dataElements" ]:
202
+ elements .append ({key : element .get (key ) for key in params ["fields" ].split ("," )})
176
203
return elements
177
204
178
- def data_element_groups (self ) -> List [dict ]:
205
+ def data_element_groups (self , fields : str = "id,name,dataElements" ) -> List [dict ]:
179
206
"""Get data element groups metadata.
180
-
207
+ Parameters
208
+ ----------
209
+ fields: str, optional
210
+ DHIS2 fields to include in the response, where default value is "id,name,dataElements"
181
211
Return
182
212
------
183
213
list of dict
@@ -186,15 +216,14 @@ def data_element_groups(self) -> List[dict]:
186
216
de_groups = []
187
217
for page in self .client .api .get_paged (
188
218
"dataElementGroups" ,
189
- params = {"fields" : "id,name,dataElements" },
219
+ params = {"fields" : fields },
190
220
):
191
221
groups = []
192
222
for group in page .get ("dataElementGroups" ):
193
223
groups .append (
194
224
{
195
- "id" : group .get ("id" ),
196
- "name" : group .get ("name" ),
197
- "data_elements" : [ou .get ("id" ) for ou in group ["dataElements" ]],
225
+ key : group .get (key ) if key != "dataElements" else [de .get ("id" ) for de in group ["dataElements" ]]
226
+ for key in fields .split ("," )
198
227
}
199
228
)
200
229
de_groups += groups
@@ -213,7 +242,7 @@ def category_option_combos(self) -> List[dict]:
213
242
combos += page .get ("categoryOptionCombos" )
214
243
return combos
215
244
216
- def indicators (self , filter : str = None ) -> List [dict ]:
245
+ def indicators (self , fields : str = "id,name,numerator,denominator" , filter : str = None ) -> List [dict ]:
217
246
"""Get indicators metadata.
218
247
219
248
Parameters
@@ -226,18 +255,20 @@ def indicators(self, filter: str = None) -> List[dict]:
226
255
list of dict
227
256
Id, name, numerator and denominator of all indicators.
228
257
"""
229
- params = {"fields" : "id,name,numerator,denominator" }
258
+ params = {"fields" : fields }
230
259
if filter :
231
260
params ["filter" ] = filter
232
261
indicators = []
233
262
for page in self .client .api .get_paged (
234
263
"indicators" ,
235
264
params = params ,
236
265
):
266
+ for indicator in page ["indicators" ]:
267
+ indicators .append ({key : indicator .get (key ) for key in fields .split ("," )})
237
268
indicators += page ["indicators" ]
238
269
return indicators
239
270
240
- def indicator_groups (self ) -> List [dict ]:
271
+ def indicator_groups (self , fields : str = "id,name,indicators" ) -> List [dict ]:
241
272
"""Get indicator groups metadata.
242
273
243
274
Return
@@ -248,15 +279,16 @@ def indicator_groups(self) -> List[dict]:
248
279
ind_groups = []
249
280
for page in self .client .api .get_paged (
250
281
"indicatorGroups" ,
251
- params = {"fields" : "id,name,indicators" },
282
+ params = {"fields" : fields },
252
283
):
253
284
groups = []
254
285
for group in page .get ("indicatorGroups" ):
255
286
groups .append (
256
287
{
257
- "id" : group .get ("id" ),
258
- "name" : group .get ("name" ),
259
- "indicators" : [ou .get ("id" ) for ou in group ["indicators" ]],
288
+ key : group .get (key )
289
+ if key != "indicators"
290
+ else [indicator .get ("id" ) for indicator in group ["indicators" ]]
291
+ for key in fields .split ("," )
260
292
}
261
293
)
262
294
ind_groups += groups
0 commit comments