Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions operating_unit_isolation/README.rst
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.
1 change: 1 addition & 0 deletions operating_unit_isolation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
21 changes: 21 additions & 0 deletions operating_unit_isolation/__manifest__.py
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,
}
1 change: 1 addition & 0 deletions operating_unit_isolation/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import base_operating_unit_isolation
71 changes: 71 additions & 0 deletions operating_unit_isolation/models/base_operating_unit_isolation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# 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"]]

# For Odoo 18 compatibility, if the field is product_template_id and
# it's on sale.order.line, we can fallback to
# order_id.operating_unit_id if isolation isn't explicitly defined.
isolation_val = getattr(fld, "operating_unit_isolation", False)
if (
not isolation_val
and record["_name"] == "sale.order.line"
and record["_field"] in ("product_id", "product_template_id")
):
isolation_val = "order_id.operating_unit_id"

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
3 changes: 3 additions & 0 deletions operating_unit_isolation/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
4 changes: 4 additions & 0 deletions operating_unit_isolation/readme/CONTRIBUTORS.md
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\>

15 changes: 15 additions & 0 deletions operating_unit_isolation/readme/DESCRIPTION.md
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.
68 changes: 68 additions & 0 deletions operating_unit_isolation/readme/USAGE.md
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.
Loading
Loading