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 save_types(type_stubs, excel_importer=None): """ Given a list of LocationTypeStub objects, saves them to SQL as LocationType objects :param type_stubs: (list) list of LocationType objects with meta-data attributes and `needs_save`, 'is_new', 'db_object' set correctly :param excel_importer: Used for providing progress feedback. Disabled on None :returns: (dict) a dict of {object.code: object for all type objects} """ # # This proceeds in 3 steps # 1. Lookup all to be deleted types and 'bulk_delete' them # 2. Lookup all new types and 'bulk_create' the SQL objects, but don't set ForeignKey attrs like # 'parent', 'expand_from', 'expand_to' yet # 3. Lookup all to be updated types. Set foreign key attrs on these and new objects, and # 'bulk_update' the objects # step 1 to_be_deleted_types = [lt.db_object for lt in type_stubs if lt.do_delete] LocationType.bulk_delete(to_be_deleted_types) if excel_importer: excel_importer.add_progress(len(to_be_deleted_types)) # step 2 new_type_objects = LocationType.bulk_create( [lt.db_object for lt in type_stubs if lt.is_new]) if excel_importer: excel_importer.add_progress(len(new_type_objects)) # step 3 type_objects_by_code = {lt.code: lt for lt in new_type_objects} type_objects_by_code.update({ROOT_LOCATION_TYPE: None}) type_objects_by_code.update({ lt.code: lt.db_object for lt in type_stubs if not lt.is_new and not lt.do_delete }) to_bulk_update = [] for lt in type_stubs: if (lt.needs_save or lt.is_new) and not lt.do_delete: # lookup foreign key attributes from stub and set them on objects type_object = type_objects_by_code[lt.code] type_object.parent_type = type_objects_by_code[lt.parent_code] if lt.expand_from: type_object.expand_from = type_objects_by_code[lt.expand_from] if lt.expand_to: type_object.expand_to = type_objects_by_code[lt.expand_to] to_bulk_update.append(type_object) LocationType.bulk_update(to_bulk_update) if excel_importer: excel_importer.add_progress(len(to_bulk_update)) all_objs_by_code = {lt.code: lt for lt in to_bulk_update} all_objs_by_code.update( {lt.code: lt.db_object for lt in type_stubs if not lt.needs_save}) return all_objs_by_code
def setUp(self): self.city = LocationType( domain='test_domain', name='City', code='city', ) self.state = LocationType( domain='test_domain', name='State', code='state', )
def test_location(self): loc_type = LocationType(domain=self.domain, name='type') loc_type.save() self.addCleanup(loc_type.delete) location = make_location(domain=self.domain, site_code='doc_info_test', name='doc_info_test', location_type='type') location.save() self.addCleanup(location.delete) self._test_doc(location.location_id, "Location")
def setUpClass(cls): super(LocationTypesViewTest, cls).setUpClass() cls.domain = "test-domain" cls.project = create_domain(cls.domain) cls.couch_user = WebUser.create(cls.domain, "test", "foobar", None, None) cls.couch_user.add_domain_membership(cls.domain, is_admin=True) cls.couch_user.set_role(cls.domain, "admin") cls.couch_user.save() cls.loc_type1 = LocationType(domain=cls.domain, name='type1', code='code1') cls.loc_type1.save() cls.loc_type2 = LocationType(domain=cls.domain, name='type2', code='code2') cls.loc_type2.save()
def save_types(type_stubs): # Given a list of LocationTypeStub objects, saves them to SQL as LocationType objects # # args: # type_stubs (list): list of LocationType objects with meta-data attributes and # `needs_save`, 'is_new', 'db_object' set correctly # # returns: # dict: a dict of {object.code: object for all type objects} # # This proceeds in 3 steps # 1. Lookup all to be deleted types and 'bulk_delete' them # 2. Lookup all new types and 'bulk_create' the SQL objects, but don't set ForeignKey attrs like # 'parent', 'expand_from', 'expand_to' yet # 3. Lookup all to be updated types. Set foreign key attrs on these and new objects, and # 'bulk_update' the objects # step 1 to_be_deleted_types = [lt.db_object for lt in type_stubs if lt.do_delete] LocationType.bulk_delete(to_be_deleted_types) # step 2 new_type_objects = LocationType.bulk_create([lt.db_object for lt in type_stubs if lt.is_new]) # step 3 type_objects_by_code = {lt.code: lt for lt in new_type_objects} type_objects_by_code.update({ROOT_LOCATION_TYPE: None}) type_objects_by_code.update({ lt.code: lt.db_object for lt in type_stubs if not lt.is_new and not lt.do_delete }) to_bulk_update = [] for lt in type_stubs: if (lt.needs_save or lt.is_new) and not lt.do_delete: # lookup foreign key attributes from stub and set them on objects type_object = type_objects_by_code[lt.code] type_object.parent_type = type_objects_by_code[lt.parent_code] if lt.expand_from: type_object.expand_from = type_objects_by_code[lt.expand_from] if lt.expand_to: type_object.expand_to = type_objects_by_code[lt.expand_to] to_bulk_update.append(type_object) LocationType.bulk_update(to_bulk_update) all_objs_by_code = {lt.code: lt for lt in to_bulk_update} all_objs_by_code.update({ lt.code: lt.db_object for lt in type_stubs if not lt.needs_save }) return all_objs_by_code
def save_types(type_stubs): # Given a list of LocationTypeStub objects, saves them to SQL as LocationType objects # # args: # type_stubs (list): list of LocationType objects with meta-data attributes and # `needs_save`, 'is_new', 'db_object' set correctly # # returns: # dict: a dict of {object.code: object for all type objects} # # This proceeds in 3 steps # 1. Lookup all to be deleted types and 'bulk_delete' them # 2. Lookup all new types and 'bulk_create' the SQL objects, but don't set ForeignKey attrs like # 'parent', 'expand_from', 'expand_to' yet # 3. Lookup all to be updated types. Set foreign key attrs on these and new objects, and # 'bulk_update' the objects # step 1 to_be_deleted_types = [lt.db_object for lt in type_stubs if lt.do_delete] LocationType.bulk_delete(to_be_deleted_types) # step 2 new_type_objects = LocationType.bulk_create([lt.db_object for lt in type_stubs if lt.is_new]) # step 3 type_objects_by_code = {lt.code: lt for lt in new_type_objects} type_objects_by_code.update({ROOT_LOCATION_TYPE: None}) type_objects_by_code.update({ lt.code: lt.db_object for lt in type_stubs if not lt.is_new and not lt.do_delete }) to_bulk_update = [] for lt in type_stubs: if (lt.needs_save or lt.is_new) and not lt.do_delete: # lookup foreign key attributes from stub and set them on objects type_object = type_objects_by_code[lt.code] type_object.parent_type = type_objects_by_code[lt.parent_code] if lt.expand_from: type_object.expand_from = type_objects_by_code[lt.expand_from] if lt.expand_to: type_object.expand_to = type_objects_by_code[lt.expand_to] to_bulk_update.append(type_object) LocationType.bulk_update(to_bulk_update) all_objs_by_code = {lt.code: lt for lt in to_bulk_update} all_objs_by_code.update({ lt.code: lt.db_object for lt in type_stubs if not lt.needs_save }) return all_objs_by_code
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().setUpClass() cls.domain = 'test' cls.domain_obj = create_domain(cls.domain) set_toggle(FILTERED_BULK_USER_DOWNLOAD.slug, cls.domain, True, namespace=NAMESPACE_DOMAIN) location_type = LocationType(domain=cls.domain, name='phony') location_type.save() cls.some_location = make_loc('1', 'some_location', type=location_type, domain=cls.domain_obj.name) cls.admin_user = WebUser.create( cls.domain_obj.name, '*****@*****.**', 'badpassword', None, None, email='*****@*****.**', first_name='Edith', last_name='Wharton', is_admin=True, ) cls.admin_user_with_location = WebUser.create( cls.domain_obj.name, '*****@*****.**', 'badpassword', None, None, email='*****@*****.**', first_name='Edith', last_name='Wharton', is_admin=True, ) cls.admin_user_with_location.set_location(cls.domain, cls.some_location) populate_user_index([ cls.admin_user_with_location, cls.admin_user, ])
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 setUpClass(cls): cls._delete_everything() cls.domain = "Erebor" cls.username = "******" cls.location_type = LocationType( domain=cls.domain, name="state", code="state", ) password = "******" cls.user = CommCareUser.create(cls.domain, cls.username, password) cls.user.save() cls.location_type.save()
def lookup_old_collection_data(self, old_collection): # Lookup whether the type already exists in old_collection or is new. Depending on that # set attributes like 'is_new', 'needs_save', 'db_obect' self.is_new = self._is_new(old_collection) if self.is_new: self.db_object = LocationType(domain=old_collection.domain_name) else: self.db_object = copy.copy(old_collection.types_by_code[self.code]) self.needs_save = self._needs_save() for attr in self.meta_data_attrs: setattr(self.db_object, attr, getattr(self, attr, None))
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 setUpClass(cls): super(SiteCodeTest, cls).setUpClass() cls.project = bootstrap_domain(cls.domain) LocationType(domain=cls.domain, name='type').save()
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()
def setUp(self): self.domain = bootstrap_domain() LocationType(domain=self.domain.name, name='type').save()
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