Skip to content

Commit

Permalink
[16.0][MIG] base_partition
Browse files Browse the repository at this point in the history
  • Loading branch information
glitchov committed Apr 25, 2023
1 parent aaec909 commit e07142b
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 271 deletions.
26 changes: 12 additions & 14 deletions base_partition/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ Base Partition
.. |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
.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github
:target: https://github.com/OCA/server-tools/tree/12.0/base_partition
:target: https://github.com/OCA/server-tools/tree/16.0/base_partition
: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-12-0/server-tools-12-0-base_partition
:target: https://translation.odoo-community.org/projects/server-tools-16-0/server-tools-16-0-base_partition
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/149/12.0
:alt: Try me on Runbot
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/webui/builds.html?repo=OCA/server-tools&target_branch=16.0
:alt: Try me on Runboat

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

Expand All @@ -35,9 +35,6 @@ and with values that are recordsets
So if we have a recordset (x | y | z ) such that x.f == True, y.f == z.f == False,
then (x | y | z ).partition("f") == {True: x, False: (y | z)}.

It also provides a backport of `filtered_domain`,
which filters a recordset in place with a provided domain.

**Table of contents**

.. contents::
Expand All @@ -49,7 +46,7 @@ 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 smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20base_partition%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20base_partition%0Aversion:%2016.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.

Expand All @@ -59,12 +56,13 @@ Credits
Authors
~~~~~~~

* Acsone
* Acsone SA/NV

Contributors
~~~~~~~~~~~~

* Nans Lefebvre <[email protected]>
* Hughes Damry <[email protected]>

Maintainers
~~~~~~~~~~~
Expand All @@ -79,6 +77,6 @@ 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/server-tools <https://github.com/OCA/server-tools/tree/12.0/base_partition>`_ project on GitHub.
This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/16.0/base_partition>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
8 changes: 4 additions & 4 deletions base_partition/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Copyright 2020 Acsone (http://www.acsone.eu)
# Nans Lefebvre <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

{
"name": "Base Partition",
"summary": "Base module that provide the partition method on all models",
"version": "12.0.1.0.0",
"version": "16.0.1.0.0",
"category": "Uncategorized",
"website": "https://github.com/OCA/server-tools",
"author": "Acsone, Odoo Community Association (OCA)",
"license": "AGPL-3",
"author": "Acsone SA/NV, Odoo Community Association (OCA)",
"license": "LGPL-3",
"installable": True,
"depends": ["base"],
}
18 changes: 13 additions & 5 deletions base_partition/i18n/base_partition.pot
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * base_partition
# * base_partition
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 12.0\n"
"Project-Id-Version: Odoo Server 16.0+e\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-09-04 13:29+0000\n"
"PO-Revision-Date: 2020-09-04 13:29+0000\n"
"Last-Translator: <>\n"
"POT-Creation-Date: 2023-04-24 15:38+0000\n"
"PO-Revision-Date: 2023-04-24 15:38+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
Expand All @@ -20,3 +20,11 @@ msgstr ""
msgid "Base"
msgstr ""

#. module: base_partition
#. odoo-python
#: code:addons/base_partition/models/models.py:0
#, python-format
msgid ""
"Either set up a '_default_batch_size' on the model or provide a batch_size "
"parameter."
msgstr ""
32 changes: 32 additions & 0 deletions base_partition/i18n/fr_BE.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * base_partition
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 16.0+e\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-04-24 15:38+0000\n"
"PO-Revision-Date: 2023-04-24 15:38+0000\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"

#. module: base_partition
#: model:ir.model,name:base_partition.model_base
msgid "Base"
msgstr ""

#. module: base_partition
#. odoo-python
#: code:addons/base_partition/models/models.py:0
#, python-format
msgid ""
"Either set up a '_default_batch_size' on the model or provide a batch_size "
"parameter."
msgstr ""
"Définir '_default_batch_size' sur le modèle ou fournir une valeur de "
"batch_size en paramètre."
140 changes: 11 additions & 129 deletions base_partition/models/models.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
# © 2020 Acsone (http://www.acsone.eu)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

import fnmatch

from odoo import _, fields, models
from odoo import _, models
from odoo.exceptions import UserError
from odoo.osv import expression

LIKE_COMPARATORS = (
"like",
"ilike",
"=like",
"=ilike",
"not ilike",
"not like",
)


class Base(models.AbstractModel):
Expand All @@ -23,15 +11,15 @@ class Base(models.AbstractModel):

def partition(self, accessor):
"""Returns a dictionary forming a partition of self into a dictionary
value/recordset for each value obtained from the accessor.
The accessor itself can be either a string that can be passed to mapped,
or an arbitrary function.
Note that it is always at least as fast to pass a function,
hence the current implementation.
If we have a 'field.subfield' accessor such that subfield is not a relational
then the result is a list (not hashable). Then the str(key) are used.
In the general case a value could both not be hashable nor stringifiable,
in a which case this function would crash.
value/recordset for each value obtained from the accessor.
The accessor itself can be either a string that can be passed to mapped,
or an arbitrary function.
Note that it is always at least as fast to pass a function,
hence the current implementation.
If we have a 'field.subfield' accessor such that subfield is not a relational
then the result is a list (not hashable). Then the str(key) are used.
In the general case a value could both not be hashable nor stringifiable,
in a which case this function would crash.
"""
partition = {}

Expand Down Expand Up @@ -74,109 +62,3 @@ def read_per_record(self, fields=None, load="_classic_read"):
key = d.pop("id")
result[key] = d
return result

def filtered_domain(self, domain):
"""Backport from standard.
"""
if not domain:
return self
result = []
for d in reversed(domain):
if d == "|":
result.append(result.pop() | result.pop())
elif d == "!":
result.append(self - result.pop())
elif d == "&":
result.append(result.pop() & result.pop())
elif d == expression.TRUE_LEAF:
result.append(self)
elif d == expression.FALSE_LEAF:
result.append(self.browse())
else:
(key, comparator, value) = d
if key.endswith(".id"):
key = key[:-3]
if key == "id":
key = ""
# determine the field with the final type for values
field = None
if key:
model = self.browse()
for fname in key.split("."):
field = model._fields[fname]
model = model[fname]

if comparator in LIKE_COMPARATORS:
value_esc = (
value.replace("_", "?").replace("%", "*").replace("[", "?")
)
records = self.browse()
for rec in self:
data = rec.mapped(key)
if comparator in ("child_of", "parent_of"):
records = data.search([(data._parent_name, comparator, value)])
value = records.ids
comparator = "in"
if isinstance(data, models.BaseModel):
v = value
if isinstance(value, (list, tuple)) and len(value):
v = value[0]
if isinstance(v, str):
data = data.mapped("display_name")
else:
data = data.ids if data else [False]
elif field and field.type in ("date", "datetime"):
# convert all date and datetime values to datetime
normalize = fields.Datetime.to_datetime
if isinstance(value, (list, tuple)):
value = [normalize(v) for v in value]
else:
value = normalize(value)
data = [normalize(d) for d in data]
if comparator in ("in", "not in"):
if not (isinstance(value, list) or isinstance(value, tuple)):
value = [value]

if comparator == "=":
ok = value in data
elif comparator == "in":
ok = any(map(lambda x: x in data, value))
elif comparator == "<":
ok = any(map(lambda x: x is not None and x < value, data))
elif comparator == ">":
ok = any(map(lambda x: x is not None and x > value, data))
elif comparator == "<=":
ok = any(map(lambda x: x is not None and x <= value, data))
elif comparator == ">=":
ok = any(map(lambda x: x is not None and x >= value, data))
elif comparator in ("!=", "<>"):
ok = value not in data
elif comparator == "not in":
ok = all(map(lambda x: x not in data, value))
elif comparator == "not ilike":
ok = all(map(lambda x: value.lower() not in x.lower(), data))
elif comparator == "ilike":
data = [x.lower() for x in data]
match = fnmatch.filter(
data, "*" + (value_esc or "").lower() + "*"
)
ok = bool(match)
elif comparator == "not like":
ok = all(map(lambda x: value not in x, data))
elif comparator == "like":
ok = bool(fnmatch.filter(data, value and "*" + value_esc + "*"))
elif comparator == "=?":
ok = (value in data) or not value
elif comparator == "=like":
ok = bool(fnmatch.filter(data, value_esc))
elif comparator == "=ilike":
data = [x.lower() for x in data]
ok = bool(fnmatch.filter(data, value and value_esc.lower()))
else:
raise ValueError
if ok:
records |= rec
result.append(records)
while len(result) > 1:
result.append(result.pop() & result.pop())
return result[0]
1 change: 1 addition & 0 deletions base_partition/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* Nans Lefebvre <[email protected]>
* Hughes Damry <[email protected]>
3 changes: 0 additions & 3 deletions base_partition/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,3 @@ and with values that are recordsets

So if we have a recordset (x | y | z ) such that x.f == True, y.f == z.f == False,
then (x | y | z ).partition("f") == {True: x, False: (y | z)}.

It also provides a backport of `filtered_domain`,
which filters a recordset in place with a provided domain.
13 changes: 6 additions & 7 deletions base_partition/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.15.1: http://docutils.sourceforge.net/" />
<meta name="generator" content="Docutils: http://docutils.sourceforge.net/" />
<title>Base Partition</title>
<style type="text/css">

Expand Down Expand Up @@ -367,7 +367,7 @@ <h1 class="title">Base Partition</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/server-tools/tree/12.0/base_partition"><img alt="OCA/server-tools" src="https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/server-tools-12-0/server-tools-12-0-base_partition"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/149/12.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/server-tools/tree/16.0/base_partition"><img alt="OCA/server-tools" src="https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/server-tools-16-0/server-tools-16-0-base_partition"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runboat.odoo-community.org/webui/builds.html?repo=OCA/server-tools&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module adds a <cite>partition(self, accessor)</cite> method to every model.
It accepts for accessor any parameter that would be accepted by <cite>mapped</cite>,
i.e. a string <cite>“field(.subfield)*”</cite> or a function <cite>(lambda x: not x.b)</cite>.
Expand All @@ -376,8 +376,6 @@ <h1 class="title">Base Partition</h1>
(these recordsets forming a partition of the initial recordset, conveniently).</p>
<p>So if we have a recordset (x | y | z ) such that x.f == True, y.f == z.f == False,
then (x | y | z ).partition(“f”) == {True: x, False: (y | z)}.</p>
<p>It also provides a backport of <cite>filtered_domain</cite>,
which filters a recordset in place with a provided domain.</p>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
Expand All @@ -395,21 +393,22 @@ <h1><a class="toc-backref" href="#id1">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/server-tools/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/server-tools/issues/new?body=module:%20base_partition%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/OCA/server-tools/issues/new?body=module:%20base_partition%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
<h1><a class="toc-backref" href="#id2">Credits</a></h1>
<div class="section" id="authors">
<h2><a class="toc-backref" href="#id3">Authors</a></h2>
<ul class="simple">
<li>Acsone</li>
<li>Acsone SA/NV</li>
</ul>
</div>
<div class="section" id="contributors">
<h2><a class="toc-backref" href="#id4">Contributors</a></h2>
<ul class="simple">
<li>Nans Lefebvre &lt;<a class="reference external" href="mailto:nans.lefebvre&#64;acsone.eu">nans.lefebvre&#64;acsone.eu</a>&gt;</li>
<li>Hughes Damry &lt;<a class="reference external" href="mailto:hughes.damry&#64;acsone.eu">hughes.damry&#64;acsone.eu</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand All @@ -419,7 +418,7 @@ <h2><a class="toc-backref" href="#id5">Maintainers</a></h2>
<p>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.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/server-tools/tree/12.0/base_partition">OCA/server-tools</a> project on GitHub.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/server-tools/tree/16.0/base_partition">OCA/server-tools</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
</div>
Expand Down
Loading

0 comments on commit e07142b

Please sign in to comment.