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

[change] Make register_menu_group(item) idempotent

Open asmodehn opened this issue 8 months ago • 5 comments

Is your feature request related to a problem? Please describe.

When attempting to use django.test.override_settings(<INSTALLED_APPS>) in tests, it fails.

django.core.exceptions.ImproperlyConfigured: A group/link with config {...} is being registered at position "40",                but another group named "Users & Organizations" is already registered at the same position.

The current problem is that the application is reinitialized (ready called a second time), and register_menu_group fails, since it is already there at the specific location.

Describe the solution you'd like Ideally, register_menu_group(item) should be idempotent, so calling it multiple times would just ensure it worked. Not crash with an error, when everything is actually in place in the menu.

The simple way to achieve this seems to be skipping the error, if it is the exact same item which is already there, that is being registered again. Just replacing it with the new one (in case the old one is broken for some reason known to the user calling register_menu_group)

Additional context I seem to remember I already encountered this problem in the past, and there was an issue somewhere... Can't find it now though, and I am hitting this problem again on openwisp modules v1.2

asmodehn avatar Apr 03 '26 06:04 asmodehn

There's two functions: one to register menu groups and another to register menu items/subitems:

https://github.com/openwisp/openwisp-utils/blob/0e66d19eb66114cef7345517dcf9332669ceb1f7/openwisp_utils/admin_theme/menu.py?utm_source=chatgpt.com#L182-L253

I believe both would be affected by this change.

The proposed change makes sense. The issue was originally opened as a feature request, but I am reclassifying it as a change request because it does not introduce new functionality, it changes the behavior of existing functionality.

I propose the following behavior:

  1. If the exact same menu group or subitem is registered again at the same position, instead of raising an exception as we do now, we should silently allow it and emit an info level log entry for visibility.

  2. If a different target is registered for a position that is already occupied by another target, we should continue raising an exception. In that case, it most likely indicates conflicting application logic, and it is better to fail loudly and early rather than allow the issue to go unnoticed and create more complex problems later on.

nemesifier avatar May 09 '26 15:05 nemesifier

Hi @CodingWithSaksham 👋,

Thank you for opening PR #714 to address this issue!

GitHub did not allow the bot to assign this issue to you automatically, because you are not yet an organization member and have not previously commented on this issue.

To get assigned, please comment @openwisp-companion assign on this issue. Your comment satisfies GitHub's requirement and the bot will then assign the issue to you.

openwisp-companion avatar Jul 04 '26 17:07 openwisp-companion

Connecting these issues together to manage them better: https://github.com/openwisp/openwisp-users/issues/445

CodingWithSaksham avatar Jul 04 '26 17:07 CodingWithSaksham

@openwisp-companion assign

CodingWithSaksham avatar Jul 04 '26 17:07 CodingWithSaksham

This issue has been assigned to @CodingWithSaksham who opened PR #714 to address it. 🎯

openwisp-companion avatar Jul 04 '26 17:07 openwisp-companion