def test_import_monthly_not_first(self): with patch('corehq.motech.openmrs.models.datetime') as datetime_mock: datetime_mock.today.return_value = datetime.date(2019, 9, 2) importer = OpenmrsImporter( domain=domain_name, import_frequency=IMPORT_FREQUENCY_MONTHLY, ) self.assertFalse(importer.should_import_today())
def test_import_weekly_tuesday(self): with patch('corehq.motech.openmrs.models.datetime') as datetime_mock: datetime_mock.today.return_value = datetime.date(2019, 9, 17) importer = OpenmrsImporter( domain=domain_name, import_frequency=IMPORT_FREQUENCY_WEEKLY, ) self.assertTrue(importer.should_import_today())
def post(self, request, *args, **kwargs): try: new_openmrs_importers = json.loads( request.POST['openmrs_importers']) current_openmrs_importers = get_openmrs_importers_by_domain( request.domain) i = -1 for i, openmrs_importer in enumerate(current_openmrs_importers): if i < len(new_openmrs_importers): self._update_importer(openmrs_importer, new_openmrs_importers[i]) else: # Delete removed OpenMRS Importers openmrs_importer.delete() if i + 1 < len(new_openmrs_importers): # Insert new OpenMRS Importers for j in range(i + 1, len(new_openmrs_importers)): openmrs_importer = OpenmrsImporter(domain=request.domain) self._update_importer(openmrs_importer, new_openmrs_importers[j]) get_openmrs_importers_by_domain.clear(request.domain) return json_response({ 'message': _('OpenMRS Importers saved'), 'error': None }) except Exception as err: return json_response({ 'message': None, 'error': str(err) }, status_code=500)
def get_openmrs_importers_by_domain(domain_name): return OpenmrsImporter.view( 'by_domain_doc_type_date/view', key=[domain_name, 'OpenmrsImporter', None], include_docs=True, reduce=False, ).all()
def import_patients_with_importer(importer_json): importer = OpenmrsImporter.wrap(importer_json) password = b64_aes_decrypt(importer.password) requests = get_basic_requests( importer.domain, importer.server_url, importer.username, password, notify_addresses=importer.notify_addresses, ) if importer.location_type_name: try: location_type = LocationType.objects.get(domain=importer.domain, name=importer.location_type_name) except LocationType.DoesNotExist: requests.notify_error( f'No organization level named "{importer.location_type_name}" ' f'found in project space "{importer.domain}".' ) return if importer.location_id: location = SQLLocation.objects.filter(domain=importer.domain).get(importer.location_id) locations = location.get_descendants.filter(location_type=location_type) else: locations = SQLLocation.objects.filter(domain=importer.domain, location_type=location_type) for location in locations: # Assign cases to the first user in the location, not to the location itself owner = get_one_commcare_user_at_location(importer.domain, location.location_id) if not owner: requests.notify_error( f'Project space "{importer.domain}" at location ' f'"{location.name}" has no user to own cases imported ' f'from OpenMRS Importer "{importer}"' ) continue # The same report is fetched for each location. WE DO THIS # ASSUMING THAT THE LOCATION IS USED IN THE REPORT # PARAMETERS. If not, OpenMRS will return THE SAME PATIENTS # multiple times and they will be assigned to a different # user each time. try: import_patients_of_owner(requests, importer, importer.domain, owner.user_id, location) except ConfigurationError as err: requests.notify_error(str(err)) elif importer.owner_id: if not is_valid_owner(importer.owner_id): requests.notify_error( f'Error importing patients for project space "{importer.domain}" ' f'from OpenMRS Importer "{importer}": owner_id "{importer.owner_id}" ' 'is invalid.' ) return try: import_patients_of_owner(requests, importer, importer.domain, importer.owner_id) except ConfigurationError as err: requests.notify_error(str(err)) else: requests.notify_error( f'Error importing patients for project space "{importer.domain}" from ' f'OpenMRS Importer "{importer}": Unable to determine the owner of ' 'imported cases without either owner_id or location_type_name' )
def get_importer(): importer = OpenmrsImporter.wrap({ 'domain': 'test-domain', 'server_url': 'http://www.example.com/openmrs', 'username': '******', 'password': '******', 'location_id': '', 'import_frequency': IMPORT_FREQUENCY_MONTHLY, 'log_level': logging.INFO, 'timezone': 'Africa/Maputo', 'report_uuid': 'c0ffee', 'report_params': {}, 'case_type': 'patient', 'owner_id': '123456', 'location_type_name': '', 'external_id_column': 'NID', 'name_columns': 'nome_inicial apelido', 'column_map': [ {'column': 'NID', 'property': 'nid'}, {'column': 'nome_inicial', 'property': 'nome'}, {'column': 'apelido', 'property': 'apelido'}, {'column': 'Telefone', 'property': 'contact_phone_number'}, {'column': 'provincia', 'property': 'provincia'}, {'column': 'distrito', 'property': 'distrito'}, {'column': 'localidade', 'property': 'avenida'}, {'column': 'bairro', 'property': 'bairro'}, {'column': 'Referencia', 'property': 'celula'}, {'column': 'genero', 'property': 'genero'}, {'column': 'data_do_nacimento', 'property': 'data_do_nacimento'}, {'column': 'filhos', 'property': 'numero_de_filhos'}, {'column': 'testados', 'property': 'numero_de_filhos_testados'}, {'column': 'positivos', 'property': 'numero_de_filhos_positivos'}, {'column': 'serologia', 'property': 'parceiro_serologia'}, {'column': 'conviventes', 'property': 'numero_conviventes'}, {'column': 'tarv_elegivel', 'property': 'tarv_elegivel'}, {'column': 'estado_tarv', 'property': 'estado_tarv'}, {'column': 'gravida', 'property': 'gravida'}, {'column': 'coinfectado', 'property': 'coinfectado'}, {'column': 'a_faltar', 'property': 'a_faltar'}, { 'column': 'data_ultima_consulta', 'data_type': 'posix_milliseconds', 'property': 'data_ultima_consulta' }, { 'column': 'data_proxima_consulta', 'data_type': 'posix_milliseconds', 'property': 'data_proxima_consulta' } ], }) try: yield importer finally: del importer
def import_patients_with_importer(importer_json): importer = OpenmrsImporter.wrap(importer_json) password = b64_aes_decrypt(importer.password) requests = Requests(importer.domain, importer.server_url, importer.username, password) if importer.location_type_name: try: location_type = LocationType.objects.get( domain=importer.domain, name=importer.location_type_name) except LocationType.DoesNotExist: logger.error( f'No organization level named "{importer.location_type_name}" ' f'found in project space "{importer.domain}".') return if importer.location_id: location = SQLLocation.objects.filter(domain=importer.domain).get( importer.location_id) locations = location.get_descendants.filter( location_type=location_type) else: locations = SQLLocation.objects.filter(domain=importer.domain, location_type=location_type) for location in locations: # Assign cases to the first user in the location, not to the location itself owner = get_one_commcare_user_at_location(importer.domain, location.location_id) if not owner: logger.error( f'Project space "{importer.domain}" at location ' f'"{location.name}" has no user to own cases imported ' f'from OpenMRS Importer "{importer}"') continue import_patients_of_owner(requests, importer, importer.domain, owner, location) elif importer.owner_id: try: owner = CommCareUser.get(importer.owner_id) except ResourceNotFound: logger.error( f'Project space "{importer.domain}" has no user to own cases ' f'imported from OpenMRS Importer "{importer}"') return import_patients_of_owner(requests, importer, importer.domain, owner) else: logger.error( f'Error importing patients for project space "{importer.domain}" from ' f'OpenMRS Importer "{importer}": Unable to determine the owner of ' 'imported cases without either owner_id or location_type_name')
def save(self, domain_name): try: importers = get_openmrs_importers_by_domain(domain_name) importer = importers[0] if importers else None # TODO: Support multiple if importer is None: importer = OpenmrsImporter(domain=domain_name) importer.server_url = self.cleaned_data['server_url'] importer.username = self.cleaned_data['username'] if self.cleaned_data['password']: # Don't save it if it hasn't been changed. importer.password = b64_aes_encrypt(self.cleaned_data['password']) importer.location_id = self.cleaned_data['location_id'] importer.import_frequency = self.cleaned_data['import_frequency'] importer.log_level = self.cleaned_data['log_level'] importer.report_uuid = self.cleaned_data['report_uuid'] importer.report_params = self.cleaned_data['report_params'] importer.case_type = self.cleaned_data['case_type'] importer.owner_id = self.cleaned_data['owner_id'] importer.location_type_name = self.cleaned_data['location_type_name'] importer.external_id_column = self.cleaned_data['external_id_column'] importer.name_columns = self.cleaned_data['name_columns'] importer.column_map = list(map(ColumnMapping.wrap, self.cleaned_data['column_map'])) importer.save() return True except Exception as err: logging.error('Unable to save OpenMRS Importer: %s' % err) return False
def test_import_daily(self): importer = OpenmrsImporter( domain=domain_name, import_frequency=IMPORT_FREQUENCY_DAILY, ) self.assertTrue(importer.should_import_today())