openwisp-utils icon indicating copy to clipboard operation
openwisp-utils copied to clipboard

[change] Make menu item registration idempotent #641

Open CodingWithSaksham opened this issue 4 months ago β€’ 2 comments

Re-initializing the Django app registery calls AppConfig.ready() again which re-invokes register_menu_group() / register_menu_subitem() with the same config. Both functions unconditionally raised ImproperlyConfigured whenever the target position was already occupied, even when it was the exact same app re-registering the exact same item, making it impossible to survive repeated app registry population.

Both functions now compare the incoming config against the config of the item already registered at that position and log at INFO level if identical config is found Closes

Checklist

  • [x] I have read the OpenWISP Contributing Guidelines.
  • [x] I have manually tested the changes proposed in this pull request.
  • [x] I have written new test cases for new code and/or updated existing tests for changes to existing code.
  • [ ] I have updated the documentation.

Reference to Existing Issue

Closes #641 .

Description of Changes

  • Previously, calling register_menu_group/register_menu_subitem twice with the same config at the same position always errored, even with app reloads or repeated ready() calls. Now each menu item stores its config, and both functions check for an identical config before raising, if it matches, they log an INFO message and skip instead of erroring.
  • Tests added for the new skip-and-log behavior.

CodingWithSaksham avatar Jul 04 '26 17:07 CodingWithSaksham

Review Change Stack

πŸ“ Walkthrough

Walkthrough

The change makes menu registration idempotent when the same config is registered again at an occupied group or item position. BaseMenuItem now stores config, the menu registration functions log and return on exact config matches, and the docs and tests were updated to cover the no-op case versus the conflicting-config error case.

Estimated code review effort: 2 (Simple) | ~10 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant register_menu_group
  participant register_menu_subitem
  participant MENU
  participant logger

  Caller->>register_menu_group: register_menu_group(position, config)
  register_menu_group->>MENU: compare existing config at position
  alt identical config already present
    register_menu_group->>logger: info "already registered"
    register_menu_group-->>Caller: return
  else new config
    register_menu_group->>MENU: register item
    register_menu_group-->>Caller: return
  end

  Caller->>register_menu_subitem: register_menu_subitem(item_position, config)
  register_menu_subitem->>MENU: compare existing item config
  alt identical config already present
    register_menu_subitem->>logger: info "already registered"
    register_menu_subitem-->>Caller: return
  else new config
    register_menu_subitem->>MENU: register subitem
    register_menu_subitem-->>Caller: return
  end

Related issues: #641

Suggested labels: enhancement, admin_theme

Suggested reviewers: none


[!CAUTION]

Pre-merge checks failed

Please resolve all errors before merging. Addressing warnings is optional.

  • [ ] Ignore

❌ Failed checks (1 error)

Check name Status Explanation Resolution
General Rules ❌ Error New exception/log messages in menu.py are user-facing but aren’t marked with Django i18n helpers, violating the Django string-translatability rule. Wrap the new messages in gettext_lazy/_ and update tests/docs if needed.
βœ… Passed checks (7 passed)
Check name Status Explanation
Linked Issues check βœ… Passed The changes implement idempotent menu-group registration with matching-config no-op behavior and tests for the reported issue.
Out of Scope Changes check βœ… Passed The edits stay focused on the menu idempotency fix, tests, and docs, with no clearly unrelated changes.
Bug Fixes βœ… Passed Root cause is fixed by idempotent config comparison, and regression tests cover occupied-position re-registration with identical configs and INFO logs.
Features βœ… Passed Issue #641 exists and is triaged/assigned; docs and tests were updated for the idempotent menu registration behavior.
Changes βœ… Passed Docs were updated for the new idempotent behavior, tests cover the non-trivial re-registration cases, and there are no UI changes or API breaks needing migration.
Title check βœ… Passed The title is descriptive, matches the idempotent menu registration change, and uses the required [change] prefix.
Description check βœ… Passed The description includes the checklist, linked issue, and change summary; only the optional screenshot section is missing.
✨ Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • [ ] Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot] avatar Jul 04 '26 17:07 coderabbitai[bot]

Coverage Status

coverage: 97.645% (+0.004%) from 97.641% β€” CodingWithSaksham:issues/445-make-menu-item-registration-idempotent into openwisp:master

coveralls avatar Jul 04 '26 17:07 coveralls

@coderabbitai review

CodingWithSaksham avatar Jul 06 '26 15:07 CodingWithSaksham

βœ… Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

coderabbitai[bot] avatar Jul 06 '26 15:07 coderabbitai[bot]