def testParseLocationJSON(self): with open(os.path.join(self.datapath, 'sample_location.json')) as f: location = Location.from_json(json.loads(f.read())) self.assertEqual(location.id, 1) self.assertEqual(location.name, "MOHSW") self.assertEqual(location.type, "MOHSW") self.assertEqual(location.parent, None) self.assertEqual(location.latitude, -10.6676087700) self.assertEqual(location.longitude, 39.1621900200) self.assertEqual(location.code, "MOHSW-MOHSW")
def test_create_location(self): with open(os.path.join(self.datapath, 'sample_location.json')) as f: location = Loc.from_json(json.loads(f.read())) ilsgateway_location = sync_ilsgateway_location(TEST_DOMAIN, None, location) self.assertEqual(ilsgateway_location.name, location.name) self.assertEqual(ilsgateway_location.location_type, location.location_type) self.assertEqual(ilsgateway_location.longitude, location.longitude) self.assertEqual(ilsgateway_location.latitude, location.latitude) self.assertEqual(ilsgateway_location.parent, location.parent)
def test_create_location(self): with open(os.path.join(self.datapath, 'sample_location.json')) as f: location = Loc.from_json(json.loads(f.read())) ilsgateway_location = sync_ilsgateway_location(TEST_DOMAIN, None, location) self.assertEqual(ilsgateway_location.name, location.name) self.assertEqual(ilsgateway_location.location_type, location.type) self.assertEqual(ilsgateway_location.longitude, location.longitude) self.assertEqual(ilsgateway_location.latitude, location.latitude) self.assertEqual(ilsgateway_location.parent, location.parent)
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 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_locations(domain, endpoint): for facility in FACILITIES: location = endpoint.get_location(facility, params=dict(with_historical_groups=1)) sync_ilsgateway_location(domain, endpoint, Location.from_json(location))