def send_datasets(domain_name, send_now=False, send_date=None): """ Sends a data set of data values in the following format: { "dataSet": "dataSetID", "completeDate": "date", "period": "period", "orgUnit": "orgUnitID", "attributeOptionCombo", "aocID", "dataValues": [ { "dataElement": "dataElementID", "categoryOptionCombo": "cocID", "value": "1", "comment": "comment1" }, { "dataElement": "dataElementID", "categoryOptionCombo": "cocID", "value": "2", "comment": "comment2" }, { "dataElement": "dataElementID", "categoryOptionCombo": "cocID", "value": "3", "comment": "comment3" } ] } See DHIS2 API docs for more details: https://docs.dhis2.org/master/en/developer/html/webapi_data_values.html """ if not send_date: send_date = datetime.today() dataset_maps = get_dataset_maps(domain_name) if not dataset_maps: return # Nothing to do for dataset_map in dataset_maps: if send_now or should_send_on_date(dataset_map, send_date): conn = dataset_map.connection_settings dataset = get_dataset(dataset_map, send_date) with conn.get_requests() as requests: requests.post('/api/dataValueSets', json=dataset)
def post(self, request, *args, **kwargs): def update_dataset_map(instance, dict_): for key, value in dict_.items(): if key == 'datavalue_maps': value = [DataValueMap(**v) for v in value] instance[key] = value try: new_dataset_maps = json.loads(request.POST['dataset_maps']) current_dataset_maps = get_dataset_maps(request.domain) i = -1 for i, dataset_map in enumerate(current_dataset_maps): if i < len(new_dataset_maps): # Update current dataset maps update_dataset_map(dataset_map, new_dataset_maps[i]) dataset_map.save() else: # Delete removed dataset maps dataset_map.delete() if i + 1 < len(new_dataset_maps): # Insert new dataset maps for j in range(i + 1, len(new_dataset_maps)): dataset_map = DataSetMap(domain=request.domain) update_dataset_map(dataset_map, new_dataset_maps[j]) dataset_map.save() get_dataset_maps.clear(request.domain) return json_response({'success': _('DHIS2 DataSet Maps saved')}) except Exception as err: return json_response({'error': str(err)}, status_code=500)
def post(self, request, *args, **kwargs): def update_dataset_map(instance, dict_): for key, value in dict_.items(): if key == 'datavalue_maps': value = [DataValueMap(**v) for v in value] instance[key] = value try: new_dataset_maps = json.loads(request.POST['dataset_maps']) current_dataset_maps = get_dataset_maps(request.domain) i = -1 for i, dataset_map in enumerate(current_dataset_maps): if i < len(new_dataset_maps): # Update current dataset maps update_dataset_map(dataset_map, new_dataset_maps[i]) dataset_map.save() else: # Delete removed dataset maps dataset_map.delete() if i + 1 < len(new_dataset_maps): # Insert new dataset maps for j in range(i + 1, len(new_dataset_maps)): dataset_map = DataSetMap(domain=request.domain) update_dataset_map(dataset_map, new_dataset_maps[j]) dataset_map.save() get_dataset_maps.clear(request.domain) return json_response({'success': _('DHIS2 DataSet Maps saved')}) except Exception as err: return json_response({'error': str(err)}, status_code=500)
def page_context(self): def to_json(dataset_map): dataset_map = dataset_map.to_json() del (dataset_map['_id']) del (dataset_map['_rev']) del (dataset_map['doc_type']) del (dataset_map['domain']) for datavalue_map in dataset_map['datavalue_maps']: del (datavalue_map['doc_type']) return dataset_map dataset_maps = [ to_json(d) for d in get_dataset_maps(self.request.domain) ] return { 'dataset_maps': dataset_maps, 'connection_settings': ConnectionSettings.objects.filter(domain=self.domain).all(), 'ucrs': get_report_configs_for_domain(self.domain), 'send_data_url': reverse('send_dhis2_data', kwargs={'domain': self.domain}), 'is_json_ui': int(self.request.GET.get('json', 0)), }
def link_data_set_maps(conn_settings: ConnectionSettings): """ Links DataSetMap instances to their ConnectionSettings instance. """ for data_set_map in get_dataset_maps(conn_settings.domain): data_set_map.connection_settings_id = conn_settings.id data_set_map.save() get_dataset_maps.clear(conn_settings.domain)
def page_context(self): dataset_maps = [ d.to_json() for d in get_dataset_maps(self.request.domain) ] return { 'dataset_maps': dataset_maps, 'send_data_url': reverse('send_dhis2_data', kwargs={'domain': self.domain}), }
def get_connection_ids_in_use(domain): from corehq.motech.dhis2.dbaccessors import get_dataset_maps dataset_maps = get_dataset_maps(domain) # So far only DataSetMaps use ConnectionSettings. When more things # do (like Repeaters), this must check those too. return { m.connection_settings_id for m in dataset_maps if m.connection_settings_id }
def send_datasets(domain_name, send_now=False, send_date=None): """ Sends a data set of data values in the following format: { "dataSet": "dataSetID", "completeDate": "date", "period": "period", "orgUnit": "orgUnitID", "attributeOptionCombo", "aocID", "dataValues": [ { "dataElement": "dataElementID", "categoryOptionCombo": "cocID", "value": "1", "comment": "comment1" }, { "dataElement": "dataElementID", "categoryOptionCombo": "cocID", "value": "2", "comment": "comment2" }, { "dataElement": "dataElementID", "categoryOptionCombo": "cocID", "value": "3", "comment": "comment3" } ] } See DHIS2 API docs for more details: https://docs.dhis2.org/master/en/developer/html/webapi_data_values.html """ if not send_date: send_date = datetime.today() dhis2_conn = get_dhis2_connection(domain_name) dataset_maps = get_dataset_maps(domain_name) if not dhis2_conn or not dataset_maps: return # Nothing to do api = JsonApiRequest( dhis2_conn.server_url, dhis2_conn.username, bz2.decompress(b64decode(dhis2_conn.password)), domain_name=domain_name, ) endpoint = 'dataValueSets' for dataset_map in dataset_maps: if send_now or dataset_map.should_send_on_date(send_date): try: dataset = dataset_map.get_dataset(send_date) except Exception as err: domain_log_level = getattr(dhis2_conn, 'log_level', logging.INFO) log_level = logging.ERROR if log_level >= domain_log_level: JsonApiLog.log( log_level, api, str(err), response_status=None, response_body=None, method_func=api.post, request_url=api.get_request_url(endpoint), ) else: api.post(endpoint, dataset)
def send_datasets(domain_name, send_now=False, send_date=None): """ Sends a data set of data values in the following format: { "dataSet": "dataSetID", "completeDate": "date", "period": "period", "orgUnit": "orgUnitID", "attributeOptionCombo", "aocID", "dataValues": [ { "dataElement": "dataElementID", "categoryOptionCombo": "cocID", "value": "1", "comment": "comment1" }, { "dataElement": "dataElementID", "categoryOptionCombo": "cocID", "value": "2", "comment": "comment2" }, { "dataElement": "dataElementID", "categoryOptionCombo": "cocID", "value": "3", "comment": "comment3" } ] } See DHIS2 API docs for more details: https://docs.dhis2.org/master/en/developer/html/webapi_data_values.html """ if not send_date: send_date = datetime.today() dhis2_conn = Dhis2Connection.objects.filter(domain=domain_name).first() dataset_maps = get_dataset_maps(domain_name) if not dataset_maps: return # Nothing to do for dataset_map in dataset_maps: if send_now or dataset_map.should_send_on_date(send_date): if dataset_map.connection_settings: conn = dataset_map.connection_settings url = conn.url endpoint = '/api/dataValueSets' else: conn = dhis2_conn url = conn.server_url endpoint = 'dataValueSets' if not conn: continue dataset = dataset_map.get_dataset(send_date) requests = Requests(domain_name, url, conn.username, conn.plaintext_password, verify=not conn.skip_cert_verify, notify_addresses=conn.notify_addresses if hasattr(conn, 'notify_addresses') else None) requests.post(endpoint, json=dataset)
def used_by(self): """ Returns the names of kinds of things that are currently using this instance. Used for informing users, and determining whether the instance can be deleted. """ from corehq.motech.dhis2.dbaccessors import get_dataset_maps kinds = set() if self.incrementalexport_set.exists(): kinds.add(_('Incremental Exports')) if any(m.connection_settings_id == self.id for m in get_dataset_maps(self.domain)): kinds.add(_('DHIS2 DataSet Maps')) # TODO: Check Repeaters (when Repeaters use ConnectionSettings) # TODO: Check OpenmrsImporters (ditto) return kinds
def page_context(self): def to_json(dataset_map): dataset_map = dataset_map.to_json() del(dataset_map['_id']) del(dataset_map['_rev']) del(dataset_map['doc_type']) del(dataset_map['domain']) for datavalue_map in dataset_map['datavalue_maps']: del(datavalue_map['doc_type']) return dataset_map dataset_maps = [to_json(d) for d in get_dataset_maps(self.request.domain)] return { 'dataset_maps': dataset_maps, 'send_data_url': reverse('send_dhis2_data', kwargs={'domain': self.domain}), 'is_json_ui': int(self.request.GET.get('json', 0)), }
def send_datasets(domain_name, send_now=False, send_date=None): """ Sends a data set of data values in the following format: { "dataSet": "dataSetID", "completeDate": "date", "period": "period", "orgUnit": "orgUnitID", "attributeOptionCombo", "aocID", "dataValues": [ { "dataElement": "dataElementID", "categoryOptionCombo": "cocID", "value": "1", "comment": "comment1" }, { "dataElement": "dataElementID", "categoryOptionCombo": "cocID", "value": "2", "comment": "comment2" }, { "dataElement": "dataElementID", "categoryOptionCombo": "cocID", "value": "3", "comment": "comment3" } ] } See DHIS2 API docs for more details: https://docs.dhis2.org/master/en/developer/html/webapi_data_values.html """ if not send_date: send_date = datetime.today() dhis2_conn = get_dhis2_connection(domain_name) dataset_maps = get_dataset_maps(domain_name) if not dhis2_conn or not dataset_maps: return # Nothing to do requests = Requests( domain_name, dhis2_conn.server_url, dhis2_conn.username, bz2.decompress(b64decode(dhis2_conn.password)), verify=not dhis2_conn.skip_cert_verify, ) endpoint = 'dataValueSets' for dataset_map in dataset_maps: if send_now or dataset_map.should_send_on_date(send_date): dataset = dataset_map.get_dataset(send_date) requests.post(endpoint, json=dataset)
def send_datasets(domain_name, send_now=False, send_date=None): """ Sends a data set of data values in the following format: { "dataSet": "dataSetID", "completeDate": "date", "period": "period", "orgUnit": "orgUnitID", "attributeOptionCombo", "aocID", "dataValues": [ { "dataElement": "dataElementID", "categoryOptionCombo": "cocID", "value": "1", "comment": "comment1" }, { "dataElement": "dataElementID", "categoryOptionCombo": "cocID", "value": "2", "comment": "comment2" }, { "dataElement": "dataElementID", "categoryOptionCombo": "cocID", "value": "3", "comment": "comment3" } ] } See DHIS2 API docs for more details: https://docs.dhis2.org/master/en/developer/html/webapi_data_values.html """ if not send_date: send_date = datetime.today() dhis2_conn = get_dhis2_connection(domain_name) dataset_maps = get_dataset_maps(domain_name) if not dhis2_conn or not dataset_maps: return # Nothing to do requests = Requests( domain_name, dhis2_conn.server_url, dhis2_conn.username, bz2.decompress(b64decode(dhis2_conn.password)), ) endpoint = 'dataValueSets' for dataset_map in dataset_maps: if send_now or dataset_map.should_send_on_date(send_date): dataset = dataset_map.get_dataset(send_date) requests.post(endpoint, json=dataset)
def page_context(self): dataset_maps = [d.to_json() for d in get_dataset_maps(self.request.domain)] return { 'dataset_maps': dataset_maps, 'send_data_url': reverse('send_dhis2_data', kwargs={'domain': self.domain}), }