def test_returns_none_if_provided_page_is_not_a_descendant_of_a_section_root(self):
        # Using the default section root depth of 3
        with self.assertNumQueries(0):
            result = derive_section_root(self.page_with_depth_of_2)
            self.assertIs(result, None)

        # Using a custom section root depth of 4
        with self.settings(WAGTAILMENUS_SECTION_ROOT_DEPTH=4):
            with self.assertNumQueries(0):
                result = derive_section_root(self.page_with_depth_of_3)
                self.assertIs(result, None)
Exemple #2
0
    def test_returns_same_page_if_provided_page_is_section_root(self):
        # Using the default section root depth of 3
        with self.assertNumQueries(1):
            # One query should be used to get the specific page
            result = derive_section_root(self.page_with_depth_of_3)
            # The function should return the specific version of the same page
            self.assertEqual(result, self.page_with_depth_of_3.specific)

        # Using a custom section root depth of 4
        with self.settings(WAGTAILMENUS_SECTION_ROOT_DEPTH=4):
            with self.assertNumQueries(1):
                # One query should be used to get the specific page
                result = derive_section_root(self.page_with_depth_of_4)
                # The function should return the specific version of the same page
                self.assertEqual(result, self.page_with_depth_of_4.specific)
    def test_returns_section_root_if_provided_page_is_a_descendant_of_one(self):
        # Using the default section root depth of 3
        with self.assertNumQueries(2):
            # Two queries should be used to identify the page
            # and to get the specific version
            result = derive_section_root(self.page_with_depth_of_5)
            self.assertEqual(result.depth, defaults.SECTION_ROOT_DEPTH)
            self.assertIsInstance(result, TopLevelPage)

        # Using a custom section root depth of 4
        with self.settings(WAGTAILMENUS_SECTION_ROOT_DEPTH=4):
            with self.assertNumQueries(2):
                result = derive_section_root(self.page_with_depth_of_5)
                self.assertEqual(result.depth, 4)
                self.assertIsInstance(result, LowLevelPage)
    def _get_wagtailmenus_vals():
        current_page = request.META.get('WAGTAILMENUS_CURRENT_PAGE')
        section_root = request.META.get('WAGTAILMENUS_CURRENT_SECTION_ROOT')
        site = get_site_from_request(request, fallback_to_default=True)
        ancestor_ids = ()
        match = None

        guess_position = settings.GUESS_TREE_POSITION_FROM_PATH
        section_root_depth = settings.SECTION_ROOT_DEPTH

        if guess_position and not current_page:
            match, full_url_match = derive_page(request, site)
            if full_url_match:
                current_page = match

        if not section_root and current_page or match:
            section_root = derive_section_root(current_page or match)

        if current_page or match:
            page = current_page or match
            if page.depth >= section_root_depth:
                ancestor_ids = page.get_ancestors(inclusive=True).filter(
                    depth__gte=section_root_depth).values_list('id', flat=True)

        return {
            'current_page': current_page,
            'section_root': section_root,
            'current_page_ancestor_ids': ancestor_ids,
        }
def wagtailmenu_params_helper(page, request, serve_args, serve_kwargs):
    request.META.update({
        'WAGTAILMENUS_CURRENT_PAGE': page,
        'WAGTAILMENUS_CURRENT_SECTION_ROOT': derive_section_root(page),
    })