-
-
Notifications
You must be signed in to change notification settings - Fork 272
[18.0][ADD] operating_unit_isolation: Introducing Operating Unit Isolation #859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
solomonprabu
wants to merge
3
commits into
OCA:18.0
Choose a base branch
from
solomonprabu:18.0-add-operating_unit_isolation
base: 18.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| ======================== | ||
| Operating Unit Isolation | ||
| ======================== | ||
|
|
||
| .. | ||
| !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
| !! This file is generated by oca-gen-addon-readme !! | ||
| !! changes will be overwritten. !! | ||
| !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
| !! source digest: sha256:8b1b300b6d9585b5c6702476a01dc638cc0a401514c3ef25c5e774b90387a28d | ||
| !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
|
|
||
| .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png | ||
| :target: https://odoo-community.org/page/development-status | ||
| :alt: Beta | ||
| .. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png | ||
| :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html | ||
| :alt: License: AGPL-3 | ||
| .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Foperating--unit-lightgray.png?logo=github | ||
| :target: https://github.com/OCA/operating-unit/tree/18.0/operating_unit_isolation | ||
| :alt: OCA/operating-unit | ||
| .. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png | ||
| :target: https://translation.odoo-community.org/projects/operating-unit-18-0/operating-unit-18-0-operating_unit_isolation | ||
| :alt: Translate me on Weblate | ||
| .. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png | ||
| :target: https://runboat.odoo-community.org/builds?repo=OCA/operating-unit&target_branch=18.0 | ||
| :alt: Try me on Runboat | ||
|
|
||
| |badge1| |badge2| |badge3| |badge4| |badge5| | ||
|
|
||
| An extension to provide dynamic isolation for operating units across | ||
| models. | ||
|
|
||
| This module ensures that users can only select related records that | ||
| belong to the same Operating Unit as the current record or its parent | ||
| record. It achieves this by seamlessly injecting the current record and | ||
| parent record state from the frontend (Odoo 18 OWL framework) into the | ||
| backend ``_search`` operations. | ||
|
|
||
| When a user attempts to search or select a record for a relation (e.g., | ||
| in a dropdown autocomplete), this module will dynamically filter the | ||
| available options based on the ``operating_unit_id`` of the active | ||
| record. | ||
|
|
||
| This isolation is triggered automatically for any relation field that | ||
| specifies the ``operating_unit_isolation`` attribute. | ||
|
|
||
| **Table of contents** | ||
|
|
||
| .. contents:: | ||
| :local: | ||
|
|
||
| Usage | ||
| ===== | ||
|
|
||
| To use this functionality in your own module, add the | ||
| ``operating_unit_isolation`` property to the field definitions in your | ||
| models. | ||
|
|
||
| **Isolating based on the current record's Operating Unit:** | ||
|
|
||
| If you have a field on a model and you want the selectable records to be | ||
| restricted to the same Operating Unit as the current record, use the | ||
| field name of the operating unit as the value: | ||
|
|
||
| .. code:: python | ||
|
|
||
| from odoo import fields, models | ||
|
|
||
| class CustomModel(models.Model): | ||
| _name = "custom.model" | ||
|
|
||
| operating_unit_id = fields.Many2one("operating.unit", "Operating Unit") | ||
|
|
||
| # This will restrict selectable partners to those matching the | ||
| # 'operating_unit_id' of this custom.model record (or those without an OU) | ||
| partner_id = fields.Many2one( | ||
| "res.partner", | ||
| string="Partner", | ||
| operating_unit_isolation="operating_unit_id" | ||
| ) | ||
|
|
||
| **Isolating based on a parent record's Operating Unit (One2many | ||
| context):** | ||
|
|
||
| If you are working within a One2many line (e.g., ``sale.order.line``) | ||
| and want to filter the relation based on the parent model's Operating | ||
| Unit, specify the relational field name to the parent and the parent's | ||
| operating unit field name separated by a dot: | ||
|
|
||
| .. code:: python | ||
|
|
||
| from odoo import fields, models | ||
|
|
||
| class SaleOrderLine(models.Model): | ||
| _inherit = "sale.order.line" | ||
|
|
||
| # This will restrict selectable products to those matching the | ||
| # 'operating_unit_id' on the parent 'sale.order' (linked via order_id) | ||
| product_id = fields.Many2one( | ||
| "product.product", | ||
| string="Product", | ||
| operating_unit_isolation="order_id.operating_unit_id" | ||
| ) | ||
|
|
||
| Technical Details | ||
| ----------------- | ||
|
|
||
| - **Frontend**: Overrides the base OWL ``Field`` component's props to | ||
| pass the ``record`` and ``parent_record`` along with their | ||
| ``operating_unit_id`` into the RPC context. | ||
| - **Backend**: Safely monkey-patches ``models.BaseModel._search`` to | ||
| intercept the domain and append an operating unit restriction if the | ||
| targeted field specifies the ``operating_unit_isolation`` property. | ||
|
|
||
| - **Dynamic Field Detection**: Automatically detects if the target | ||
| model uses a Many2one (``operating_unit_id``) or Many2many | ||
| (``operating_unit_ids``) field for its operating unit. | ||
| - **Empty Value Handling**: Explicitly includes an ``OR`` condition | ||
| allowing records with no assigned operating unit (``False``) to be | ||
| universally selectable. | ||
| - **Odoo 18 Compatibility Fallback**: Automatically applies the | ||
| ``order_id.operating_unit_id`` isolation for ``product_template_id`` | ||
| and ``product_id`` on the ``sale.order.line`` model natively, | ||
| without requiring the property to be explicitly defined on the | ||
| field. | ||
|
|
||
| Bug Tracker | ||
| =========== | ||
|
|
||
| Bugs are tracked on `GitHub Issues <https://github.com/OCA/operating-unit/issues>`_. | ||
| In case of trouble, please check there if your issue has already been reported. | ||
| If you spotted it first, help us to smash it by providing a detailed and welcomed | ||
| `feedback <https://github.com/OCA/operating-unit/issues/new?body=module:%20operating_unit_isolation%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_. | ||
|
|
||
| Do not contact contributors directly about support or help with technical issues. | ||
|
|
||
| Credits | ||
| ======= | ||
|
|
||
| Authors | ||
| ------- | ||
|
|
||
| * CIT Services | ||
|
|
||
| Contributors | ||
| ------------ | ||
|
|
||
| - Jordi Riera | ||
| - Luc De Meyer <luc.demeyer@noviat.com> | ||
| - Solomon Prabu <s.prabu@cit-services.eu> | ||
|
|
||
| Maintainers | ||
| ----------- | ||
|
|
||
| This module is maintained by the OCA. | ||
|
|
||
| .. image:: https://odoo-community.org/logo.png | ||
| :alt: Odoo Community Association | ||
| :target: https://odoo-community.org | ||
|
|
||
| OCA, or the Odoo Community Association, is a nonprofit organization whose | ||
| mission is to support the collaborative development of Odoo features and | ||
| promote its widespread use. | ||
|
|
||
| This module is part of the `OCA/operating-unit <https://github.com/OCA/operating-unit/tree/18.0/operating_unit_isolation>`_ project on GitHub. | ||
|
|
||
| You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from . import models |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Copyright 2016-2026 CIT Services | ||
| # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
|
||
| { | ||
| "name": "Operating Unit Isolation", | ||
| "summary": "An extension to provide isolation for operating units", | ||
| "version": "18.0.1.0.0", | ||
| "author": "CIT Services, Odoo Community Association (OCA)", | ||
| "company": "CIT Services", | ||
| "website": "https://github.com/OCA/operating-unit", | ||
| "category": "Generic", | ||
| "depends": ["base", "operating_unit"], | ||
| "data": [], | ||
| "assets": { | ||
| "web.assets_backend": [ | ||
| "operating_unit_isolation/static/src/js/operating_unit_isolation.esm.js" | ||
| ], | ||
| }, | ||
| "license": "AGPL-3", | ||
| "installable": True, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from . import base_operating_unit_isolation |
65 changes: 65 additions & 0 deletions
65
operating_unit_isolation/models/base_operating_unit_isolation.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # Copyright 2017-2026 CIT Services | ||
| # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
|
||
| from odoo import api, models | ||
|
|
||
|
|
||
| class BaseOperatingUnitIsolation(models.AbstractModel): | ||
| _name = "base_operating_unit_isolation" | ||
| _description = "Extend AbstractModel for Operation Unit Isolation" | ||
|
|
||
| def _register_hook(self): | ||
| res = super()._register_hook() | ||
| ou_patched = getattr(models.BaseModel, "ou_patched", False) | ||
| if not ou_patched: | ||
| origin_search = models.BaseModel._search | ||
|
|
||
| @api.model | ||
| def _ou_search(self, domain, *args, **kwargs): | ||
| record = self.env.context.get("record") | ||
|
|
||
| ou_field = False | ||
| if "operating_unit_id" in self._fields: | ||
| ou_field = "operating_unit_id" | ||
| elif "operating_unit_ids" in self._fields: | ||
| ou_field = "operating_unit_ids" | ||
|
|
||
| if ( | ||
| record | ||
| and ou_field | ||
| and record.get("_field") | ||
| and "_name" in record | ||
| and record["_name"] in self.env | ||
| and record["_field"] in self.env[record["_name"]]._fields | ||
| ): | ||
| fld = self.env[record["_name"]]._fields[record["_field"]] | ||
|
|
||
| isolation_val = getattr(fld, "operating_unit_isolation", False) | ||
| if not isolation_val: | ||
| parent_record = self.env.context.get("parent_record", {}) | ||
| isolation_val = parent_record.get("operating_unit_id.name") | ||
|
solomonprabu marked this conversation as resolved.
Outdated
|
||
|
|
||
| if isolation_val: | ||
| isolation = isolation_val.split(".") | ||
| filter_ou = False | ||
| if len(isolation) == 2: | ||
| parent_record = self.env.context.get("parent_record", {}) | ||
| filter_ou = parent_record.get(isolation[1]) | ||
| elif len(isolation) == 1: | ||
| filter_ou = record.get(isolation[0]) | ||
| if filter_ou: | ||
| domain = list(domain) if domain else [] | ||
| domain.extend( | ||
| [ | ||
| "|", | ||
| (ou_field, "=", False), | ||
| (ou_field, "in", [filter_ou]), | ||
| ] | ||
| ) | ||
|
|
||
| return origin_search(self, domain, *args, **kwargs) | ||
|
|
||
| models.BaseModel._search = _ou_search | ||
| models.BaseModel.ou_patched = True | ||
|
|
||
| return res | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [build-system] | ||
| requires = ["whool"] | ||
| build-backend = "whool.buildapi" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| - Jordi Riera | ||
| - Luc De Meyer \<luc.demeyer@noviat.com\> | ||
| - Solomon Prabu \<s.prabu@cit-services.eu\> | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| An extension to provide dynamic isolation for operating units across | ||
| models. | ||
|
|
||
| This module ensures that users can only select related records that | ||
| belong to the same Operating Unit as the current record or its parent | ||
| record. It achieves this by seamlessly injecting the current record and | ||
| parent record state from the frontend (Odoo 18 OWL framework) into the | ||
| backend `_search` operations. | ||
|
|
||
| When a user attempts to search or select a record for a relation (e.g., | ||
| in a dropdown autocomplete), this module will dynamically filter the | ||
| available options based on the `operating_unit_id` of the active record. | ||
|
|
||
| This isolation is triggered automatically for any relation field that | ||
| specifies the `operating_unit_isolation` attribute. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| To use this functionality in your own module, add the | ||
| `operating_unit_isolation` property to the field definitions in your | ||
| models. | ||
|
|
||
| **Isolating based on the current record's Operating Unit:** | ||
|
|
||
| If you have a field on a model and you want the selectable records to be | ||
| restricted to the same Operating Unit as the current record, use the | ||
| field name of the operating unit as the value: | ||
|
|
||
| ``` python | ||
| from odoo import fields, models | ||
|
|
||
| class CustomModel(models.Model): | ||
| _name = "custom.model" | ||
|
|
||
| operating_unit_id = fields.Many2one("operating.unit", "Operating Unit") | ||
|
|
||
| # This will restrict selectable partners to those matching the | ||
| # 'operating_unit_id' of this custom.model record (or those without an OU) | ||
| partner_id = fields.Many2one( | ||
| "res.partner", | ||
| string="Partner", | ||
| operating_unit_isolation="operating_unit_id" | ||
| ) | ||
| ``` | ||
|
|
||
| **Isolating based on a parent record's Operating Unit (One2many | ||
| context):** | ||
|
|
||
| If you are working within a One2many line (e.g., `sale.order.line`) and | ||
| want to filter the relation based on the parent model's Operating Unit, | ||
| specify the relational field name to the parent and the parent's | ||
| operating unit field name separated by a dot: | ||
|
|
||
| ``` python | ||
| from odoo import fields, models | ||
|
|
||
| class SaleOrderLine(models.Model): | ||
| _inherit = "sale.order.line" | ||
|
|
||
| # This will restrict selectable products to those matching the | ||
| # 'operating_unit_id' on the parent 'sale.order' (linked via order_id) | ||
| product_id = fields.Many2one( | ||
| "product.product", | ||
| string="Product", | ||
| operating_unit_isolation="order_id.operating_unit_id" | ||
| ) | ||
| ``` | ||
|
|
||
| ## Technical Details | ||
|
|
||
| - **Frontend**: Overrides the base OWL `Field` component's props to pass | ||
| the `record` and `parent_record` along with their `operating_unit_id` | ||
| into the RPC context. | ||
| - **Backend**: Safely monkey-patches `models.BaseModel._search` to | ||
| intercept the domain and append an operating unit restriction if | ||
| the targeted field specifies the `operating_unit_isolation` property. | ||
| - **Dynamic Field Detection**: Automatically detects if the target model | ||
| uses a Many2one (`operating_unit_id`) or Many2many (`operating_unit_ids`) | ||
| field for its operating unit. | ||
| - **Empty Value Handling**: Explicitly includes an `OR` condition allowing | ||
| records with no assigned operating unit (`False`) to be universally | ||
| selectable. | ||
| - **Odoo 18 Compatibility Fallback**: Automatically applies the | ||
| `order_id.operating_unit_id` isolation for `product_template_id` and | ||
| `product_id` on the `sale.order.line` model natively, without requiring | ||
| the property to be explicitly defined on the field. | ||
|
solomonprabu marked this conversation as resolved.
Outdated
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.