Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

fsbraun
Copy link
Member

@fsbraun fsbraun commented Aug 28, 2025

It can be less than zero when the from_level is greater than zero.

Description

Related resources

  • #...
  • #...

Checklist

Summary by Sourcery

Fix cut_after to remove children for nodes when levels is less than or equal to zero and add tests to verify this behavior

Bug Fixes:

  • Ensure cut_after treats levels <= 0 as a cutoff and clears node.children accordingly

Tests:

  • Add test_show_menu_cut_inactive to verify that nodes with non-positive levels have no children

#8322)

It can be less than zero when the from_level is greater than zero.

Co-authored-by: Fabian Braun <[email protected]>
Copy link
Contributor

sourcery-ai bot commented Aug 28, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR refines the menu pruning logic to clear children when levels is non-positive and adds a corresponding test to verify inactive node removal at or below zero levels.

Class diagram for updated cut_after function in menu_tags

classDiagram
    class Node {
        children: list[Node]
    }
    class menu_tags {
        +cut_after(node: Node, levels: int)
    }
    Node <|-- menu_tags
    menu_tags : cut_after(node, levels)
    menu_tags : if levels <= 0 then node.children = []
    menu_tags : else recursively cut_after(child, levels-1)
Loading

File-Level Changes

Change Details Files
Broaden cut_after condition to clear children when levels is less than or equal to zero
  • Changed condition from "levels == 0" to "levels <= 0"
  • Ensure node.children is emptied when levels <= 0
menus/templatetags/menu_tags.py
Add test to verify cutting of inactive nodes when level parameter is non-positive
  • Introduced test_show_menu_cut_inactive method
  • Render show_menu with level <= 0 scenario
  • Assert expected counts for child and grandchild nodes
cms/tests/test_menu.py

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

πŸ‘‹ Hi there!

Please remember to MERGE COMMIT pull requests from main!

Do not SQUASH commits to preserve history for the changelog.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • Consider adding a test case for negative levels to explicitly verify that any levels < 0 also result in children being cut.
  • Update the cut_after docstring to mention that non-positive levels (<=0) will clear all children, so the behavior is clear in the API docs.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider adding a test case for negative levels to explicitly verify that any levels < 0 also result in children being cut.
- Update the cut_after docstring to mention that non-positive levels (<=0) will clear all children, so the behavior is clear in the API docs.

## Individual Comments

### Comment 1
<location> `cms/tests/test_menu.py:230` </location>
<code_context>
             tpl = Template("{% load menu_tags %}{% show_menu 0 0 0 0 'menu/menu.html' child %}")
             self.assertRaises(TemplateSyntaxError, tpl.render, context)

+    def test_show_menu_cut_inactive(self):
+        root = self.get_page(2)
+        context = self.get_context(page=root)
+        tpl = Template("{% load menu_tags %}{% show_menu 1 100 0 1 %}")
+        tpl.render(context)
+        nodes = context["children"]
+        self.assertEqual(len(nodes), 2)
+        self.assertEqual(len(nodes[0].children), 1)
+        self.assertEqual(len(nodes[1].children), 0)
+
     def test_show_submenu_nephews(self):
</code_context>

<issue_to_address>
Consider adding tests for negative level values and edge cases.

Please add a test for negative level values (e.g., -1) to verify that children are correctly handled. Also, consider testing with levels > 0 and no children to ensure the logic is robust.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
    def test_show_menu_cut_inactive(self):
        root = self.get_page(2)
        context = self.get_context(page=root)
        tpl = Template("{% load menu_tags %}{% show_menu 1 100 0 1 %}")
        tpl.render(context)
        nodes = context["children"]
        self.assertEqual(len(nodes), 2)
        self.assertEqual(len(nodes[0].children), 1)
        self.assertEqual(len(nodes[1].children), 0)
=======
    def test_show_menu_cut_inactive(self):
        root = self.get_page(2)
        context = self.get_context(page=root)
        tpl = Template("{% load menu_tags %}{% show_menu 1 100 0 1 %}")
        tpl.render(context)
        nodes = context["children"]
        self.assertEqual(len(nodes), 2)
        self.assertEqual(len(nodes[0].children), 1)
        self.assertEqual(len(nodes[1].children), 0)

    def test_show_menu_negative_level(self):
        root = self.get_page(2)
        context = self.get_context(page=root)
        tpl = Template("{% load menu_tags %}{% show_menu -1 100 0 0 %}")
        tpl.render(context)
        nodes = context["children"]
        # Expecting no children for negative level, or specific behavior depending on implementation
        self.assertIsInstance(nodes, list)
        self.assertEqual(len(nodes), 0)

    def test_show_menu_positive_level_no_children(self):
        # Create a page with no children
        root = self.get_page(3)
        context = self.get_context(page=root)
        tpl = Template("{% load menu_tags %}{% show_menu 2 100 0 0 %}")
        tpl.render(context)
        nodes = context["children"]
        self.assertIsInstance(nodes, list)
        self.assertEqual(len(nodes), 0)
>>>>>>> REPLACE

</suggested_fix>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click πŸ‘ or πŸ‘Ž on each comment and I'll use the feedback to improve your reviews.

Comment on lines +230 to +238
def test_show_menu_cut_inactive(self):
root = self.get_page(2)
context = self.get_context(page=root)
tpl = Template("{% load menu_tags %}{% show_menu 1 100 0 1 %}")
tpl.render(context)
nodes = context["children"]
self.assertEqual(len(nodes), 2)
self.assertEqual(len(nodes[0].children), 1)
self.assertEqual(len(nodes[1].children), 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Consider adding tests for negative level values and edge cases.

Please add a test for negative level values (e.g., -1) to verify that children are correctly handled. Also, consider testing with levels > 0 and no children to ensure the logic is robust.

Suggested change
def test_show_menu_cut_inactive(self):
root = self.get_page(2)
context = self.get_context(page=root)
tpl = Template("{% load menu_tags %}{% show_menu 1 100 0 1 %}")
tpl.render(context)
nodes = context["children"]
self.assertEqual(len(nodes), 2)
self.assertEqual(len(nodes[0].children), 1)
self.assertEqual(len(nodes[1].children), 0)
def test_show_menu_cut_inactive(self):
root = self.get_page(2)
context = self.get_context(page=root)
tpl = Template("{% load menu_tags %}{% show_menu 1 100 0 1 %}")
tpl.render(context)
nodes = context["children"]
self.assertEqual(len(nodes), 2)
self.assertEqual(len(nodes[0].children), 1)
self.assertEqual(len(nodes[1].children), 0)
def test_show_menu_negative_level(self):
root = self.get_page(2)
context = self.get_context(page=root)
tpl = Template("{% load menu_tags %}{% show_menu -1 100 0 0 %}")
tpl.render(context)
nodes = context["children"]
# Expecting no children for negative level, or specific behavior depending on implementation
self.assertIsInstance(nodes, list)
self.assertEqual(len(nodes), 0)
def test_show_menu_positive_level_no_children(self):
# Create a page with no children
root = self.get_page(3)
context = self.get_context(page=root)
tpl = Template("{% load menu_tags %}{% show_menu 2 100 0 0 %}")
tpl.render(context)
nodes = context["children"]
self.assertIsInstance(nodes, list)
self.assertEqual(len(nodes), 0)

@fsbraun fsbraun changed the title Fix: Cut children from inactive nodes when level is less or equal to 0 (#8322) fix: Cut children from inactive nodes when level is less or equal to 0 (#8322) Aug 28, 2025
@fsbraun fsbraun merged commit bc2cf65 into release/5.0.x Sep 1, 2025
68 of 70 checks passed
@fsbraun fsbraun changed the title fix: Cut children from inactive nodes when level is less or equal to 0 (#8322) fix: Cut children from inactive menu nodes when level is less or equal to 0 (#8322) Sep 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants