Skip to content

[18.0][ADD] web_operating_unit_switcher: session based OU switcher - #891

Open
luc-demeyer wants to merge 20 commits into
OCA:18.0from
luc-demeyer:18.0-add-web_easy_switch_operating_unit
Open

luc-demeyer wants to merge 20 commits into
OCA:18.0from
luc-demeyer:18.0-add-web_easy_switch_operating_unit

Conversation

@luc-demeyer

Copy link
Copy Markdown
Contributor

this PR superceeds #853 in order to align the OU switcher behavior to Odoo's standard company switcher : session based OU switcher so that a different OU can selected by browser tab.

@oca-cla-bot

oca-cla-bot Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@luc-demeyer

Copy link
Copy Markdown
Contributor Author

@solomonprabu
red pipeline since this work is based upon your #853 and flagged with missing CLA. Can you sign this CLA ?

@luc-demeyer luc-demeyer changed the title [add] web_easy_switch_operating_unit: session based OU switcher [18.0][ADD] web_easy_switch_operating_unit: session based OU switcher Nov 10, 2026

@ByteMeAsap ByteMeAsap left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM!!

luc-demeyer added a commit to luc-demeyer/operating-unit that referenced this pull request Nov 11, 2026
…omain

Replace direct access to user.default_operating_unit_id with
res.users,_get_default_operating_unit() so that the upcoming cookie-based
session OU takes priority (cf. OCA#891).
luc-demeyer added a commit to luc-demeyer/operating-unit that referenced this pull request Nov 11, 2026
…omain

Replace direct access to user.default_operating_unit_id with
res.users,_get_default_operating_unit() so that the upcoming cookie-based
session OU takes priority (cf. OCA#891).
luc-demeyer added a commit to luc-demeyer/operating-unit that referenced this pull request Nov 11, 2026
…omain

Replace direct access to user.default_operating_unit_id with
res.users,_get_default_operating_unit() so that the upcoming cookie-based
session OU takes priority (cf. OCA#891).
Comment on lines +17 to +21
context = self.env.context
if "current_operating_unit_id" in context:
ou_id = context["current_operating_unit_id"]
else:
ou_id = self.env.user.default_operating_unit_id.id

@LINTOANTONY007 LINTOANTONY007 Nov 12, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You can replace these lines with below code
ou_id = self.env.context.get("current_operating_unit_id", False) or self.env.user.default_operating_unit_id.id

Comment thread operating_unit/models/res_users.py Outdated
Comment on lines +41 to +45
if "current_operating_unit_id" in self.env.context:
context_ou_id = self.env.context["current_operating_unit_id"]
if context_ou_id:
return self.env["operating.unit"].browse(context_ou_id)
return self.env["operating.unit"]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You can replace these lines with ,

context = self.env.context
if "current_operating_unit_id" in context:
        return self.env["operating.unit"].browse(context.get("current_operating_unit_id", []))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@LINTOANTONY007
thanks for your feedback, I have simplified your suggestion a bit further and pushed the following code change

    context = self.env.context
    if "current_operating_unit_id" in context:
        return self.env["operating.unit"].browse(
            context["current_operating_unit_id"]
        )

in the js code I set "ouId || false" to the context key hence no context.get needed.

@luc-demeyer
luc-demeyer force-pushed the 18.0-add-web_easy_switch_operating_unit branch from d47b1e9 to b1e643d Compare November 12, 2026 09:20
@luc-demeyer

Copy link
Copy Markdown
Contributor Author

@sbidoul
I superceeded #853 from @solomonprabu and get a red pipeline due to missing CLA but there was no such warning in the originating PR.
It looks like this is false CLA alert.
How can we fix this ?
@solomonprabu has changed employer in the meanwhile and hence doesn't work any more on the project that requires this module.

@sbidoul

sbidoul commented Nov 12, 2026

Copy link
Copy Markdown
Member

@luc-demeyer that's not a false alert, we have no CLA for that person.

@solomonprabu if you are available to click on the CLA button in #891 (comment), that would be helpful. The procedure is simple and only takes a few seconds.

@solomonprabu

Copy link
Copy Markdown

@luc-demeyer that's not a false alert, we have no CLA for that person.

@solomonprabu if you are available to click on the CLA button in #891 (comment), that would be helpful. The procedure is simple and only takes a few seconds.

I have signed the CLA, sorry for keeping youwaiting :)

@AaronHForgeFlow

Copy link
Copy Markdown
Contributor

It is a very interesting module and it works good. Not a blocking thing but I think web_operating_unit_switch would be a more conventional name.

One commit per module is a requirement, web_easy_switch_operating_unit should have only one commit, because this is an [ADD].

luc-demeyer added a commit to luc-demeyer/operating-unit that referenced this pull request Nov 12, 2026
…omain

Replace direct access to user.default_operating_unit_id with
res.users,_get_default_operating_unit() so that the upcoming cookie-based
session OU takes priority (cf. OCA#891).
@luc-demeyer

Copy link
Copy Markdown
Contributor Author

@AaronHForgeFlow
Good suggestion to rename the module.
I prefer web_operating_unit_switcher over web_operating_unit_switch : agree ?
I'll rebase this PR, have a look to the unit tests and rename the module so that this PR is ready for merge.

@AaronHForgeFlow

Copy link
Copy Markdown
Contributor

@luc-demeyer web_operating_unit_switcher sounds good. Thanks.

@luc-demeyer
luc-demeyer force-pushed the 18.0-add-web_easy_switch_operating_unit branch from b1e643d to 873afb1 Compare November 12, 2026 16:23
@luc-demeyer

Copy link
Copy Markdown
Contributor Author

@AaronHForgeFlow
I have now added javascript unit tests but I needed to update eslint.config.cjs
An alternative is that I perform a rebase after you run a copier update.
cf. OCA/web@28f7818

@AaronHForgeFlow

Copy link
Copy Markdown
Contributor

@luc-demeyer thanks, if propose a PR with the copier update I would merge it

@luc-demeyer
luc-demeyer force-pushed the 18.0-add-web_easy_switch_operating_unit branch from 886c5f4 to 1c77cf1 Compare November 13, 2026 10:25
@luc-demeyer luc-demeyer changed the title [18.0][ADD] web_easy_switch_operating_unit: session based OU switcher [18.0][ADD] web_operating_unit_switcher: session based OU switcher Nov 13, 2026
@OCA-git-bot OCA-git-bot added mod:web_operating_unit_switcher Module web_operating_unit_switcher and removed mod:web_easy_switch_operating_unit Module web_easy_switch_operating_unit labels Nov 13, 2026
@luc-demeyer

Copy link
Copy Markdown
Contributor Author

@AaronHForgeFlow

This PR is ready for merge when

  • runners become available and pipeline is green (coverage should be ok now)
  • we squash the commits

For the javascript unit tests I needed to update the CI config.
I am not sure if we want to squash that commit into the commit(s) for adding this module.

There is also a long history behind this module:
created originally by Andre Schenkels (Odoo 8.0)
migrated to Odoo 13.0 by Simon Janssens
migrated to Odoo 18.0 by Solomon Prabu
refactored for session based OU by myself.
The common part is that all contributions have been made for 'CIT services'.

I propose we squash all commits from Solomon into a singe commit (we don't have the prior Odoo 8.0 and 13.0 history if his PR).
I propose also that we squash the 'session based OU' refactoring commits.

But several operating_unit modules are also adapted for the injection of the active OU in the context key and the associated enhancements to the unit tests.
Do you prefer to keep separate commits per module ?

@luc-demeyer

Copy link
Copy Markdown
Contributor Author

@AaronHForgeFlow

I missed your demand "if propose a PR with the copier update I would merge it" while typing my question on how we'll squash the commits.
I can do so (although updating via copier isn't really my cup of tea).
I suppose you mean a separate 'copier update' PR which you'll merge and I'll use for rebasing this PR ?
Please confirm.

@AaronHForgeFlow

Copy link
Copy Markdown
Contributor

@luc-demeyer Can you please check the copier update PR? #898

solomonprabu and others added 20 commits November 20, 2026 11:17
…removal of Controller as OWL adaptation makes it redundant
Refactor this module to allow a different OU per browser tab,
following the same behavior as Odoo's standard company switcher.
In res.users,_get_default_operating_unit(), check
current_operating_unit_id in context first so the cookie-based session
OU takes priority over the user's DB default, enabling a different OU
per browser tab.

In ir.rule,_eval_context(), read the session OU from context directly
(avoids a recursive access check on operating.unit). When a specific OU
is active, narrow operating_unit_ids to that single OU so all security
rules filter records to the selected OU — mirroring the company
switcher behaviour.
Adapt the sale.order _compute_operating_unit_id(), _compute_team_id()
and _check_team_operating_unit() so that only sales teams with an OU
set propagate their OU. For shared (OU less) teams the session OU is
used with fallback to the user's default operating unit.
Replace check_company=True on crm.team,operating_unit_id with an
explicit domain that includes OUs from all allowed companies.
With check_company=True, a team without a company_id generates domain
[('company_id', '=', False)], hiding all OUs (since an OU has always
a company).
Adapt res.partner,operating_unit_default_get() so that
the session OU is used.
Override ir.rule,_compute_domain_keys() to include
current_operating_unit_id in the domain cache key.
Distinguish between "current_operating_unit_id key absent" (switcher not
active → fall back to user's default OU) and "key present but False"
(user explicitly selected No OU → no fallback).

In res.users,_get_default_operating_unit(), return an empty recordset
when the key is present but False, instead of falling back to the
user's default OU.

In ir.rule,_eval_context(), read the key directly instead of using
the "or" shortcut, so ou_id stays False when No OU is selected and
effective_ou_ids falls back to all allowed OUs.
@luc-demeyer
luc-demeyer force-pushed the 18.0-add-web_easy_switch_operating_unit branch from 87bf3cd to d30c07f Compare November 20, 2026 10:23
@luc-demeyer

Copy link
Copy Markdown
Contributor Author

@AaronHForgeFlow
Thanks for the copier update, I have now rebased the PR.

Can you also let me know how to squash the commits (cf. my proposal and questions earlier in this thread) so that we are ready for merge ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved mod:operating_unit Module operating_unit mod:res_partner_operating_unit Module res_partner_operating_unit mod:sale_operating_unit Module sale_operating_unit mod:sales_team_operating_unit Module sales_team_operating_unit mod:web_operating_unit_switcher Module web_operating_unit_switcher series:18.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants