def __init__(self, *args, **kwargs): from corehq.apps.locations.util import get_lineage_from_location, get_lineage_from_location_id if 'parent' in kwargs: parent = kwargs['parent'] if parent: if isinstance(parent, Document): lineage = get_lineage_from_location(parent) else: # 'parent' is a doc id lineage = get_lineage_from_location_id(parent) else: lineage = [] kwargs['lineage'] = lineage del kwargs['parent'] location_type = kwargs.pop('location_type', None) super(Document, self).__init__(*args, **kwargs) if location_type: self.location_type = location_type
def __init__(self, *args, **kwargs): from corehq.apps.locations.util import get_lineage_from_location, get_lineage_from_location_id if 'parent' in kwargs: parent = kwargs['parent'] if parent: if isinstance(parent, Document): lineage = get_lineage_from_location(parent) else: # 'parent' is a doc id lineage = get_lineage_from_location_id(parent) else: lineage = [] kwargs['lineage'] = lineage del kwargs['parent'] location_type = kwargs.pop('location_type', None) super(Document, self).__init__(*args, **kwargs) if location_type: self.set_location_type(location_type)
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: new_parent = self.endpoint.get_location(ilsgateway_location.parent_id) loc_parent = self.location_sync(Location(new_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': if not interface.get_by_location(location): interface.create_from_location(self.domain, location) location.save() else: sql_location = location.sql_location if not sql_location.supply_point_id: 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: update_supply_point_from_location(case, location) else: SupplyInterface.create_from_location(self.domain, location) location_parent = location.parent if ilsgateway_location.type == 'FACILITY' and ilsgateway_location.parent_id and location_parent \ and location_parent.external_id != str(ilsgateway_location.parent_id): new_parent = self.endpoint.get_location(ilsgateway_location.parent_id) new_parent = self.location_sync(Location(new_parent)) location.lineage = get_lineage_from_location_id(new_parent.get_id) location.save() location.previous_parents = [location_parent.get_id] location_edited_receiver(None, location, moved=True) return location