def test_location_queries(self): test_state1 = make_loc("teststate1", type="state", parent=self.user.location, domain=self.domain.name) test_state2 = make_loc("teststate2", type="state", parent=self.user.location, domain=self.domain.name) test_village1 = make_loc("testvillage1", type="village", parent=test_state1, domain=self.domain.name) test_village1.site_code = "tv1" test_village1.save() test_village2 = make_loc("testvillage2", type="village", parent=test_state2, domain=self.domain.name) def compare(list1, list2): self.assertEqual(set([l._id for l in list1]), set([l._id for l in list2])) # descendants compare([test_state1, test_state2, test_village1, test_village2], self.user.location.descendants) # children compare([test_state1, test_state2], self.user.location.children) # siblings compare([test_state2], test_state1.siblings()) # parent and parent_id self.assertEqual(self.user.location._id, test_state1.parent_id) self.assertEqual(self.user.location._id, test_state1.parent._id) # is_root self.assertTrue(self.user.location.is_root) self.assertFalse(test_state1.is_root) # Location.root_locations compare([self.user.location], Location.root_locations(self.domain.name)) # Location.filter_by_type compare([test_village1, test_village2], Location.filter_by_type(self.domain.name, "village")) compare([test_village1], Location.filter_by_type(self.domain.name, "village", test_state1)) # Location.get_in_domain test_village2.domain = "rejected" bootstrap_location_types("rejected") test_village2.save() self.assertEqual(Location.get_in_domain(self.domain.name, test_village1._id)._id, test_village1._id) self.assertIsNone(Location.get_in_domain(self.domain.name, test_village2._id)) self.assertIsNone(Location.get_in_domain(self.domain.name, "not-a-real-id")) self.assertEqual( {loc._id for loc in [self.user.location, test_state1, test_state2, test_village1]}, set(SQLLocation.objects.filter(domain=self.domain.name).location_ids()), ) # Location.by_site_code self.assertEqual(test_village1._id, Location.by_site_code(self.domain.name, "tv1")._id) self.assertIsNone(None, Location.by_site_code(self.domain.name, "notreal")) # Location.by_domain compare([self.user.location, test_state1, test_state2, test_village1], Location.by_domain(self.domain.name))
def load_locs_json(domain, selected_loc_id=None): """initialize a json location tree for drill-down controls on the client. tree is only partially initialized and branches will be filled in on the client via ajax. what is initialized: * all top level locs * if a 'selected' loc is provided, that loc and its complete ancestry """ def loc_to_json(loc): return {"name": loc.name, "location_type": loc.location_type, "uuid": loc._id} loc_json = [loc_to_json(loc) for loc in root_locations(domain)] # if a location is selected, we need to pre-populate its location hierarchy # so that the data is available client-side to pre-populate the drop-downs selected = Location.get_in_domain(domain, selected_loc_id) if selected: lineage = list(Location.view("_all_docs", keys=selected.path, include_docs=True)) parent = {"children": loc_json} for loc in lineage: # find existing entry in the json tree that corresponds to this loc this_loc = [k for k in parent["children"] if k["uuid"] == loc._id][0] this_loc["children"] = [loc_to_json(loc) for loc in loc.children] parent = this_loc return loc_json
def load_locs_json(domain, selected_loc_id=None): """initialize a json location tree for drill-down controls on the client. tree is only partially initialized and branches will be filled in on the client via ajax. what is initialized: * all top level locs * if a 'selected' loc is provided, that loc and its complete ancestry """ def loc_to_json(loc): return { 'name': loc.name, 'location_type': loc.location_type, 'uuid': loc._id, } loc_json = [loc_to_json(loc) for loc in root_locations(domain)] # if a location is selected, we need to pre-populate its location hierarchy # so that the data is available client-side to pre-populate the drop-downs selected = Location.get_in_domain(domain, selected_loc_id) if selected: lineage = list(Location.view('_all_docs', keys=selected.path, include_docs=True)) parent = {'children': loc_json} for loc in lineage: # find existing entry in the json tree that corresponds to this loc this_loc = [k for k in parent['children'] if k['uuid'] == loc._id][0] this_loc['children'] = [loc_to_json(loc) for loc in loc.children] parent = this_loc return loc_json
def active_location(self): return Location.get_in_domain(self.domain, self.config.get('location_id'))
def test_location_queries(self): test_state1 = make_loc( 'teststate1', type='state', parent=self.user.location, domain=self.domain.name ) test_state2 = make_loc( 'teststate2', type='state', parent=self.user.location, domain=self.domain.name ) test_village1 = make_loc( 'testvillage1', type='village', parent=test_state1, domain=self.domain.name ) test_village1.site_code = 'tv1' test_village1.save() test_village2 = make_loc( 'testvillage2', type='village', parent=test_state2, domain=self.domain.name ) def compare(list1, list2): self.assertEqual( set([l._id for l in list1]), set([l._id for l in list2]) ) # descendants compare( [test_state1, test_state2, test_village1, test_village2], self.user.location.descendants ) # children compare( [test_state1, test_state2], self.user.location.children ) # siblings compare( [test_state2], test_state1.siblings() ) # parent and parent_id self.assertEqual( self.user.location._id, test_state1.parent_id ) self.assertEqual( self.user.location._id, test_state1.parent._id ) # is_root self.assertTrue(self.user.location.is_root) self.assertFalse(test_state1.is_root) # Location.root_locations compare( [self.user.location], Location.root_locations(self.domain.name) ) # Location.filter_by_type compare( [test_village1, test_village2], Location.filter_by_type(self.domain.name, 'village') ) compare( [test_village1], Location.filter_by_type(self.domain.name, 'village', test_state1) ) # Location.get_in_domain test_village2.domain = 'rejected' bootstrap_location_types('rejected') test_village2.save() self.assertEqual( Location.get_in_domain(self.domain.name, test_village1._id)._id, test_village1._id ) self.assertIsNone( Location.get_in_domain(self.domain.name, test_village2._id), ) self.assertIsNone( Location.get_in_domain(self.domain.name, 'not-a-real-id'), ) self.assertEqual( {loc._id for loc in [self.user.location, test_state1, test_state2, test_village1]}, set(SQLLocation.objects.filter(domain=self.domain.name).location_ids()), ) # Location.by_site_code self.assertEqual( test_village1._id, Location.by_site_code(self.domain.name, 'tv1')._id ) self.assertIsNone( None, Location.by_site_code(self.domain.name, 'notreal') ) # Location.by_domain compare( [self.user.location, test_state1, test_state2, test_village1], Location.by_domain(self.domain.name) )
def test_location_queries(self): test_state1 = make_loc( 'teststate1', type='state', parent=self.user.locations[0] ) test_state2 = make_loc( 'teststate2', type='state', parent=self.user.locations[0] ) test_village1 = make_loc( 'testvillage1', type='village', parent=test_state1 ) test_village1.site_code = 'tv1' test_village1.save() test_village2 = make_loc( 'testvillage2', type='village', parent=test_state2 ) def compare(list1, list2): self.assertEqual( set([l._id for l in list1]), set([l._id for l in list2]) ) # descendants compare( [test_state1, test_state2, test_village1, test_village2], self.user.locations[0].descendants ) # children compare( [test_state1, test_state2], self.user.locations[0].children ) # siblings compare( [test_state2], test_state1.siblings() ) # parent and parent_id self.assertEqual( self.user.locations[0]._id, test_state1.parent_id ) self.assertEqual( self.user.locations[0]._id, test_state1.parent._id ) # is_root self.assertTrue(self.user.locations[0].is_root) self.assertFalse(test_state1.is_root) # Location.root_locations compare( [self.user.locations[0]], Location.root_locations(self.domain.name) ) # Location.filter_by_type compare( [test_village1, test_village2], Location.filter_by_type(self.domain.name, 'village') ) compare( [test_village1], Location.filter_by_type(self.domain.name, 'village', test_state1) ) # Location.filter_by_type_count self.assertEqual( 2, Location.filter_by_type_count(self.domain.name, 'village') ) self.assertEqual( 1, Location.filter_by_type_count(self.domain.name, 'village', test_state1) ) # Location.get_in_domain test_village2.domain = 'rejected' test_village2.save() self.assertEqual( Location.get_in_domain(self.domain.name, test_village1._id)._id, test_village1._id ) self.assertIsNone( Location.get_in_domain(self.domain.name, test_village2._id), ) self.assertIsNone( Location.get_in_domain(self.domain.name, 'not-a-real-id'), ) # Location.all_locations compare( [self.user.locations[0], test_state1, test_state2, test_village1], Location.all_locations(self.domain.name) ) # Location.by_site_code self.assertEqual( test_village1._id, Location.by_site_code(self.domain.name, 'tv1')._id ) self.assertIsNone( None, Location.by_site_code(self.domain.name, 'notreal') ) # Location.by_domain compare( [self.user.locations[0], test_state1, test_state2, test_village1], Location.by_domain(self.domain.name) )
def test_location_queries(self): test_state1 = make_loc( 'teststate1', type='state', parent=self.user.location, domain=self.domain.name ) test_state2 = make_loc( 'teststate2', type='state', parent=self.user.location, domain=self.domain.name ) test_village1 = make_loc( 'testvillage1', type='village', parent=test_state1, domain=self.domain.name ) test_village1.site_code = 'tv1' test_village1.save() test_village2 = make_loc( 'testvillage2', type='village', parent=test_state2, domain=self.domain.name ) def compare(list1, list2): self.assertEqual( set([l._id for l in list1]), set([l._id for l in list2]) ) # descendants compare( [test_state1, test_state2, test_village1, test_village2], self.user.location.descendants ) # children compare( [test_state1, test_state2], self.user.location.children ) # siblings compare( [test_state2], test_state1.siblings() ) # parent and parent_id self.assertEqual( self.user.location._id, test_state1.parent_id ) self.assertEqual( self.user.location._id, test_state1.parent._id ) # is_root self.assertTrue(self.user.location.is_root) self.assertFalse(test_state1.is_root) # Location.root_locations compare( [self.user.location], Location.root_locations(self.domain.name) ) # Location.filter_by_type compare( [test_village1, test_village2], Location.filter_by_type(self.domain.name, 'village') ) compare( [test_village1], Location.filter_by_type(self.domain.name, 'village', test_state1) ) # Location.get_in_domain test_village2.domain = 'rejected' bootstrap_location_types('rejected') test_village2.save() self.assertEqual( Location.get_in_domain(self.domain.name, test_village1._id)._id, test_village1._id ) self.assertIsNone( Location.get_in_domain(self.domain.name, test_village2._id), ) self.assertIsNone( Location.get_in_domain(self.domain.name, 'not-a-real-id'), ) def _all_locations(domain): return Location.view( 'locations/hierarchy', startkey=[domain], endkey=[domain, {}], reduce=False, include_docs=True ).all() compare( [self.user.location, test_state1, test_state2, test_village1], _all_locations(self.domain.name) ) # Location.by_site_code self.assertEqual( test_village1._id, Location.by_site_code(self.domain.name, 'tv1')._id ) self.assertIsNone( None, Location.by_site_code(self.domain.name, 'notreal') ) # Location.by_domain compare( [self.user.location, test_state1, test_state2, test_village1], Location.by_domain(self.domain.name) )