[change] Make menu item registration idempotent #641
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_subitemtwice 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.
π 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.
Comment @coderabbitai help to get the list of available commands.
coverage: 97.645% (+0.004%) from 97.641% β CodingWithSaksham:issues/445-make-menu-item-registration-idempotent into openwisp:master
@coderabbitai review
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.β
Action performed