def test_duplicate_locations(self): parent = LocationType( domain="test-domain", name="parent", code="parent", ) child = LocationType(domain="test-domain", name="child", code="child", parent_type=parent) location1 = SQLLocation(id="58302461", location_id="1", name="Some Parent Location", location_type=parent) location2 = SQLLocation(id="39825", location_id="2", name="Some Child Location", location_type=child, parent=location1) set_locations = LocationSet([location1, location2]) self.assertEqual(len(set_locations.by_parent['1']), 1) self.assertEqual(len(set_locations.by_parent['2']), 0) self.assertEqual(len(set_locations.by_id), 2) set_locations.add_location(location1) set_locations.add_location(location2) self.assertEqual(len(set_locations.by_id), 2) self.assertEqual(len(set_locations.by_parent['1']), 1) self.assertEqual(len(set_locations.by_parent['2']), 0)
def setUpClass(cls): super(LocationHierarchyTest, cls).setUpClass() domain = "a-song-of-ice-and-fire" domain_obj = create_domain(domain) continent_location_type = LocationType( domain=domain, name="continent", code="continent", ) continent_location_type.save() kingdom_location_type = LocationType( domain=domain, name="kingdom", code="kingdom", parent_type=continent_location_type, ) kingdom_location_type.save() city_location_type = LocationType( domain=domain, name="city", code="city", parent_type=kingdom_location_type, ) city_location_type.save() continent = SQLLocation( domain=domain, name="Westeros", location_type=continent_location_type, site_code="westeros", ) continent.save() kingdom = SQLLocation( domain=domain, name="The North", location_type=kingdom_location_type, parent=continent, site_code="the_north", ) kingdom.save() city = SQLLocation( domain=domain, name="Winterfell", location_type=city_location_type, parent=kingdom, site_code="winterfell", ) city.save() cls.domain_obj = domain_obj cls.domain = domain cls.continent = continent cls.kingdom = kingdom cls.city = city
def setUpClass(cls): super(TestLocationTypeExpression, cls).setUpClass() cls.domain_name = "test-domain" cls.domain_obj = create_domain(cls.domain_name) cls.location_type = LocationType( domain=cls.domain_name, name="state", code="state", ) cls.location_type.save() cls.location = SQLLocation(domain="test-domain", name="Braavos", location_type=cls.location_type) cls.location.save() cls.unique_id = cls.location.location_id cls.spec = { "type": "location_type_name", "location_id_expression": { "type": "property_name", "property_name": "_id" } } cls.expression = ExpressionFactory.from_spec(cls.spec)
def copy_locations(self, types_only=False): from corehq.apps.locations.models import LocationType, SQLLocation from corehq.apps.locations.views import LocationFieldsView self._copy_custom_data(LocationFieldsView.field_type) location_types = LocationType.objects.by_domain(self.existing_domain) location_types_map = {} for location_type in location_types: if location_type.parent_type_id: location_type.parent_type_id = location_types_map[location_type.parent_type_id] old_id, new_id = self.save_sql_copy(location_type, self.new_domain) location_types_map[old_id] = new_id if not types_only: # MPTT sorts this queryset so we can just save in the same order new_loc_pks_by_code = {} for loc in SQLLocation.active_objects.filter(domain=self.existing_domain): # start with a new location so we don't inadvertently copy over a bunch of foreign keys new_loc = SQLLocation() for field in ["name", "site_code", "external_id", "metadata", "is_archived", "latitude", "longitude"]: setattr(new_loc, field, getattr(loc, field, None)) new_loc.domain = self.new_domain new_loc.parent_id = new_loc_pks_by_code[loc.parent.site_code] if loc.parent_id else None new_loc.location_type_id = location_types_map[loc.location_type_id] _, new_pk = self.save_sql_copy(new_loc, self.new_domain) new_loc_pks_by_code[new_loc.site_code] = new_pk existing_fixture_config = LocationFixtureConfiguration.for_domain(self.existing_domain) self.save_sql_copy(existing_fixture_config, self.new_domain)
def lookup_old_collection_data(self, old_collection, locs_by_code): """Lookup whether the location already exists or is new""" if self.is_new: self.db_object = SQLLocation(domain=old_collection.domain_name) else: self.db_object = copy.copy( old_collection.locations_by_id[self.location_id]) if self.db_object.parent_id is not None: parent = old_collection.locations_by_pk[self.db_object.parent_id] old_parent_code = parent.site_code else: old_parent_code = ROOT_LOCATION_TYPE self.needs_save = self._needs_save(old_parent_code) self.moved_to_root = (not self.is_new and self.parent_code == ROOT_LOCATION_TYPE and old_parent_code != ROOT_LOCATION_TYPE) if self.parent_code and self.parent_code != ROOT_LOCATION_TYPE: try: self._new_parent_stub = locs_by_code[self.parent_code] except KeyError: # TODO require all referenced locations to be in stubs and then # remove this exception handler and fake stub nonsense. # Breaks test: TestBulkManagement.test_large_upload class fake_stub: db_object = old_collection.locations_by_site_code[ self.parent_code] self._new_parent_stub = fake_stub for attr in self.meta_data_attrs: setattr(self.db_object, attr, getattr(self, attr, None)) self.db_object.metadata = self.custom_location_data
def make_new_location_form(self, location_name, location_type, parent, location_fields=None, user_fields=None): bound_data = self._get_form_bound_data(location_name, location_type, location_fields, user_fields) return ENikshayLocationFormSet( location=SQLLocation(domain=self.domain, parent=parent), bound_data=bound_data, request_user=self.web_user, is_new=True, )
def setUpClass(cls): super(TestLocationParentIdExpression, cls).setUpClass() cls.domain = 'test-loc-parent-id' cls.domain_obj = create_domain(cls.domain) cls.location_type = LocationType( domain=cls.domain, name="state", code="state", ) cls.location_type.save() cls.parent = SQLLocation( domain=cls.domain, name="Westeros", location_type=cls.location_type, site_code="westeros", ) cls.parent.save() cls.child = SQLLocation( domain=cls.domain, name="The North", location_type=cls.location_type, parent=cls.parent, site_code="the_north", ) cls.child.save() cls.grandchild = SQLLocation( domain=cls.domain, name="Winterfell", location_type=cls.location_type, parent=cls.child, site_code="winterfell", ) cls.grandchild.save() cls.evaluation_context = EvaluationContext({"domain": cls.domain}) cls.expression_spec = { "type": "location_parent_id", "location_id_expression": { "type": "property_name", "property_name": "location_id", } } cls.expression = ExpressionFactory.from_spec(cls.expression_spec)
def _make_loc(name, site_code, location_type, parent_code, location_id, do_delete, external_id, latitude, longitude, custom_data, index, location_data_model, parent=None): _type = lt_by_code.get(location_type) loc = SQLLocation( site_code=site_code, name=name, domain=self.domain.name, location_type=_type, parent=parent, ) loc.save() return loc
def make_new_location_form(self, name, parent, nikshay_code): return LocationForm( location=SQLLocation(domain=self.domain, parent=parent), bound_data={ 'name': name, 'data-field-nikshay_code': nikshay_code }, user=self.web_user, is_new=True, )
def make_new_location_form(self, name, location_type, parent, nikshay_code): return ENikshayLocationFormSet( location=SQLLocation(domain=self.domain, parent=parent), bound_data={ 'name': name, 'location_type': self.location_types[location_type], 'data-field-nikshay_code': nikshay_code }, request_user=self.web_user, is_new=True, )
def test_set_primary_location(self): location_id = uuid.uuid4().hex location = SQLLocation(name="Location Name", location_id=location_id) self._test_change_messages( UserChangeMessage.primary_location_info, [location], { "location": { "set_primary_location": { "id": location_id, "name": "Location Name" } } }, f"Primary location: Location Name[{location_id}]")
def test_set_assigned_locations(self): location1_id = uuid.uuid4().hex location1 = SQLLocation(name="Location 1", location_id=location1_id) location2_id = uuid.uuid4().hex location2 = SQLLocation(name="Location 2", location_id=location2_id) self._test_change_messages( UserChangeMessage.assigned_locations_info, [[location1, location2]], { "assigned_locations": { "set_assigned_locations": { "locations": [{ 'id': location1_id, 'name': "Location 1" }, { 'id': location2_id, 'name': "Location 2" }] } } }, f"Assigned locations: ['Location 1[{location1_id}]', 'Location 2[{location2_id}]']" )
def lookup_old_collection_data(self, old_collection): # Lookup whether the location already exists in old_collection or is new. # Depending on that set attributes like 'is_new', 'needs_save', 'db_obect' self.autoset_location_id_or_site_code(old_collection) if self.is_new: self.db_object = SQLLocation(domain=old_collection.domain_name) else: self.db_object = copy.copy(old_collection.locations_by_id[self.location_id]) self.needs_save = self._needs_save() self.moved_to_root = self._moved_to_root() for attr in self.meta_data_attrs: setattr(self.db_object, attr, getattr(self, attr, None)) self.db_object.metadata = self.custom_location_data
def test_metadata(self): location = SQLLocation( id="854208", location_id="unique-id", domain="test-domain", name="Braavos", location_type=self.location_type, metadata={ 'best_swordsman': "Sylvio Forel", 'in_westeros': "false" }, ) location_db = _location_footprint([location]) fixture = _location_to_fixture(location_db, location, self.location_type) location_data = {e.tag: e.text for e in fixture.find('location_data')} self.assertEquals(location_data, location.metadata)
def test_should_sync_locations_change_location_type(self): """ When location_type gets changed, we should resync locations """ yesterday = datetime.today() - timedelta(1) day_before_yesterday = yesterday - timedelta(1) LocationType.objects.all().update(last_modified=day_before_yesterday ) # Force update because of auto_now self.location_type = LocationType.objects.last() location = SQLLocation( location_id="unique-id", domain="test-domain", name="Meereen", location_type=self.location_type, metadata={ 'queen': "Daenerys Targaryen", 'rebels': "Sons of the Harpy" }, ) location.save() SQLLocation.objects.filter(pk=1).update( last_modified=day_before_yesterday) location = SQLLocation.objects.last() location_db = _location_footprint([location]) self.assertFalse( should_sync_locations(SyncLog(date=yesterday), location_db, self.user)) self.location_type.shares_cases = True self.location_type.save() location = SQLLocation.objects.last() location_db = _location_footprint([location]) self.assertTrue( should_sync_locations(SyncLog(date=yesterday), location_db, self.user))
def setUpClass(cls): cls.domain = Domain(name=DOMAIN) cls.domain.save() cls.country = LocationType(domain=DOMAIN, name='country') cls.country.save() cls.state = LocationType( domain=DOMAIN, name='state', parent_type=cls.country, ) cls.state.save() cls.city = LocationType( domain=DOMAIN, name='city', parent_type=cls.state, shares_cases=True, ) cls.city.save() cls.usa = SQLLocation( domain=DOMAIN, name='The United States of America', site_code='usa', location_type=cls.country, ) cls.usa.save() cls.massachusetts = SQLLocation( domain=DOMAIN, name='Massachusetts', site_code='massachusetts', location_type=cls.state, parent=cls.usa, ) cls.massachusetts.save() cls.new_york = SQLLocation( domain=DOMAIN, name='New York', site_code='new_york', location_type=cls.state, parent=cls.usa, ) cls.new_york.save() cls.cambridge = SQLLocation( domain=DOMAIN, name='Cambridge', site_code='cambridge', location_type=cls.city, parent=cls.massachusetts, ) cls.cambridge.save() cls.somerville = SQLLocation( domain=DOMAIN, name='Somerville', site_code='somerville', location_type=cls.city, parent=cls.massachusetts, ) cls.somerville.save() cls.nyc = SQLLocation( domain=DOMAIN, name='New York City', site_code='nyc', location_type=cls.city, parent=cls.new_york, ) cls.nyc.save() cls.drew = CommCareUser( domain=DOMAIN, username='******', location_id=cls.nyc.location_id, assigned_location_ids=[cls.nyc.location_id], ) cls.jon = CommCareUser( domain=DOMAIN, username='******', location_id=cls.cambridge.location_id, assigned_location_ids=[cls.cambridge.location_id], ) cls.nate = CommCareUser( domain=DOMAIN, username='******', location_id=cls.somerville.location_id, assigned_location_ids=[cls.somerville.location_id], ) cls.sheel = CommCareUser( domain=DOMAIN, username='******', location_id=cls.somerville.location_id, assigned_location_ids=[cls.somerville.location_id], last_login=datetime.datetime.now(), date_joined=datetime.datetime.now(), ) cls.sheel.save()