def main(): args = parse_args() init_logger(args.debug) log_start_info(__file__) dhis = Dhis(server=args.server, username=args.username, password=args.password, api_version=args.api_version) params1 = { 'fields': 'dataViewOrganisationUnits[path],userCredentials[username,userRoles[name]],name,' 'organisationUnits[path],userGroups[name]', 'paging': False } users = dhis.get(endpoint='users', file_type='json', params=params1) params2 = {'fields': 'id,name', 'paging': False} orgunits = dhis.get(endpoint='organisationUnits', file_type='json', params=params2) oumap = {ou['id']: ou['name'] for ou in orgunits['organisationUnits']} file_name = "userinfo-{}.csv".format(dhis.file_timestamp) with open(file_name, 'wb') as csvfile: fieldnames = [ 'name', 'username', 'userGroups', 'userRoles', 'orgunitPaths', 'dataViewOrgunitPaths' ] writer = csv.DictWriter(csvfile, fieldnames=fieldnames, encoding='utf-8', delimiter=',', quoting=csv.QUOTE_MINIMAL) writer.writeheader() for u in users['users']: export = { 'name': u['name'], 'username': u['userCredentials']['username'], 'userGroups': ", ".join([ug['name'] for ug in u['userGroups']]), 'userRoles': ", ".join( [ur['name'] for ur in u['userCredentials']['userRoles']]) } orgunits = [ou['path'] for ou in u['organisationUnits']] export['orgunitPaths'] = "\n".join( [replace_path(oumap, elem) for elem in orgunits]) dvorgunits = [ou['path'] for ou in u['dataViewOrganisationUnits']] export['dataViewOrgunitPaths'] = "\n".join( [replace_path(oumap, elem) for elem in dvorgunits]) writer.writerow(export) log_info(u"+++ Success! CSV file exported to {}".format(file_name))
def main(): args = parse_args() init_logger(args.debug) log_start_info(__file__) dhis = Dhis(server=args.server, username=args.username, password=args.password, api_version=None) p = dhis.post_file(endpoint='files/style', filename=args.css, content_type='text/css') print("{} CSS posted to {}. Clear your caches.".format(args.css, dhis.api_url)) print(p)
def main(): args = parse_args() dhis = Dhis(server=args.server, username=args.username, password=args.password, api_version=args.api_version) init_logger(args.debug) log_start_info(__file__) indicators = dhis.get(endpoint='indicators', file_type='json', params=get_params(args.indicator_filter)) message = analyze_result(indicators, args.indicator_filter) log_info(message) if message.startswith('No indicators'): sys.exit() log_info(u"Downloading other UIDs <-> Names...") object_mapping = object_map(dhis) file_name = u'indicators-{}.csv'.format(dhis.file_timestamp) write_to_csv(indicators, object_mapping, file_name)
def main(): args = parse_args() init_logger(args.debug) log_start_info(__file__) dhis = Dhis(server=args.server, username=args.username, password=args.password, api_version=args.api_version) if not valid_uid(args.orgunit): log_info(u"{} is not a valid DHIS2 UID".format(args.orgunit)) sys.exit() orgunit_root_uid = args.orgunit # get root orgunit endpoint1 = 'organisationUnits/{}'.format(orgunit_root_uid) params1 = {'fields': 'id,name,path,users'} root_orgunit = dhis.get(endpoint=endpoint1, params=params1) # get path of root orgunit path = root_orgunit['path'] # get all descendant orgunit UIDs (excluding self) via 'path' field filter endpoint2 = 'organisationUnits' params2 = { 'filter': ['path:^like:' + path, 'id:!eq:' + orgunit_root_uid], 'fields': 'id', 'paging': False } resp2 = dhis.get(endpoint=endpoint2, params=params2) no_of_users = len(root_orgunit['users']) # put the response ids into a list sub_uids = list(ou['id'] for ou in resp2['organisationUnits']) log_info( u"Checking users against Sub-OrgUnit assignments of Parent-OrgUnit {}". format(root_orgunit['name'])) # Python2 & 3 compatibility try: xrange except NameError: xrange = range # check each user of root orgunit # log user if: [has more than 1 orgunit associated] AND [any other user orgunit is a sub-orgunit of root orgunit] users_export = [] counter = 0 for user in root_orgunit['users']: counter += 1 user_uid = user['id'] endpoint3 = 'users/{}'.format(user_uid) params3 = {'fields': 'id,name,organisationUnits,lastUpdated'} user = dhis.get(endpoint=endpoint3, params=params3) print("({}/{}) {} ...".format(str(counter), str(no_of_users), user_uid)) user_orgunits = user['organisationUnits'] if len(user_orgunits) > 1: # find conflicting sub-orgunits and only proceed if a conflict has not been found yet, otherwise break for x in xrange(len(user_orgunits)): user_orgunit_uid = user_orgunits[x]['id'] if user_orgunit_uid != orgunit_root_uid and user_orgunit_uid in sub_uids: users_export.append(user) log_info(u"Conflicting user found: {} - UID: {}".format( user['name'], user_uid)) break # write conflicting users to CSV file if users_export: # remove orgunits for export for u in xrange(len(users_export)): users_export[u].pop('organisationUnits') file_name = 'user-orgunits_{}-{}_{}.csv'.format( root_orgunit['name'], root_orgunit['id'], dhis.file_timestamp) with open(file_name, 'w') as csv_file: # use the keys of first user as csv headers fieldnames = list(users_export[0].keys()) writer = csv.DictWriter(csv_file, fieldnames=fieldnames) writer.writeheader() writer.writerows(users_export) log_info(u"+++ Success! Exported {} users to {}".format( len(users_export), file_name)) else: log_info(u"+++ No conflicts found.")
def test_url_with_api(): with pytest.raises(SystemExit): Dhis(server='play.dhis2.org/demo/api', username='******', password='******', api_version=None)
def dhis(): return Dhis(server='play.dhis2.org/demo', username='******', password='******', api_version=None)
def dhis_localhost_ip(): return Dhis(server='127.0.0.1:8080', username='******', password='******', api_version=None)
def dhis_without_https(): return Dhis(server='http://play.dhis2.org/demo', username='******', password='******', api_version=None)