Exemplo n.º 1
0
    def test_parent_change(self):
        with open(os.path.join(self.datapath, 'sample_locations.json')) as f:
            location = Loc(**json.loads(f.read())[0])

        self.api_object.location_sync(location)
        location.parent_id = 2626
        ilsgateway_location = self.api_object.location_sync(location)
        self.assertEqual(ilsgateway_location.parent.external_id, '2626')
Exemplo n.º 2
0
 def test_parse_location_json(self):
     with open(os.path.join(self.datapath, 'sample_locations.json')) as f:
         location = Location(json.loads(f.read())[1])
     self.assertEqual(location.id, 1)
     self.assertEqual(location.name, "MOHSW")
     self.assertEqual(location.type, "MOHSW")
     self.assertEqual(location.parent_id, None)
     self.assertEqual(location.latitude, "-10.6676087700")
     self.assertEqual(location.longitude, "39.1621900200")
     self.assertEqual(location.code, "MOHSW-MOHSW")
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
 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")
Exemplo n.º 5
0
 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.type)
        self.assertEqual(ilsgateway_location.longitude, location.longitude)
        self.assertEqual(ilsgateway_location.latitude, location.latitude)
        self.assertEqual(ilsgateway_location.parent, location.parent)
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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)
Exemplo n.º 9
0
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))
Exemplo n.º 10
0
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