Skip to content
Draft
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
123 changes: 123 additions & 0 deletions base_domain_inverse_function/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
============================
Base Domain Inverse Function
============================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:560316908c1bd487469324c2345197a83a48235ba11384b655e7f0a0b87b5824
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png
:target: https://odoo-community.org/page/development-status
:alt: Alpha
.. |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%2Fserver--tools-lightgray.png?logo=github
:target: https://github.com/OCA/server-tools/tree/18.0/base_domain_inverse_function
:alt: OCA/server-tools
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-tools-18-0/server-tools-18-0-base_domain_inverse_function
: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/server-tools&target_branch=18.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module provides functions to decompose normalized domains into
domains operands, as these functions are the inverse of AND and OR
functions available in odoo.osv.expression.

.. IMPORTANT::
This is an alpha version, the data model and design can change at any time without warning.
Only for development or testing purpose, do not use in production.
`More details on development status <https://odoo-community.org/page/development-status>`_

**Table of contents**

.. contents::
:local:

Usage
=====

If you have to decompose a complex domain to inject some conditions,
this shows what you can do:

.. code:: python

from odoo.osv.expression import AND, OR
from odoo.addons.base_domain_inverse_function.expression import inverse_AND, inverse_OR

domain = AND([d1, d2, d3])
d1, d2, d3 = inverse_AND(domain)

domain = OR([d1, d2, d3])
d1, d2, d3 = inverse_OR(domain)

Known issues / Roadmap
======================

- Allow to inverse domains containing NOT '!' operator

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/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/server-tools/issues/new?body=module:%20base_domain_inverse_function%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
-------

* Camptocamp

Contributors
------------

- Akim Juillerat <akim.juillerat@camptocamp.com>

- `Trobz <https://trobz.com>`__:

- Son Ho <sonhd@trobz.com>

Other credits
-------------

The migration of this module from 13.0 to 16.0 was financially supported
by Camptocamp

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.

.. |maintainer-grindtildeath| image:: https://github.com/grindtildeath.png?size=40px
:target: https://github.com/grindtildeath
:alt: grindtildeath

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-grindtildeath|

This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/18.0/base_domain_inverse_function>`_ 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 base_domain_inverse_function/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import inverse_expression
18 changes: 18 additions & 0 deletions base_domain_inverse_function/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2022 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
{
"name": "Base Domain Inverse Function",
"summary": "Provide function to inverse domain into parts",
"version": "18.0.1.0.1",
"development_status": "Alpha",
"category": "Others",
"website": "https://github.com/OCA/server-tools",
"author": "Camptocamp, Odoo Community Association (OCA)",
"maintainers": ["grindtildeath"],
"license": "AGPL-3",
"application": False,
"installable": True,
"depends": [
"base",
],
}
13 changes: 13 additions & 0 deletions base_domain_inverse_function/i18n/base_domain_inverse_function.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 16.0\n"
"Report-Msgid-Bugs-To: \n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"
14 changes: 14 additions & 0 deletions base_domain_inverse_function/i18n/it.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 16.0\n"
"Report-Msgid-Bugs-To: \n"
"Last-Translator: Automatically generated\n"
"Language-Team: none\n"
"Language: it\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
92 changes: 92 additions & 0 deletions base_domain_inverse_function/inverse_expression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Copyright 2022 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
from odoo.osv.expression import (
AND,
AND_OPERATOR,
DOMAIN_OPERATORS,
NOT_OPERATOR,
OR,
OR_OPERATOR,
)


def inverse_combine(domain, operator):
"""Decompose normalized domain into operands according to operator

The result can be then altered easily to inject domain operands before
rebuilding a new domain using the corresponding function from osv.expression.

:param domain: A normalized domain
:param operator: The "main" domain operator of the domain being either '&' or '|'
(must be the first operator in a normalized domain)

:return list: A list of domains
"""
if operator not in DOMAIN_OPERATORS:
raise Exception("Unsupported operator parameter: %s" % operator)
operator_func = {AND_OPERATOR: AND, OR_OPERATOR: OR}
other_operator = OR_OPERATOR if operator == AND_OPERATOR else AND_OPERATOR
result = []
operator_elements_stack = []
other_elements_stack = []
elements_stack = []

last_element = False

# 1. Loop over the domain in reverse order
for element in reversed(domain):
if element == NOT_OPERATOR:
raise Exception(
"Inversing domains including NOT operator ('!') is not supported"
)
if element in DOMAIN_OPERATORS:
# 3. When we reach an operator:
# - pop the last item from the element stack to the corresponding operator stack
# - if such stack contains only one element, the actual operator applies to the two
# last items in the elements stack, so pop the penultimate item as well
if element != operator:
if len(elements_stack) > 0:
other_elements_stack.append([elements_stack.pop()])
if (
len(other_elements_stack) == 1
and last_element not in DOMAIN_OPERATORS
):
other_elements_stack.append([elements_stack.pop()])
else:
if len(elements_stack) > 0:
operator_elements_stack.append([elements_stack.pop()])
if (
len(operator_elements_stack) == 1
and last_element not in DOMAIN_OPERATORS
):
operator_elements_stack.append([elements_stack.pop()])
last_element = element
else:
# 4. If actual element is a tuple, but last element was an operator, empty the
# corresponding operator stack into the result
if last_element in DOMAIN_OPERATORS:
if last_element != operator:
result.append(operator_func[last_element](other_elements_stack))
other_elements_stack = []
else:
# TODO: Add tests to cover these lines (and eventually fix these)
result.append(operator_func[last_element](operator_elements_stack))
operator_elements_stack = []
# 2. Add any tuple element to the stack
elements_stack.append(element)
last_element = element
# 5. Empty operators stack when reaching the end
if operator_elements_stack:
operator_elements_stack.extend(result)
result = operator_elements_stack
elif other_elements_stack:
result.append(operator_func[other_operator](other_elements_stack))
return result


def inverse_OR(domain):
return inverse_combine(domain, OR_OPERATOR)


def inverse_AND(domain):
return inverse_combine(domain, AND_OPERATOR)
7 changes: 7 additions & 0 deletions base_domain_inverse_function/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
name = "odoo-addon-base-domain-inverse-function"
version = "18.0.1.0.0"

[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
5 changes: 5 additions & 0 deletions base_domain_inverse_function/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- Akim Juillerat \<<akim.juillerat@camptocamp.com>\>

- [Trobz](https://trobz.com):

> - Son Ho \<<sonhd@trobz.com>\>
2 changes: 2 additions & 0 deletions base_domain_inverse_function/readme/CREDITS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The migration of this module from 13.0 to 16.0 was financially supported
by Camptocamp
3 changes: 3 additions & 0 deletions base_domain_inverse_function/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This module provides functions to decompose normalized domains into
domains operands, as these functions are the inverse of AND and OR
functions available in odoo.osv.expression.
1 change: 1 addition & 0 deletions base_domain_inverse_function/readme/ROADMAP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Allow to inverse domains containing NOT '!' operator
13 changes: 13 additions & 0 deletions base_domain_inverse_function/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
If you have to decompose a complex domain to inject some conditions,
this shows what you can do:

``` python
from odoo.osv.expression import AND, OR
from odoo.addons.base_domain_inverse_function.expression import inverse_AND, inverse_OR

domain = AND([d1, d2, d3])
d1, d2, d3 = inverse_AND(domain)

domain = OR([d1, d2, d3])
d1, d2, d3 = inverse_OR(domain)
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading