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() interface = SupplyInterface(self.domain) if ilsgateway_location.type == 'FACILITY' and not interface.get_by_location(location): interface.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 = SupplyInterface(self.domain).get_by_location(location) if apply_updates(location, location_dict): location.save() if case: case.update_from_location(location) else: SupplyInterface.create_from_location(self.domain, location) return location