def sync_facility_to_supply_point(domain, facility): supply_point = get_supply_point(domain, facility) facility_dict = { 'domain': domain, 'location_type': facility.type, 'external_id': facility.code, 'name': facility.name, 'site_code': facility.code, # todo: do they have a human readable code? 'latitude': facility.latitude, 'longitude': facility.longitude, } parent_sp = None if facility.parent_id: parent_sp = get_supply_point(domain, facility.parent_id) if not parent_sp: raise BadParentException('No matching supply point with code %s found' % facility.parent_id) if supply_point is None: if parent_sp: facility_dict['parent'] = parent_sp.location facility_loc = Location(**facility_dict) facility_loc.save() return make_supply_point(domain, facility_loc) else: facility_loc = supply_point.location if parent_sp and facility_loc.parent_id != parent_sp.location._id: raise BadParentException('You are trying to move a location. This is currently not supported.') should_save = apply_updates(facility_loc, facility_dict) if should_save: facility_loc.save() return supply_point
def create_locations(locations, types, parent): for name, children in locations: location = Location(domain=domain, name=name, parent=parent, location_type=types[0]) location.save() locations_dict[name] = location.sql_location create_locations(children, types[1:], location)
def _create_location_from_supply_point(self, supply_point, location): try: sql_location = SQLLocation.objects.get(domain=self.domain, site_code=supply_point.code) return Loc.get(sql_location.location_id) except SQLLocation.DoesNotExist: parent = location if supply_point.code in TEACHING_HOSPITAL_MAPPING: parent = self._sync_parent(TEACHING_HOSPITAL_MAPPING[ supply_point.code]['parent_external_id']) new_location = Loc(parent=parent) new_location.domain = self.domain new_location.location_type = supply_point.type new_location.name = supply_point.name new_location.site_code = supply_point.code if supply_point.supervised_by: new_location.metadata[ 'supervised_by'] = supply_point.supervised_by new_location.save() sql_loc = new_location.sql_location sql_loc.products = SQLProduct.objects.filter( domain=self.domain, code__in=supply_point.products) sql_loc.save() return new_location
def test_archiving_location_should_resync(self): """ When locations are archived, we should resync them """ couch_location = Location( domain=self.domain, name='winterfell', location_type=self.location_type.name, ) couch_location.save() after_save = datetime.utcnow() location = SQLLocation.objects.last() self.assertEqual(couch_location._id, location.location_id) self.assertEqual('winterfell', location.name) location_db = _location_footprint([location]) self.assertFalse(should_sync_locations(SyncLog(date=after_save), location_db, self.user)) # archive the location couch_location.archive() after_archive = datetime.utcnow() location = SQLLocation.objects.last() location_db = _location_footprint([location]) self.assertTrue(should_sync_locations(SyncLog(date=after_save), location_db, self.user)) self.assertFalse(should_sync_locations(SyncLog(date=after_archive), location_db, self.user))
def make_loc(code, name, domain, type, metadata=None, parent=None): name = name or code LocationType.objects.get(domain=domain, name=type) loc = Location(site_code=code, name=name, domain=domain, location_type=type, parent=parent) loc.metadata = metadata or {} loc.save() return loc
def _create_data(self, domain_name, i): product = Product(domain=domain_name, name='test-{}'.format(i)) product.save() location = Location( domain=domain_name, site_code='testcode-{}'.format(i), name='test-{}'.format(i), location_type='facility' ) location.save() SupplyPointCase.create_from_location(domain_name, location) report = StockReport.objects.create( type='balance', domain=domain_name, form_id='fake', date=datetime.utcnow() ) StockTransaction.objects.create( report=report, product_id=product.get_id, sql_product=SQLProduct.objects.get(product_id=product.get_id), section_id='stock', type='stockonhand', case_id=location.linked_supply_point().get_id, stock_on_hand=100 )
def setUp(self): self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy') self.stock_api_object = MockEWSStockDataSynchronization(TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), 'data') initial_bootstrap(TEST_DOMAIN) self.api_object = EWSApi(TEST_DOMAIN, self.endpoint) self.api_object.prepare_commtrack_config() config = EWSGhanaConfig() config.domain = TEST_DOMAIN config.enabled = True config.all_stock_data = True config.password = '******' config.username = '******' config.url = 'http://test-api.com/' config.save() l1 = Location( name='Test location 1', external_id='3445', location_type='Hospital', domain=TEST_DOMAIN ) l2 = Location( name='Test location 2', external_id='4407', location_type='Hospital', domain=TEST_DOMAIN ) l1.save() l2.save() with open(os.path.join(self.datapath, 'sample_products.json')) as f: for product_json in json.loads(f.read()): self.api_object.product_sync(Product(product_json))
def testCreateVirtualFacility(self): loc = Location(site_code='1234', name='beavis', domain=self.domain, location_type='chw') loc.save() sp = make_supply_point(self.domain, loc) self.assertTrue(sync_supply_point_to_openlmis(sp, self.api)) self.assertTrue(sync_supply_point_to_openlmis(sp, self.api, False))
def _create_data(self, domain_name, i): product = Product(domain=domain_name, name='test-{}'.format(i)) product.save() location = Location(domain=domain_name, site_code='testcode-{}'.format(i), name='test-{}'.format(i), location_type='facility') location.save() report = StockReport.objects.create( type='balance', domain=domain_name, form_id='fake', date=datetime.utcnow(), server_date=datetime.utcnow(), ) StockTransaction.objects.create( report=report, product_id=product.get_id, sql_product=SQLProduct.objects.get(product_id=product.get_id), section_id='stock', type='stockonhand', case_id=location.linked_supply_point().get_id, stock_on_hand=100) SMS.objects.create(domain=domain_name) Call.objects.create(domain=domain_name) SQLLastReadMessage.objects.create(domain=domain_name) ExpectedCallback.objects.create(domain=domain_name) PhoneNumber.objects.create(domain=domain_name, is_two_way=False, pending_verification=False) event = MessagingEvent.objects.create( domain=domain_name, date=datetime.utcnow(), source=MessagingEvent.SOURCE_REMINDER, content_type=MessagingEvent.CONTENT_SMS, status=MessagingEvent.STATUS_COMPLETED) MessagingSubEvent.objects.create( parent=event, date=datetime.utcnow(), recipient_type=MessagingEvent.RECIPIENT_CASE, content_type=MessagingEvent.CONTENT_SMS, status=MessagingEvent.STATUS_COMPLETED) SelfRegistrationInvitation.objects.create( domain=domain_name, phone_number='999123', token=uuid.uuid4().hex, expiration_date=datetime.utcnow().date(), created_date=datetime.utcnow()) backend = SQLMobileBackend.objects.create(domain=domain_name, is_global=False) SQLMobileBackendMapping.objects.create( domain=domain_name, backend_type=SQLMobileBackend.SMS, prefix=str(i), backend=backend) MobileBackendInvitation.objects.create(domain=domain_name, backend=backend)
def setUp(self): self.endpoint = MockEndpoint("http://test-api.com/", "dummy", "dummy") self.stock_api_object = MockILSStockDataSynchronization(TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), "data") initial_bootstrap(TEST_DOMAIN) self.api_object = ILSGatewayAPI(TEST_DOMAIN, self.endpoint) self.api_object.prepare_commtrack_config() config = ILSGatewayConfig() config.domain = TEST_DOMAIN config.enabled = True config.all_stock_data = True config.password = "******" config.username = "******" config.url = "http://test-api.com/" config.save() l1 = Location(name="Test location 1", external_id="3445", location_type="FACILITY", domain=TEST_DOMAIN) l2 = Location(name="Test location 2", external_id="4407", location_type="FACILITY", domain=TEST_DOMAIN) l1.save() l2.save() SupplyPointCase.create_from_location(TEST_DOMAIN, l1) SupplyPointCase.create_from_location(TEST_DOMAIN, l2) with open(os.path.join(self.datapath, "sample_products.json")) as f: for product_json in json.loads(f.read()): self.api_object.product_sync(Product(product_json)) StockTransaction.objects.all().delete()
def testOtherCharacters(self): location = Location(name=u"Somé$ #Location (Old)", domain=self.domain, location_type="type") location.save() self.assertEqual(location.site_code, 'some_location_old')
def testSimpleName(self): location = Location(name="Some Location", domain=self.domain, location_type="type") location.save() self.assertEqual(location.site_code, 'some_location')
def location_sync(self, ews_location): try: sql_loc = SQLLocation.objects.get( domain=self.domain, external_id=int(ews_location.id) ) location = Loc.get(sql_loc.location_id) except SQLLocation.DoesNotExist: location = None if not location: if ews_location.parent_id: try: loc_parent = SQLLocation.objects.get( external_id=ews_location.parent_id, domain=self.domain ) loc_parent_id = loc_parent.location_id except SQLLocation.DoesNotExist: parent = self.endpoint.get_location(ews_location.parent_id) loc_parent = self.location_sync(Location(parent)) loc_parent_id = loc_parent._id location = Loc(parent=loc_parent_id) else: location = Loc() location.lineage = [] self._set_location_properties(location, ews_location) location.save() self._set_up_supply_point(location, ews_location) else: location_dict = { 'name': ews_location.name, 'latitude': float(ews_location.latitude) if ews_location.latitude else None, 'longitude': float(ews_location.longitude) if ews_location.longitude else None, 'site_code': ews_location.code.lower(), 'external_id': str(ews_location.id), } if apply_updates(location, location_dict): location.save() for supply_point in ews_location.supply_points: sp = SupplyPointCase.view('hqcase/by_domain_external_id', key=[self.domain, str(supply_point.id)], reduce=False, include_docs=True, limit=1).first() if sp: sql_location = sp.location.sql_location sql_location.stocks_all_products = False if not sql_location.products: sql_location.products = SQLProduct.objects.filter( domain=self.domain, code__in=supply_point.products ) sql_location.save() return location
def _create_data(self, domain_name): product = Product(domain=domain_name, name='test-product') product.save() location = Location(domain=domain_name, site_code='testcode', name='test1', location_type='facility') location.save() self.locations[domain_name] = location.get_id DeliveryGroupReport.objects.create(location_id=location.get_id, quantity=1, message='test', delivery_group='A') SupplyPointWarehouseRecord.objects.create( supply_point=location.get_id, create_date=datetime.utcnow()) Alert.objects.create(text='test', expires=datetime.utcnow(), date=datetime.utcnow(), location_id=location.get_id) organization_summary = OrganizationSummary.objects.create( date=datetime.utcnow(), location_id=location.get_id) GroupSummary.objects.create(org_summary=organization_summary) ProductAvailabilityData.objects.create(product=product.get_id, date=datetime.utcnow(), location_id=location.get_id) SupplyPointStatus.objects.create(location_id=location.get_id, status_type='del_fac', status_value='received') HistoricalLocationGroup.objects.create( location_id=location.sql_location, group='A', date=datetime.utcnow().date()) ReportRun.objects.create(domain=domain_name, start=datetime.utcnow(), end=datetime.utcnow(), start_run=datetime.utcnow()) ILSNotes.objects.create(location=location.sql_location, domain=domain_name, user_name='test', date=datetime.utcnow(), text='test') SupervisionDocument.objects.create(domain=domain_name, document='test', data_type='test', name='test')
def sync_facilities(request, domain): commtrack_settings = request.project.commtrack_settings # create Facility Registry and Facility LocationTypes if they don't exist if not any(lt.name == 'Facility Registry' for lt in commtrack_settings.location_types): commtrack_settings.location_types.extend([ LocationType(name='Facility Registry', allowed_parents=['']), LocationType(name='Facility', allowed_parents=['Facility Registry']) ]) commtrack_settings.save() registry_locs = dict( (l.external_id, l) for l in Location.filter_by_type(domain, 'Facility Registry')) # sync each registry and add/update Locations for each Facility for registry in FacilityRegistry.by_domain(domain): registry.sync_with_remote() try: registry_loc = registry_locs[registry.url] except KeyError: registry_loc = Location(domain=domain, location_type='Facility Registry', external_id=registry.url) registry_loc.name = registry.name registry_loc.save() registry_loc._seen = True facility_locs = dict( (l.external_id, l) for l in Location.filter_by_type(domain, 'Facility', registry_loc)) for facility in registry.get_facilities(): uuid = facility.data['uuid'] try: facility_loc = facility_locs[uuid] except KeyError: facility_loc = Location(domain=domain, location_type='Facility', external_id=uuid, parent=registry_loc) facility_loc.name = facility.data.get('name', 'Unnamed Facility') facility_loc.save() facility_loc._seen = True for id, f in facility_locs.iteritems(): if not hasattr(f, '_seen'): f.delete() for id, r in registry_locs.iteritems(): if not hasattr(r, '_seen'): r.delete() return HttpResponse('OK')
def make_loc(code, name=None, domain=TEST_DOMAIN, type=TEST_LOCATION_TYPE, parent=None): if not Domain.get_by_name(domain): raise AssertionError("You can't make a location on a fake domain") name = name or code LocationType.objects.get_or_create(domain=domain, name=type, defaults={'administrative': False}) loc = Location(site_code=code, name=name, domain=domain, location_type=type, parent=parent) loc.save() return loc
def make_loc(code, name=None, domain=TEST_DOMAIN, type=TEST_LOCATION_TYPE, parent=None, is_archived=False): name = name or code loc = Location( site_code=code, name=name, domain=domain, location_type=type, parent=parent, is_archived=is_archived ) loc.save() return loc
def make_loc(code, name, domain, type, parent=None): name = name or code sql_type, _ = LocationType.objects.get_or_create(domain=domain, name=type) loc = Location(site_code=code, name=name, domain=domain, location_type=type, parent=parent) loc.save() sql_location = loc.sql_location sql_location.products = [] sql_location.save() return loc
def make_loc(code, name, domain, type, metadata=None, parent=None): name = name or code location_type, _ = LocationType.objects.get_or_create(domain=domain, name=type) loc = Location(site_code=code, name=name, domain=domain, location_type=type, parent=parent) loc.metadata = metadata or {} loc.save() if not location_type.administrative: SupplyPointCase.create_from_location(domain, loc) loc.save() return loc
def testOtherCharacters(self): location = Location( name=u"Somé$ #Location (Old)", domain=self.domain.name, location_type="type" ) location.save() self.assertEqual(location.site_code, 'some_location_old')
def testSimpleName(self): location = Location( name="Some Location", domain=self.domain.name, location_type="type" ) location.save() self.assertEqual(location.site_code, 'some_location')
def _create_data(self, domain_name): product = Product(domain=domain_name, name="test-product") product.save() location = Location(domain=domain_name, site_code="testcode", name="test1", location_type="facility") location.save() self.locations[domain_name] = location.sql_location user = CommCareUser.create(domain=domain_name, username="******".format(domain_name), password="******") FacilityInCharge.objects.create(user_id=user.get_id, location=location.sql_location)
def sync_facilities(request, domain): commtrack_settings = request.project.commtrack_settings # create Facility Registry and Facility LocationTypes if they don't exist if not any(lt.name == 'Facility Registry' for lt in commtrack_settings.location_types): commtrack_settings.location_types.extend([ LocationType(name='Facility Registry', allowed_parents=['']), LocationType(name='Facility', allowed_parents=['Facility Registry']) ]) commtrack_settings.save() registry_locs = dict((l.external_id, l) for l in Location.filter_by_type(domain, 'Facility Registry')) # sync each registry and add/update Locations for each Facility for registry in FacilityRegistry.by_domain(domain): registry.sync_with_remote() try: registry_loc = registry_locs[registry.url] except KeyError: registry_loc = Location( domain=domain, location_type='Facility Registry', external_id=registry.url) registry_loc.name = registry.name registry_loc.save() registry_loc._seen = True facility_locs = dict((l.external_id, l) for l in Location.filter_by_type(domain, 'Facility', registry_loc)) for facility in registry.get_facilities(): uuid = facility.data['uuid'] try: facility_loc = facility_locs[uuid] except KeyError: facility_loc = Location( domain=domain, location_type='Facility', external_id=uuid, parent=registry_loc) facility_loc.name = facility.data.get('name', 'Unnamed Facility') facility_loc.save() facility_loc._seen = True for id, f in facility_locs.iteritems(): if not hasattr(f, '_seen'): f.delete() for id, r in registry_locs.iteritems(): if not hasattr(r, '_seen'): r.delete() return HttpResponse('OK')
def make_loc(code, name=None, domain=TEST_DOMAIN, type=TEST_LOCATION_TYPE, parent=None): name = name or code loc = Location(site_code=code, name=name, domain=domain, location_type=type, parent=parent) loc.save() return loc
def sync_ilsgateway_location(domain, endpoint, ilsgateway_location): location = Location.view('commtrack/locations_by_code', key=[domain, ilsgateway_location.code.lower()], include_docs=True).first() if not location: if ilsgateway_location.parent: loc_parent = SupplyPointCase.view('hqcase/by_domain_external_id', key=[domain, str(ilsgateway_location.parent)], reduce=False, include_docs=True).first() if not loc_parent: parent = endpoint.get_location(ilsgateway_location.parent) loc_parent = sync_ilsgateway_location(domain, endpoint, Loc.from_json(parent)) else: loc_parent = loc_parent.location location = Location(parent=loc_parent) else: location = Location() location.lineage = [] location.domain = domain location.name = ilsgateway_location.name if ilsgateway_location.groups: location.metadata = {'groups': ilsgateway_location.groups} if ilsgateway_location.latitude: location.latitude = float(ilsgateway_location.latitude) if ilsgateway_location.longitude: location.longitude = float(ilsgateway_location.longitude) location.location_type = ilsgateway_location.type location.site_code = ilsgateway_location.code location.external_id = str(ilsgateway_location.id) location.save() if not SupplyPointCase.get_by_location(location): SupplyPointCase.create_from_location(domain, location) else: location_dict = { 'name': ilsgateway_location.name, 'latitude': float(ilsgateway_location.latitude) if ilsgateway_location.latitude else None, 'longitude': float(ilsgateway_location.longitude) if ilsgateway_location.longitude else None, 'type': ilsgateway_location.type, 'site_code': ilsgateway_location.code.lower(), 'external_id': str(ilsgateway_location.id), } case = SupplyPointCase.get_by_location(location) if apply_updates(location, location_dict): location.save() if case: case.update_from_location(location) else: SupplyPointCase.create_from_location(domain, location) return location
def make_loc(code, name=None, domain=TEST_DOMAIN, type=TEST_LOCATION_TYPE, parent=None): name = name or code LocationType.objects.get_or_create(domain=domain, name=type) loc = Location(site_code=code, name=name, domain=domain, location_type=type, parent=parent) loc.save() return loc
def make_loc(code, name, domain, type, parent=None): name = name or code sql_type, _ = LocationType.objects.get_or_create(domain=domain, name=type) loc = Location(site_code=code, name=name, domain=domain, location_type=type, parent=parent) loc.save() if not sql_type.administrative: SupplyPointCase.create_from_location(domain, loc) loc.save() sql_location = loc.sql_location sql_location.products = [] sql_location.save() return loc
def sync_ilsgateway_location(domain, endpoint, ilsgateway_location): location = Location.view('commtrack/locations_by_code', key=[domain, ilsgateway_location.code.lower()], include_docs=True).first() if not location: if ilsgateway_location.parent: loc_parent = SupplyPointCase.view('hqcase/by_domain_external_id', key=[domain, str(ilsgateway_location.parent)], reduce=False, include_docs=True).first() if not loc_parent: parent = endpoint.get_location(ilsgateway_location.parent) loc_parent = sync_ilsgateway_location(domain, endpoint, Loc.from_json(parent)) else: loc_parent = loc_parent.location location = Location(parent=loc_parent) else: location = Location() location.lineage = [] location.domain = domain location.name = ilsgateway_location.name if ilsgateway_location.groups: location.metadata = {'groups': ilsgateway_location.groups} if ilsgateway_location.latitude: location.latitude = float(ilsgateway_location.latitude) if ilsgateway_location.longitude: location.longitude = float(ilsgateway_location.longitude) location.location_type = ilsgateway_location.type location.site_code = ilsgateway_location.code location.external_id = str(ilsgateway_location.id) location.save() if not SupplyPointCase.get_by_location(location): SupplyPointCase.create_from_location(domain, location) else: location_dict = { 'name': ilsgateway_location.name, 'latitude': float(ilsgateway_location.latitude) if ilsgateway_location.latitude else None, 'longitude': float(ilsgateway_location.longitude) if ilsgateway_location.longitude else None, 'type': ilsgateway_location.type, 'site_code': ilsgateway_location.code.lower(), 'external_id': str(ilsgateway_location.id), } case = SupplyPointCase.get_by_location(location) if apply_updates(location, location_dict): location.save() if case: case.update_from_location(location) else: SupplyPointCase.create_from_location(domain, location)
def get_or_create_msd_zone(region): msd_name = _get_msd_name(region.name) msd_code = MSDZONE_MAP[msd_name][0] try: sql_msd_loc = SQLLocation.objects.get(domain=self.domain, site_code=msd_code) msd_location = Loc.get(sql_msd_loc.location_id) except SQLLocation.DoesNotExist: msd_location = Loc(parent=loc_parent) msd_location.domain = self.domain msd_location.name = msd_name msd_location.location_type = "MSDZONE" msd_location.site_code = MSDZONE_MAP[msd_name][0] msd_location.save() return msd_location
def testDoesntDuplicate(self): location = Location(name="Location", domain=self.domain, location_type="type") location.save() self.assertEqual(location.site_code, 'location') location = Location(name="Location", domain=self.domain, location_type="type") location.save() self.assertEqual(location.site_code, 'location1')
def get_or_create_msd_zone(region): msd_name = _get_msd_name(region.name) msd_code = MSDZONE_MAP[msd_name][0] try: sql_msd_loc = SQLLocation.objects.get(domain=self.domain, site_code=msd_code) msd_location = Loc.get(sql_msd_loc.location_id) except SQLLocation.DoesNotExist: msd_location = Loc(parent=loc_parent) msd_location.domain = self.domain msd_location.name = msd_name msd_location.location_type = 'MSDZONE' msd_location.site_code = MSDZONE_MAP[msd_name][0] msd_location.save() return msd_location
def _create_location_from_supply_point(self, supply_point, location): try: sql_location = SQLLocation.objects.get(domain=self.domain, site_code=supply_point.code) return Loc.get(sql_location.location_id) except SQLLocation.DoesNotExist: new_location = Loc(parent=location) new_location.domain = self.domain new_location.location_type = supply_point.type new_location.name = supply_point.name new_location.site_code = supply_point.code if supply_point.supervised_by: new_location.metadata['supervised_by'] = supply_point.supervised_by new_location.save() sql_loc = new_location.sql_location sql_loc.products = SQLProduct.objects.filter(domain=self.domain, code__in=supply_point.products) sql_loc.save() return new_location
def _create_data(self, domain_name): product = Product(domain=domain_name, name='test-product') product.save() location = Location(domain=domain_name, site_code='testcode', name='test1', location_type='facility') location.save() self.locations[domain_name] = location.sql_location user = CommCareUser.create(domain=domain_name, username='******'.format(domain_name), password='******') FacilityInCharge.objects.create(user_id=user.get_id, location=location.sql_location)
def _create_location_from_supply_point(self, supply_point, location): try: sql_location = SQLLocation.objects.get(domain=self.domain, site_code=supply_point.code) return Loc.get(sql_location.location_id) except SQLLocation.DoesNotExist: parent = location if supply_point.code in TEACHING_HOSPITAL_MAPPING: parent = self._sync_parent(TEACHING_HOSPITAL_MAPPING[supply_point.code]['parent_external_id']) new_location = Loc(parent=parent) new_location.domain = self.domain new_location.location_type = supply_point.type new_location.name = supply_point.name new_location.site_code = supply_point.code new_location.save() sql_loc = new_location.sql_location sql_loc.products = SQLProduct.objects.filter(domain=self.domain, code__in=supply_point.products) sql_loc.save() return new_location
def testDoesntDuplicate(self): location = Location( name="Location", domain=self.domain.name, location_type="type" ) location.save() self.assertEqual(location.site_code, 'location') location = Location( name="Location", domain=self.domain.name, location_type="type" ) location.save() self.assertEqual(location.site_code, 'location1')
def _create_data(self, domain_name, i): product = Product(domain=domain_name, name='test-{}'.format(i)) product.save() location = Location(domain=domain_name, site_code='testcode-{}'.format(i), name='test-{}'.format(i), location_type='facility') location.save() SupplyPointCase.create_from_location(domain_name, location) report = StockReport.objects.create(type='balance', domain=domain_name, form_id='fake', date=datetime.utcnow()) StockTransaction.objects.create( report=report, product_id=product.get_id, sql_product=SQLProduct.objects.get(product_id=product.get_id), section_id='stock', type='stockonhand', case_id=location.linked_supply_point().get_id, stock_on_hand=100)
def setUp(self): self.endpoint = MockEndpoint('http://test-api.com/', 'dummy', 'dummy') self.stock_api_object = MockILSStockDataSynchronization( TEST_DOMAIN, self.endpoint) self.datapath = os.path.join(os.path.dirname(__file__), 'data') initial_bootstrap(TEST_DOMAIN) self.api_object = ILSGatewayAPI(TEST_DOMAIN, self.endpoint) self.api_object.prepare_commtrack_config() config = ILSGatewayConfig() config.domain = TEST_DOMAIN config.enabled = True config.all_stock_data = True config.password = '******' config.username = '******' config.url = 'http://test-api.com/' config.save() l1 = Location(name='Test location 1', external_id='3445', location_type='FACILITY', domain=TEST_DOMAIN) l2 = Location(name='Test location 2', external_id='4407', location_type='FACILITY', domain=TEST_DOMAIN) l1.save() l2.save() SupplyPointCase.create_from_location(TEST_DOMAIN, l1) SupplyPointCase.create_from_location(TEST_DOMAIN, l2) with open(os.path.join(self.datapath, 'sample_products.json')) as f: for product_json in json.loads(f.read()): self.api_object.product_sync(Product(product_json)) StockTransaction.objects.all().delete()
def location_sync(self, ilsgateway_location): def get_or_create_msd_zone(region): msd_name = _get_msd_name(region.name) msd_code = MSDZONE_MAP[msd_name][0] try: sql_msd_loc = SQLLocation.objects.get(domain=self.domain, site_code=msd_code) msd_location = Loc.get(sql_msd_loc.location_id) except SQLLocation.DoesNotExist: msd_location = Loc(parent=loc_parent) msd_location.domain = self.domain msd_location.name = msd_name msd_location.location_type = 'MSDZONE' msd_location.site_code = MSDZONE_MAP[msd_name][0] msd_location.save() return msd_location try: sql_loc = SQLLocation.objects.get(domain=self.domain, external_id=int( ilsgateway_location.id)) location = Loc.get(sql_loc.location_id) except SQLLocation.DoesNotExist: location = None except SQLLocation.MultipleObjectsReturned: return if not location: if ilsgateway_location.id in EXCLUDED_REGIONS: return if ilsgateway_location.parent_id: try: sql_loc_parent = SQLLocation.objects.get( domain=self.domain, external_id=ilsgateway_location.parent_id) loc_parent = sql_loc_parent.couch_location except SQLLocation.DoesNotExist: parent = self.endpoint.get_location( ilsgateway_location.parent_id) loc_parent = self.location_sync(Location(parent)) if not loc_parent: return if ilsgateway_location.type == 'REGION': location = Loc( parent=get_or_create_msd_zone(ilsgateway_location)) else: location = Loc(parent=loc_parent) else: location = Loc() location.lineage = [] location.domain = self.domain location.name = ilsgateway_location.name if ilsgateway_location.groups: location.metadata = {'group': ilsgateway_location.groups[0]} if ilsgateway_location.latitude: location.latitude = float(ilsgateway_location.latitude) if ilsgateway_location.longitude: location.longitude = float(ilsgateway_location.longitude) location.location_type = ilsgateway_location.type location.site_code = ilsgateway_location.code location.external_id = unicode(ilsgateway_location.id) location.save() if ilsgateway_location.type == 'FACILITY' and not SupplyPointCase.get_by_location( location): SupplyPointCase.create_from_location(self.domain, location) location.save() else: location_dict = { 'name': ilsgateway_location.name, 'latitude': float(ilsgateway_location.latitude) if ilsgateway_location.latitude else None, 'longitude': float(ilsgateway_location.longitude) if ilsgateway_location.longitude else None, 'location_type': ilsgateway_location.type, 'site_code': ilsgateway_location.code.lower(), 'external_id': str(ilsgateway_location.id), 'metadata': {} } if ilsgateway_location.groups: location_dict['metadata'][ 'group'] = ilsgateway_location.groups[0] case = SupplyPointCase.get_by_location(location) if apply_updates(location, location_dict): location.save() if case: case.update_from_location(location) else: SupplyPointCase.create_from_location(self.domain, location)
def location_sync(self, ews_location): try: sql_loc = SQLLocation.objects.get( domain=self.domain, external_id=int(ews_location.id) ) location = Loc.get(sql_loc.location_id) except SQLLocation.DoesNotExist: location = None if not location: if ews_location.parent_id: try: loc_parent = SQLLocation.objects.get( external_id=ews_location.parent_id, domain=self.domain ) loc_parent_id = loc_parent.location_id except SQLLocation.DoesNotExist: loc_parent_id = self._sync_parent(ews_location.parent_id) location = Loc(parent=loc_parent_id) else: location = Loc() location.lineage = [] self._set_location_properties(location, ews_location) self._set_up_supply_point(location, ews_location) else: location_dict = {} if location.sql_location.location_type.administrative: location_dict = { 'name': ews_location.name, 'latitude': float(ews_location.latitude) if ews_location.latitude else None, 'longitude': float(ews_location.longitude) if ews_location.longitude else None, } try: SQLLocation.objects.get(domain=self.domain, site_code=ews_location.code.lower()) except SQLLocation.DoesNotExist: location_dict['site_code'] = ews_location.code.lower() else: supply_point_with_stock_data = filter( lambda x: x.last_reported and x.active, ews_location.supply_points ) supply_point = None if supply_point_with_stock_data: supply_point = supply_point_with_stock_data[0] elif ews_location.supply_points: supply_point = ews_location.supply_points[0] if supply_point: location_dict = { 'name': supply_point.name, 'latitude': float(ews_location.latitude) if ews_location.latitude else None, 'longitude': float(ews_location.longitude) if ews_location.longitude else None, 'site_code': supply_point.code, } if location_dict and apply_updates(location, location_dict): location.save() for supply_point in ews_location.supply_points: sp = get_supply_point_case_by_domain_external_id(self.domain, supply_point.id) if sp: sql_location = sp.sql_location if set(sql_location.products.values_list('code', flat=True)) != supply_point.products: sql_location.products = SQLProduct.objects.filter( domain=self.domain, code__in=supply_point.products ) sql_location.save() for in_charge in supply_point.incharges: self._set_in_charges(in_charge, sql_location) return location
def create_test_locations(domain): country = Location(name='national', site_code='ghana', location_type='country', domain=domain) country.save() crms = Location( name='Central Regional Medical Store', site_code='crms', location_type='country', domain=domain ) crms.save() test_region = Location( name='Test Region', site_code='testregion', location_type='region', domain=domain, parent=country ) test_region.save() test_region2 = Location( name='Test Region2', site_code='testregion2', location_type='region', domain=domain, parent=country ) test_region2.save() rsp = Location( name='Test Regional Medical Store', site_code='rsp', location_type='Regional Medical Store', domain=domain, parent=test_region2 ) rsp.save() assign_products_to_locations(rsp, ["ad", "al", "mc", "ng", "mg"]) rsp2 = Location( name='Test Regional Medical Store', site_code='rsp2', location_type='Regional Medical Store', domain=domain, parent=test_region2 ) rsp2.save() assign_products_to_locations(rsp2, ["ad", "al"]) test_district = Location( name='Test District', site_code='testdistrict', location_type='district', domain=domain, parent=test_region ) test_district.save() test_facility = Location( name='Active Test hospital', site_code='tsactive', location_type='Hospital', domain=domain, parent=test_district ) test_facility.save() assign_products_to_locations(test_facility, ["ad", "al"])
def _create_data(self, domain_name, i): product = Product(domain=domain_name, name='test-{}'.format(i)) product.save() location = Location( domain=domain_name, site_code='testcode-{}'.format(i), name='test-{}'.format(i), location_type='facility' ) location.save() report = StockReport.objects.create( type='balance', domain=domain_name, form_id='fake', date=datetime.utcnow(), server_date=datetime.utcnow(), ) StockTransaction.objects.create( report=report, product_id=product.get_id, sql_product=SQLProduct.objects.get(product_id=product.get_id), section_id='stock', type='stockonhand', case_id=location.linked_supply_point().get_id, stock_on_hand=100 ) SMS.objects.create(domain=domain_name) Call.objects.create(domain=domain_name) SQLLastReadMessage.objects.create(domain=domain_name) ExpectedCallback.objects.create(domain=domain_name) PhoneNumber.objects.create(domain=domain_name) event = MessagingEvent.objects.create( domain=domain_name, date=datetime.utcnow(), source=MessagingEvent.SOURCE_REMINDER, content_type=MessagingEvent.CONTENT_SMS, status=MessagingEvent.STATUS_COMPLETED ) MessagingSubEvent.objects.create( parent=event, date=datetime.utcnow(), recipient_type=MessagingEvent.RECIPIENT_CASE, content_type=MessagingEvent.CONTENT_SMS, status=MessagingEvent.STATUS_COMPLETED ) SelfRegistrationInvitation.objects.create( domain=domain_name, phone_number='999123', token=uuid.uuid4().hex, expiration_date=datetime.utcnow().date(), created_date=datetime.utcnow() ) backend = SQLMobileBackend.objects.create(domain=domain_name, is_global=False) SQLMobileBackendMapping.objects.create( domain=domain_name, backend_type=SQLMobileBackend.SMS, prefix=str(i), backend=backend ) MobileBackendInvitation.objects.create(domain=domain_name, backend=backend)
def make_loc(code, name=None, domain=TEST_DOMAIN, type=TEST_LOCATION_TYPE, parent=None): name = name or code loc = Location(site_code=code, name=name, domain=domain, type=type, parent=parent) loc.save() return loc
def _create_data(self, domain_name): product = Product(domain=domain_name, name='test-product') product.save() location = Location( domain=domain_name, site_code='testcode', name='test1', location_type='facility' ) location.save() self.locations[domain_name] = location.get_id DeliveryGroupReport.objects.create( location_id=location.get_id, quantity=1, message='test', delivery_group='A' ) SupplyPointWarehouseRecord.objects.create( supply_point=location.get_id, create_date=datetime.utcnow() ) Alert.objects.create( text='test', expires=datetime.utcnow(), date=datetime.utcnow(), location_id=location.get_id ) organization_summary = OrganizationSummary.objects.create( date=datetime.utcnow(), location_id=location.get_id ) GroupSummary.objects.create( org_summary=organization_summary ) ProductAvailabilityData.objects.create( product=product.get_id, date=datetime.utcnow(), location_id=location.get_id ) SupplyPointStatus.objects.create( location_id=location.get_id, status_type='del_fac', status_value='received' ) HistoricalLocationGroup.objects.create( location_id=location.sql_location, group='A', date=datetime.utcnow().date() ) ReportRun.objects.create( domain=domain_name, start=datetime.utcnow(), end=datetime.utcnow(), start_run=datetime.utcnow() ) ILSNotes.objects.create( location=location.sql_location, domain=domain_name, user_name='test', date=datetime.utcnow(), text='test' ) SupervisionDocument.objects.create( domain=domain_name, document='test', data_type='test', name='test' )
def location_sync(self, ews_location): try: sql_loc = SQLLocation.objects.get(domain=self.domain, external_id=int(ews_location.id)) location = Loc.get(sql_loc.location_id) except SQLLocation.DoesNotExist: location = None if not location: if ews_location.parent_id: try: loc_parent = SQLLocation.objects.get( external_id=ews_location.parent_id, domain=self.domain) loc_parent_id = loc_parent.location_id except SQLLocation.DoesNotExist: loc_parent_id = self._sync_parent(ews_location.parent_id) location = Loc(parent=loc_parent_id) else: location = Loc() location.lineage = [] self._set_location_properties(location, ews_location) self._set_up_supply_point(location, ews_location) else: location_dict = {} if location.sql_location.location_type.administrative: location_dict = { 'name': ews_location.name, 'latitude': float(ews_location.latitude) if ews_location.latitude else None, 'longitude': float(ews_location.longitude) if ews_location.longitude else None, } try: SQLLocation.objects.get( domain=self.domain, site_code=ews_location.code.lower()) except SQLLocation.DoesNotExist: location_dict['site_code'] = ews_location.code.lower() else: supply_point_with_stock_data = filter( lambda x: x.last_reported and x.active, ews_location.supply_points) supply_point = None if supply_point_with_stock_data: supply_point = supply_point_with_stock_data[0] elif ews_location.supply_points: supply_point = ews_location.supply_points[0] if supply_point: location_dict = { 'name': supply_point.name, 'latitude': float(ews_location.latitude) if ews_location.latitude else None, 'longitude': float(ews_location.longitude) if ews_location.longitude else None, 'site_code': supply_point.code, } if location_dict and apply_updates(location, location_dict): location.save() for supply_point in ews_location.supply_points: sp = get_supply_point_case_by_domain_external_id( self.domain, supply_point.id) if sp: sql_location = sp.sql_location if set(sql_location.products.values_list( 'code', flat=True)) != supply_point.products: sql_location.products = SQLProduct.objects.filter( domain=self.domain, code__in=supply_point.products) sql_location.save() for in_charge in supply_point.incharges: self._set_in_charges(in_charge, sql_location) return location
def location_sync(self, ilsgateway_location): def get_or_create_msd_zone(region): msd_name = _get_msd_name(region.name) msd_code = MSDZONE_MAP[msd_name][0] try: sql_msd_loc = SQLLocation.objects.get( domain=self.domain, site_code=msd_code ) msd_location = Loc.get(sql_msd_loc.location_id) except SQLLocation.DoesNotExist: msd_location = Loc(parent=loc_parent) msd_location.domain = self.domain msd_location.name = msd_name msd_location.location_type = 'MSDZONE' msd_location.site_code = MSDZONE_MAP[msd_name][0] msd_location.save() return msd_location try: sql_loc = SQLLocation.objects.get( domain=self.domain, external_id=int(ilsgateway_location.id) ) location = Loc.get(sql_loc.location_id) except SQLLocation.DoesNotExist: location = None except SQLLocation.MultipleObjectsReturned: return if not location: if ilsgateway_location.parent_id: try: sql_loc_parent = SQLLocation.objects.get( domain=self.domain, external_id=ilsgateway_location.parent_id ) loc_parent = sql_loc_parent.couch_location except SQLLocation.DoesNotExist: parent = self.endpoint.get_location(ilsgateway_location.parent_id) loc_parent = self.location_sync(Location(parent)) if ilsgateway_location.type == 'REGION': location = Loc(parent=get_or_create_msd_zone(ilsgateway_location)) else: location = Loc(parent=loc_parent) else: location = Loc() location.lineage = [] location.domain = self.domain location.name = ilsgateway_location.name if ilsgateway_location.groups: location.metadata = {'group': ilsgateway_location.groups[0]} if ilsgateway_location.latitude: location.latitude = float(ilsgateway_location.latitude) if ilsgateway_location.longitude: location.longitude = float(ilsgateway_location.longitude) location.location_type = ilsgateway_location.type location.site_code = ilsgateway_location.code location.external_id = unicode(ilsgateway_location.id) location.save() if ilsgateway_location.type == 'FACILITY' and not SupplyPointCase.get_by_location(location): SupplyPointCase.create_from_location(self.domain, location) location.save() else: location_dict = { 'name': ilsgateway_location.name, 'latitude': float(ilsgateway_location.latitude) if ilsgateway_location.latitude else None, 'longitude': float(ilsgateway_location.longitude) if ilsgateway_location.longitude else None, 'location_type': ilsgateway_location.type, 'site_code': ilsgateway_location.code.lower(), 'external_id': str(ilsgateway_location.id), 'metadata': {} } if ilsgateway_location.groups: location_dict['metadata']['group'] = ilsgateway_location.groups[0] case = SupplyPointCase.get_by_location(location) if apply_updates(location, location_dict): location.save() if case: case.update_from_location(location) else: SupplyPointCase.create_from_location(self.domain, location) return location