Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8107893
[ADD] base_partition: add partition method to base model
Sep 3, 2020
cce4e09
[IMP] models: backport filtered_domain
Sep 7, 2020
a4315a4
[IMP] base_partition: add batch method on base
Sep 8, 2020
858643e
[IMP] base_partition: add read_per_record method
Oct 11, 2020
df1e578
[TEST] base_partition: add coverage
Oct 11, 2020
1303cda
[16.0][MIG] base_partition
glitchov Apr 24, 2023
67af1d8
[UPD] Update base_partition.pot
Jul 18, 2023
34cdca4
[UPD] README.rst
OCA-git-bot Jul 18, 2023
f315906
[UPD] README.rst
OCA-git-bot Sep 3, 2023
bda56dc
Added translation using Weblate (Spanish)
Ivorra78 Dec 7, 2023
aac8aee
Translated using Weblate (Spanish)
Ivorra78 Dec 7, 2023
f755c7c
Added translation using Weblate (Italian)
mymage Jan 2, 2024
af4ccfe
Translated using Weblate (Italian)
mymage Jan 2, 2024
4f1ddaa
[IMP] base_partition: pre-commit auto fixes
nguyenminhchien Feb 28, 2024
6223f2e
[MIG] base_partition: Migration to 17.0
nguyenminhchien Feb 28, 2024
19f7b1c
[UPD] Update base_partition.pot
Apr 23, 2024
3bd7ca2
[BOT] post-merge updates
OCA-git-bot Apr 23, 2024
c2a186c
Added translation using Weblate (Chinese (Simplified) (zh_CN))
xtanuiha Jun 16, 2024
690beac
Translated using Weblate (Chinese (Simplified) (zh_CN))
xtanuiha Jun 16, 2024
c9e63f9
[MIG] base_partition: Migration to 18.0
Oct 15, 2024
06d38e4
[UPD] Update base_partition.pot
Nov 21, 2024
632b48a
[BOT] post-merge updates
OCA-git-bot Nov 21, 2024
bbbb26e
Update translation files
weblate Nov 21, 2024
8b0c8f0
[MIG] base_partition: Migration to 19.0
therbin200 Dec 12, 2025
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
90 changes: 90 additions & 0 deletions base_partition/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
.. image:: https://odoo-community.org/readme-banner-image
:target: https://odoo-community.org/get-involved?utm_source=readme
:alt: Odoo Community Association

==============
Base Partition
==============

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

.. |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/license-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/19.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-19-0/server-tools-19-0-base_partition
: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=19.0
:alt: Try me on Runboat

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

This module adds a partition(self, accessor) method to every model. It
accepts for accessor any parameter that would be accepted by mapped,
i.e. a string "field(.subfield)\*" or a function (lambda x: not x.b). It
returns a dictionary with keys that are equal to
set(record.mapped(accessor)), and with values that are recordsets (these
recordsets forming a partition of the initial recordset, conveniently).

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)}.

**Table of contents**

.. contents::
:local:

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_partition%0Aversion:%2019.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
-------

* Acsone SA/NV

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

- Nans Lefebvre <nans.lefebvre@acsone.eu>
- Hughes Damry <hughes.damry@acsone.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/server-tools <https://github.com/OCA/server-tools/tree/19.0/base_partition>`_ 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_partition/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
15 changes: 15 additions & 0 deletions base_partition/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2020 Acsone (http://www.acsone.eu)
# Nans Lefebvre <nans.lefebvre@acsone.eu>
# 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": "19.0.1.0.0",
"category": "Uncategorized",
"website": "https://github.com/OCA/server-tools",
"author": "Acsone SA/NV, Odoo Community Association (OCA)",
"license": "LGPL-3",
"installable": True,
"depends": ["base"],
}
27 changes: 27 additions & 0 deletions base_partition/i18n/base_partition.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * base_partition
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 18.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"

#. 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
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/es.po
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\n"
"Report-Msgid-Bugs-To: \n"
"PO-Revision-Date: 2023-12-07 18:34+0000\n"
"Last-Translator: Ivorra78 <informatica@totmaterial.es>\n"
"Language-Team: none\n"
"Language: es\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"
"X-Generator: Weblate 4.17\n"

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

#. module: base_partition
#. odoo-python
#: code:addons/base_partition/models/models.py:0
msgid ""
"Either set up a '_default_batch_size' on the model or provide a batch_size "
"parameter."
msgstr ""
"Configure un '_default_batch_size' en el modelo o proporcione un parámetro "
"de tamaño de lote."
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."
32 changes: 32 additions & 0 deletions base_partition/i18n/it.po
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\n"
"Report-Msgid-Bugs-To: \n"
"PO-Revision-Date: 2024-01-02 16:34+0000\n"
"Last-Translator: mymage <stefano.consolaro@mymage.it>\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"
"X-Generator: Weblate 4.17\n"

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

#. module: base_partition
#. odoo-python
#: code:addons/base_partition/models/models.py:0
msgid ""
"Either set up a '_default_batch_size' on the model or provide a batch_size "
"parameter."
msgstr ""
"Impostare un '_default_batch_size' nel modello o frnire un parametro "
"batch_size."
31 changes: 31 additions & 0 deletions base_partition/i18n/zh_CN.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * base_partition
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 17.0\n"
"Report-Msgid-Bugs-To: \n"
"PO-Revision-Date: 2024-06-16 17:40+0000\n"
"Last-Translator: xtanuiha <feihu.zhang@live.com>\n"
"Language-Team: none\n"
"Language: zh_CN\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: nplurals=1; plural=0;\n"
"X-Generator: Weblate 4.17\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
msgid ""
"Either set up a '_default_batch_size' on the model or provide a batch_size "
"parameter."
msgstr ""
"要么在模型上设置一个'_default_batch_size',要么提供一个batch_size参数。"
1 change: 1 addition & 0 deletions base_partition/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
63 changes: 63 additions & 0 deletions base_partition/models/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# © 2020 Acsone (http://www.acsone.eu)
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

from odoo import models
from odoo.exceptions import UserError


class Base(models.AbstractModel):
_inherit = "base"

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.
"""
partition = {}

if isinstance(accessor, str):
if "." not in accessor:
func = lambda r: r[accessor] # noqa: E731
else:
func = lambda r: r.mapped(accessor) # noqa: E731
else:
func = accessor

for record in self:
key = func(record)
if not key.__hash__:
key = str(key)
if key not in partition:
partition[key] = record
else:
partition[key] += record

return partition

def batch(self, batch_size=None):
"""Yield successive batches of size batch_size, or ."""
if not (batch_size or "_default_batch_size" in dir(self)):
raise UserError(
self.env._(
"Either set up a '_default_batch_size' on the model"
" or provide a batch_size parameter."
)
)
batch_size = batch_size or self._default_batch_size
for i in range(0, len(self), batch_size):
yield self[i : i + batch_size]
Comment on lines +44 to +55
Copy link
Contributor

@rrebollo rrebollo Dec 13, 2025

Choose a reason for hiding this comment

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

It would be nice include some documentation in addon's description about this method too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Recently I discovered split_every. Maybe this could be deprecated in favor of the built-in helper.


def read_per_record(self, fields=None, load="_classic_read"):
result = {}
data_list = self.read(fields=fields, load=load)
for d in data_list:
key = d.pop("id")
result[key] = d
return result
Comment on lines +57 to +63
Copy link
Contributor

Choose a reason for hiding this comment

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

Idem

3 changes: 3 additions & 0 deletions base_partition/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
2 changes: 2 additions & 0 deletions base_partition/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Nans Lefebvre \<<nans.lefebvre@acsone.eu>\>
- Hughes Damry \<<hughes.damry@acsone.eu>\>
10 changes: 10 additions & 0 deletions base_partition/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
This module adds a partition(self, accessor) method to every model. It
accepts for accessor any parameter that would be accepted by mapped,
i.e. a string "field(.subfield)\*" or a function (lambda x: not x.b). It
returns a dictionary with keys that are equal to
set(record.mapped(accessor)), and with values that are recordsets (these
recordsets forming a partition of the initial recordset, conveniently).

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)}.
Binary file added base_partition/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading