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 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 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
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
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) return 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 sync_ilsgateway_location(domain, endpoint, ilsgateway_location, fetch_groups=False): try: sql_loc = SQLLocation.objects.get( domain=domain, external_id=int(ilsgateway_location.id) ) location = Location.get(sql_loc.location_id) except SQLLocation.DoesNotExist: location = None except SQLLocation.MultipleObjectsReturned: return if not location: if ilsgateway_location.parent_id: loc_parent = SupplyPointCase.view('hqcase/by_domain_external_id', key=[domain, str(ilsgateway_location.parent_id)], reduce=False, include_docs=True).first() if not loc_parent: parent = endpoint.get_location(ilsgateway_location.parent_id) loc_parent = sync_ilsgateway_location(domain, endpoint, Loc(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, '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']['groups'] = ilsgateway_location.groups 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) if ilsgateway_location.historical_groups: historical_groups = ilsgateway_location.historical_groups elif fetch_groups: location_object = endpoint.get_location( ilsgateway_location.id, params=dict(with_historical_groups=1) ) historical_groups = Loc(**location_object).historical_groups else: historical_groups = {} for date, groups in historical_groups.iteritems(): for group in groups: HistoricalLocationGroup.objects.get_or_create(date=date, group=group, location_id=location.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.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, ilsgateway_location, fetch_groups=False): 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)) 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) if ilsgateway_location.historical_groups: historical_groups = ilsgateway_location.historical_groups else: counter = 0 historical_groups = {} while counter != 5: try: # todo: we may be able to avoid this call by passing the groups in as part of the original # location dict, though that may introduce slowness/timeouts location_object = self.endpoint.get_location( ilsgateway_location.id, params=dict(with_historical_groups=1) ) historical_groups = Location(**location_object).historical_groups break except ConnectionError as e: logging.error(e) counter += 1 for date, groups in historical_groups.iteritems(): for group in groups: HistoricalLocationGroup.objects.get_or_create(date=date, group=group, location_id=location.sql_location) return location