Esempio n. 1
0
    def test_archive(self):
        test_state = make_loc(
            'teststate',
            type='state',
            parent=self.user.location
        )
        test_state.save()

        original_count = len(list(Location.by_domain(self.domain.name)))

        loc = self.user.location
        loc.archive()

        # it should also archive children
        self.assertEqual(
            len(list(Location.by_domain(self.domain.name))),
            original_count - 2
        )
        self.assertEqual(
            len(Location.root_locations(self.domain.name)),
            0
        )

        loc.unarchive()

        # and unarchive children
        self.assertEqual(
            len(list(Location.by_domain(self.domain.name))),
            original_count
        )
        self.assertEqual(
            len(Location.root_locations(self.domain.name)),
            1
        )
Esempio n. 2
0
    def test_full_delete(self):
        test_loc = make_loc(
            'test_loc',
            type='state',
            parent=self.user.location
        )
        test_loc.save()

        original_count = len(list(Location.by_domain(self.domain.name)))

        loc = self.user.location
        loc.full_delete()

        # it should also delete children
        self.assertEqual(
            len(list(Location.by_domain(self.domain.name))),
            original_count - 2
        )
        self.assertEqual(
            len(Location.root_locations(self.domain.name)),
            0
        )
        # permanently gone from sql db
        self.assertEqual(
            len(SQLLocation.objects.all()),
            0
        )
Esempio n. 3
0
def location_fixture_generator(user, version, last_sync=None):
    """
    By default this will generate a fixture for the users
    location and it's "footprint", meaning the path
    to a root location through parent hierarchies.

    There is an admin feature flag that will make this generate
    a fixture with ALL locations for the domain.
    """
    project = user.project
    if (not project or not project.commtrack_enabled
        or not project.commtrack_settings
        or not project.commtrack_settings.sync_location_fixtures):
            return []

    if toggles.SYNC_ALL_LOCATIONS.enabled(user.domain):
        location_db = _location_footprint(Location.by_domain(user.domain))
    else:
        locations = []
        if user.location:
            # add users location (and ancestors) to fixture
            locations.append(user.location)

            # optionally add all descendants as well
            if user.location.location_type_object.view_descendants:
                locations += user.location.descendants

        if user.project.supports_multiple_locations_per_user:
            # this might add duplicate locations but we filter that out later
            locations += user.locations
        location_db = _location_footprint(locations)

    if not should_sync_locations(last_sync, location_db):
        return []

    root = ElementTree.Element('fixture',
                               {'id': 'commtrack:locations',
                                'user_id': user.user_id})

    loc_types = project.location_types
    type_to_slug_mapping = dict((ltype.name, ltype.code) for ltype in loc_types)

    def location_type_lookup(location_type):
        return type_to_slug_mapping.get(location_type, unicode_slug(location_type))

    if toggles.SYNC_ALL_LOCATIONS.enabled(user.domain):
        root_locations = Location.root_locations(user.domain)
    else:
        root_locations = filter(lambda loc: loc.parent_id is None, location_db.by_id.values())

    if not root_locations:
        return []
    else:
        _append_children(root, location_db, root_locations, location_type_lookup)
        return [root]
Esempio n. 4
0
    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))
Esempio n. 5
0
    def test_archive(self):
        test_state = make_loc('teststate',
                              type='state',
                              parent=self.user.location)
        test_state.save()

        original_count = len(list(Location.by_domain(self.domain.name)))

        loc = self.user.sql_location
        loc.archive()

        # it should also archive children
        self.assertEqual(len(list(Location.by_domain(self.domain.name))),
                         original_count - 2)
        self.assertEqual(len(Location.root_locations(self.domain.name)), 0)

        loc.unarchive()

        # and unarchive children
        self.assertEqual(len(list(Location.by_domain(self.domain.name))),
                         original_count)
        self.assertEqual(len(Location.root_locations(self.domain.name)), 1)
Esempio n. 6
0
    def test_full_delete(self):
        test_loc = make_loc('test_loc',
                            type='state',
                            parent=self.user.location)
        test_loc.save()

        original_count = len(list(Location.by_domain(self.domain.name)))

        loc = self.user.sql_location
        loc.full_delete()

        # it should also delete children
        self.assertEqual(len(list(Location.by_domain(self.domain.name))),
                         original_count - 2)
        self.assertEqual(len(Location.root_locations(self.domain.name)), 0)
        # permanently gone from sql db
        self.assertEqual(len(SQLLocation.objects.all()), 0)
Esempio n. 7
0
    def copy_locations(self):
        from corehq.apps.locations.models import LocationType
        from corehq.apps.locations.models import Location
        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

        def copy_location_hierarchy(location, id_map):
            new_lineage = []
            for ancestor in location.sql_location.lineage:
                try:
                    new_lineage.append(id_map[ancestor])
                except KeyError:
                    self.stderr.write(
                        "Ancestor {} for location {} missing".format(
                            location._id, ancestor))
            location.lineage = new_lineage

            old_type_name = location.location_type_name
            if not self.no_commmit:
                location._sql_location_type = LocationType.objects.get(
                    domain=self.new_domain,
                    name=old_type_name,
                )
            children = location.get_children()
            old_id, new_id = self.save_couch_copy(location, self.new_domain)
            id_map[old_id] = new_id
            for child in children:
                copy_location_hierarchy(child, id_map)

        locations = Location.root_locations(self.existing_domain)
        for location in locations:
            copy_location_hierarchy(location, {})
Esempio n. 8
0
def location_fixture_generator(user, version, case_sync_op=None, last_sync=None):
    """
    By default this will generate a fixture for the users
    location and it's "footprint", meaning the path
    to a root location through parent hierarchies.

    There is an admin feature flag that will make this generate
    a fixture with ALL locations for the domain.
    """
    project = user.project
    if (not project or not project.commtrack_enabled
        or not project.commtrack_settings
        or not project.commtrack_settings.sync_location_fixtures):
            return []

    rewrapped_user = CommTrackUser.wrap(user.to_json())
    if toggles.SYNC_ALL_LOCATIONS.enabled(user.domain):
        location_db = _location_footprint(Location.by_domain(user.domain))
    else:
        location_db = _location_footprint(rewrapped_user.locations)

    if not should_sync_locations(last_sync, location_db):
        return []

    root = ElementTree.Element('fixture',
                               {'id': 'commtrack:locations',
                                'user_id': user.user_id})

    loc_types = project.commtrack_settings.location_types
    type_to_slug_mapping = dict((ltype.name, ltype.code) for ltype in loc_types)

    def location_type_lookup(location_type):
        return type_to_slug_mapping.get(location_type, unicode_slug(location_type))

    if toggles.SYNC_ALL_LOCATIONS.enabled(user.domain):
        root_locations = Location.root_locations(user.domain)
    else:
        root_locations = filter(lambda loc: loc.parent_id is None, location_db.by_id.values())

    _append_children(root, location_db, root_locations, location_type_lookup)
    return [root]
Esempio n. 9
0
    def copy_locations(self):
        from corehq.apps.locations.models import LocationType
        from corehq.apps.locations.models import Location
        from corehq.apps.locations.views import LocationFieldsView

        self._copy_custom_data(LocationFieldsView.field_type)

        location_types = LocationType.objects.by_domain(self.existing_domain)
        previous_location_type = None
        for location_type in location_types:
            if previous_location_type:
                location_type.parent_type = previous_location_type
            self.save_sql_copy(location_type, self.new_domain)
            previous_location_type = location_type

        def copy_location_hierarchy(location, id_map):
            new_lineage = []
            for ancestor in location.lineage:
                try:
                    new_lineage.append(id_map[ancestor])
                except KeyError:
                    self.stderr.write("Ancestor {} for location {} missing".format(location._id, ancestor))
            location.lineage = new_lineage

            old_type_name = location.location_type_name
            if not self.no_commmit:
                location._sql_location_type = LocationType.objects.get(
                    domain=self.new_domain,
                    name=old_type_name,
                )
            children = location.children
            old_id, new_id = self.save_couch_copy(location, self.new_domain)
            id_map[old_id] = new_id
            for child in children:
                copy_location_hierarchy(child, id_map)

        locations = Location.root_locations(self.existing_domain)
        for location in locations:
            copy_location_hierarchy(location, {})
Esempio n. 10
0
    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)
        )
Esempio n. 11
0
    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)
        )
Esempio n. 12
0
    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.get_children()
        )

        # parent and parent_location_id
        self.assertEqual(
            self.user.location.location_id,
            test_state1.parent_location_id
        )
        self.assertEqual(
            self.user.location.location_id,
            test_state1.parent._id
        )

        # 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)
        )

        create_domain('rejected')
        bootstrap_location_types('rejected')
        test_village2.domain = 'rejected'
        test_village2.save()
        self.assertEqual(
            {loc.location_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)
        )
Esempio n. 13
0
    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)
        )