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

Skip to content

Conversation

stefanw
Copy link
Contributor

@stefanw stefanw commented Aug 25, 2025

Description

Fixes #8321 and adds a test.

Related resources

Checklist

Summary by Sourcery

Fix the cut_after logic to remove children when levels is non-positive and add a test to verify inactive nodes have no children at non-positive levels

Bug Fixes:

  • Ensure cut_after clears node.children when levels is less or equal to zero instead of only zero

Tests:

  • Add test_show_menu_cut_inactive to verify children are cut from inactive nodes at non-positive levels

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

sourcery-ai bot commented Aug 25, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Update cut_after to remove children at non-positive levels and add a test verifying show_menu cuts inactive node children correctly.

Class diagram for updated cut_after function in menu node

classDiagram
    class Node {
        children: list[Node]
    }
    class MenuTags {
        +cut_after(node: Node, levels: int)
    }
    MenuTags --> Node: operates on
Loading

File-Level Changes

Change Details Files
Extend cut_after to handle non-positive level values
  • Changed condition from levels == 0 to levels <= 0
  • Left recursive logic unchanged for positive levels
menus/templatetags/menu_tags.py
Add test for cutting inactive node children in show_menu
  • Introduced test_show_menu_cut_inactive to assert correct child counts
  • Checked that inactive nodes at or below the threshold have no children
cms/tests/test_menu.py

Assessment against linked issues

Issue Objective Addressed Explanation
#8321 Fix the bug where show_menu renders children of inactive sibling nodes when extra_inactive is set to 0 and from_level is above 0.
#8321 Ensure that the check which cuts children from inactive nodes correctly handles cases where the level is less than or equal to 0.
#8321 Add a test to verify that only the expected nodes and children are rendered when show_menu is called with the problematic parameters.

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
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 and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## 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 assertions for edge cases with level=0 and negative levels.

Adding assertions for level=0 and negative values will help verify the function's behavior for all edge cases.
</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)
        # Standard case
        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)

        # Edge case: level=0
        tpl_zero = Template("{% load menu_tags %}{% show_menu 0 100 0 1 %}")
        tpl_zero.render(context)
        nodes_zero = context["children"]
        # Assert that nodes_zero is not empty and has expected structure
        self.assertTrue(isinstance(nodes_zero, list))
        self.assertGreaterEqual(len(nodes_zero), 0)

        # Edge case: negative level
        tpl_negative = Template("{% load menu_tags %}{% show_menu -1 100 0 1 %}")
        # Depending on implementation, this may raise an error or return empty list
        try:
            tpl_negative.render(context)
            nodes_negative = context["children"]
            self.assertTrue(isinstance(nodes_negative, list))
            self.assertEqual(len(nodes_negative), 0)
        except Exception as e:
            self.assertIsInstance(e, Exception)
>>>>>>> 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 assertions for edge cases with level=0 and negative levels.

Adding assertions for level=0 and negative values will help verify the function's behavior for all edge cases.

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)
# Standard case
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)
# Edge case: level=0
tpl_zero = Template("{% load menu_tags %}{% show_menu 0 100 0 1 %}")
tpl_zero.render(context)
nodes_zero = context["children"]
# Assert that nodes_zero is not empty and has expected structure
self.assertTrue(isinstance(nodes_zero, list))
self.assertGreaterEqual(len(nodes_zero), 0)
# Edge case: negative level
tpl_negative = Template("{% load menu_tags %}{% show_menu -1 100 0 1 %}")
# Depending on implementation, this may raise an error or return empty list
try:
tpl_negative.render(context)
nodes_negative = context["children"]
self.assertTrue(isinstance(nodes_negative, list))
self.assertEqual(len(nodes_negative), 0)
except Exception as e:
self.assertIsInstance(e, Exception)

@fsbraun fsbraun changed the title Fix: Cut children from inactive nodes when level is less or equal to 0 fix: Cut children from inactive menu nodes when level is less or equal to 0 Aug 25, 2025
Copy link

codecov bot commented Aug 25, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.69%. Comparing base (ee2fc95) to head (be8a711).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #8322   +/-   ##
=======================================
  Coverage   89.69%   89.69%           
=======================================
  Files         129      129           
  Lines       12733    12733           
=======================================
  Hits        11421    11421           
  Misses       1312     1312           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@fsbraun fsbraun left a comment

Choose a reason for hiding this comment

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

Nice work! Thank you!

@fsbraun fsbraun merged commit aaeae08 into django-cms:main Aug 28, 2025
67 checks passed
fsbraun added a commit that referenced this pull request Aug 28, 2025
#8322)

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

Co-authored-by: Fabian Braun <[email protected]>
fsbraun added a commit that referenced this pull request Sep 1, 2025
#8322) (#8324)

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

Co-authored-by: Stefan Wehrmeyer <[email protected]>
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.

[BUG] Showing a menu above level 0 always renders sibling's children
2 participants