From 9a46507ca7d4f335ea89926eb1c88f2826588433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20BEAU?= Date: Wed, 2 Aug 2017 19:28:04 +0200 Subject: [PATCH 1/9] [ADD] compute_field_after_install --- compute_field_after_install/README.rst | 95 ++++ compute_field_after_install/__init__.py | 1 + compute_field_after_install/__manifest__.py | 31 ++ compute_field_after_install/data/ir_cron.xml | 17 + .../models/__init__.py | 1 + .../models/recompute_field.py | 99 ++++ .../readme/CONFIGURATION.rst | 9 + .../readme/CONTRIBUTORS.rst | 1 + .../readme/DESCRIPTION.rst | 5 + compute_field_after_install/readme/USAGE.rst | 3 + .../security/ir.model.access.csv | 2 + .../static/description/index.html | 432 ++++++++++++++++++ compute_field_after_install/tests/__init__.py | 1 + .../tests/test_recompute.py | 51 +++ .../views/recompute_field_view.xml | 75 +++ 15 files changed, 823 insertions(+) create mode 100644 compute_field_after_install/README.rst create mode 100644 compute_field_after_install/__init__.py create mode 100644 compute_field_after_install/__manifest__.py create mode 100644 compute_field_after_install/data/ir_cron.xml create mode 100644 compute_field_after_install/models/__init__.py create mode 100644 compute_field_after_install/models/recompute_field.py create mode 100644 compute_field_after_install/readme/CONFIGURATION.rst create mode 100644 compute_field_after_install/readme/CONTRIBUTORS.rst create mode 100644 compute_field_after_install/readme/DESCRIPTION.rst create mode 100644 compute_field_after_install/readme/USAGE.rst create mode 100644 compute_field_after_install/security/ir.model.access.csv create mode 100644 compute_field_after_install/static/description/index.html create mode 100644 compute_field_after_install/tests/__init__.py create mode 100644 compute_field_after_install/tests/test_recompute.py create mode 100644 compute_field_after_install/views/recompute_field_view.xml diff --git a/compute_field_after_install/README.rst b/compute_field_after_install/README.rst new file mode 100644 index 00000000000..10798d7aecf --- /dev/null +++ b/compute_field_after_install/README.rst @@ -0,0 +1,95 @@ +================================ +Base compute field after install +================================ + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:26bad7f441b66e0de5861db7e578a3651b9e17b90748e4ca3fe6ba91025751ee + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fserver--tools-lightgray.png?logo=github + :target: https://github.com/OCA/server-tools/tree/14.0/base_compute_field_after_install + :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-14-0/server-tools-14-0-base_compute_field_after_install + :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=14.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Computed field can be really long to be computed when installing a module and so block +the migration process during a long time. + +This module give the possibility to compute +the field added after the installation + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +Just install any module with computed field. + +Database is available quickly whatever its size and computed fields. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub 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 `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Akretion + +Contributors +~~~~~~~~~~~~ + +* Sébastien BEAU + +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-sebastienbeau| image:: https://github.com/sebastienbeau.png?size=40px + :target: https://github.com/sebastienbeau + :alt: sebastienbeau + +Current `maintainer `__: + +|maintainer-sebastienbeau| + +This module is part of the `OCA/server-tools `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/compute_field_after_install/__init__.py b/compute_field_after_install/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/compute_field_after_install/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/compute_field_after_install/__manifest__.py b/compute_field_after_install/__manifest__.py new file mode 100644 index 00000000000..25380213050 --- /dev/null +++ b/compute_field_after_install/__manifest__.py @@ -0,0 +1,31 @@ +# Copyright 2016 Akretion (http://www.akretion.com) +# Sébastien BEAU +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +{ + "name": "Base compute field after install", + "summary": "Computed field are computed after the install process", + "version": "14.0.1.0.0", + "category": "e-commerce", + "website": "https://github.com/OCA/server-tools", + "author": "Akretion,Odoo Community Association (OCA)", + "maintainers": ["sebastienbeau"], + "license": "AGPL-3", + "application": False, + "installable": True, + "auto_install": False, + "external_dependencies": { + "python": [], + "bin": [], + }, + "depends": [ + "base", + ], + "data": [ + "data/ir_cron.xml", + "views/recompute_field_view.xml", + "security/ir.model.access.csv", + ], + "demo": [], + "qweb": [], +} diff --git a/compute_field_after_install/data/ir_cron.xml b/compute_field_after_install/data/ir_cron.xml new file mode 100644 index 00000000000..dad037444fe --- /dev/null +++ b/compute_field_after_install/data/ir_cron.xml @@ -0,0 +1,17 @@ + + + + + Run Recompute field + + + 30 + minutes + -1 + + + code + model._run_all() + + + diff --git a/compute_field_after_install/models/__init__.py b/compute_field_after_install/models/__init__.py new file mode 100644 index 00000000000..09d08483c70 --- /dev/null +++ b/compute_field_after_install/models/__init__.py @@ -0,0 +1 @@ +from . import recompute_field diff --git a/compute_field_after_install/models/recompute_field.py b/compute_field_after_install/models/recompute_field.py new file mode 100644 index 00000000000..1f0927ede0a --- /dev/null +++ b/compute_field_after_install/models/recompute_field.py @@ -0,0 +1,99 @@ +# Copyright 2017 Akretion (http://www.akretion.com). +# @author Sébastien BEAU +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + + +import logging + +from openerp import api, fields, models +from openerp.exceptions import Warning as UserError +from openerp.tools.translate import _ + +from odoo.tools import config + +_logger = logging.getLogger(__name__) + + +class RecomputeField(models.Model): + _name = "recompute.field" + _description = "Recompute Field" + + model = fields.Char(required=True) + field = fields.Char(required=True) + last_id = fields.Integer( + string="Last ID", help="Last record ID on which computing have been executed" + ) + step = fields.Integer(required=True, default=1000, help="Recomputing batch size.") + state = fields.Selection( + [ + ("todo", "Todo"), + ("done", "Done"), + ] + ) + + @api.model + def _run_all(self): + return self.search([("state", "=", "todo")]).run() + + def run(self): + for task in self: + cursor = self.env.cr + offset = 0 + model = self.env[task.model] + if task.last_id: + domain = [("id", ">", task.last_id)] + else: + domain = [] + if task.step <= 0: + raise UserError(_("Step must be upper than 0")) + else: + limit = task.step + while True: + _logger.info( + "Recompute field %s for model %s in background. Offset %s", + task.field, + task.model, + offset, + ) + records = model.search(domain, limit=limit, offset=offset, order="id") + if not records: + break + offset += limit + field = records._fields[task.field] + self.env.add_to_compute(field, records) + records.recompute() + task.last_id = records[-1].id + cursor.commit() + task.state = "done" + cursor.commit() + return True + + +ori_add_to_compute = api.Environment.add_to_compute + + +def add_to_compute(self, field, records): + if ( + "recompute.field" in self + and len(records) > config.get("differ_recomputed_field_size", 50000) + and self.context.get("module") + ): + _logger.info( + "Differs recomputation of field %s for model %s as there is %s records", + field.name, + records._name, + len(records), + ) + with self.norecompute(): + self["recompute.field"].create( + { + "field": field.name, + "model": records._name, + "state": "todo", + } + ) + else: + return ori_add_to_compute(self, field, records) + + +api.Environment.add_to_compute = add_to_compute diff --git a/compute_field_after_install/readme/CONFIGURATION.rst b/compute_field_after_install/readme/CONFIGURATION.rst new file mode 100644 index 00000000000..56206472f28 --- /dev/null +++ b/compute_field_after_install/readme/CONFIGURATION.rst @@ -0,0 +1,9 @@ +The module can be installed just like any other Odoo module, by adding the +module's directory to Odoo *addons_path*. In order for the module to correctly +wrap the Odoo WSGI application, it also needs to be loaded as a server-wide +module. This can be done with the ``server_wide_modules`` parameter in your +Odoo config file or with the ``--load`` command-line parameter. + +Additionnaly you can customize the number of record in a table that differs the +fields recomputation adding the following variable in the odoo config file +``differ_recomputed_field_size=20000`` diff --git a/compute_field_after_install/readme/CONTRIBUTORS.rst b/compute_field_after_install/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000000..9e0ae5f27a3 --- /dev/null +++ b/compute_field_after_install/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Sébastien BEAU diff --git a/compute_field_after_install/readme/DESCRIPTION.rst b/compute_field_after_install/readme/DESCRIPTION.rst new file mode 100644 index 00000000000..e7ff8353558 --- /dev/null +++ b/compute_field_after_install/readme/DESCRIPTION.rst @@ -0,0 +1,5 @@ +Computed field can be really long to be computed when installing a module and so block +the migration process during a long time. + +This module give the possibility to compute +the field added after the installation diff --git a/compute_field_after_install/readme/USAGE.rst b/compute_field_after_install/readme/USAGE.rst new file mode 100644 index 00000000000..9f4526d7939 --- /dev/null +++ b/compute_field_after_install/readme/USAGE.rst @@ -0,0 +1,3 @@ +Just install any module with computed field. + +Database is available quickly whatever its size and computed fields. diff --git a/compute_field_after_install/security/ir.model.access.csv b/compute_field_after_install/security/ir.model.access.csv new file mode 100644 index 00000000000..5d195c5d7fa --- /dev/null +++ b/compute_field_after_install/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_recompute_field,access_recompute_field,model_recompute_field,base.group_user,1,0,0,0 diff --git a/compute_field_after_install/static/description/index.html b/compute_field_after_install/static/description/index.html new file mode 100644 index 00000000000..885592a3707 --- /dev/null +++ b/compute_field_after_install/static/description/index.html @@ -0,0 +1,432 @@ + + + + + + +Base compute field after install + + + +
+

Base compute field after install

+ + +

Beta License: AGPL-3 OCA/server-tools Translate me on Weblate Try me on Runboat

+

Computed field can be really long to be computed when installing a module and so block +the migration process during a long time.

+

This module give the possibility to compute +the field added after the installation

+

Table of contents

+ +
+

Usage

+

Just install any module with computed field.

+

Database is available quickly whatever its size and computed fields.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub 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.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Akretion
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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.

+

Current maintainer:

+

sebastienbeau

+

This module is part of the OCA/server-tools project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/compute_field_after_install/tests/__init__.py b/compute_field_after_install/tests/__init__.py new file mode 100644 index 00000000000..2adaf71023c --- /dev/null +++ b/compute_field_after_install/tests/__init__.py @@ -0,0 +1 @@ +from . import test_recompute diff --git a/compute_field_after_install/tests/test_recompute.py b/compute_field_after_install/tests/test_recompute.py new file mode 100644 index 00000000000..229a69c22e2 --- /dev/null +++ b/compute_field_after_install/tests/test_recompute.py @@ -0,0 +1,51 @@ +# Copyright 2023 Akretion (https://www.akretion.com). +# @author Sébastien BEAU +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from unittest.mock import Mock, patch + +from odoo.tests import TransactionCase +from odoo.tools import config + + +class TestRecompute(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + + def setUp(self): + super().setUp() + self.env.cr.commit = Mock() + + def test_add_fields(self): + # Simulate the module installation with a computed field + records = self.env["res.partner"].search([]) + with patch.dict( + config.options, {"differ_recomputed_field_size": 1}, clear=True + ): + self.env(context={"module": "fake_module"}).add_to_compute( + records._fields["commercial_company_name"], records + ) + + # Check that a job have been created + recompute_field = self.env["recompute.field"].search( + [ + ("model", "=", "res.partner"), + ("field", "=", "commercial_company_name"), + ] + ) + self.assertEqual(recompute_field.state, "todo") + + # Purge field commercial_company_name to simulate + # the installation of a new field + self.env.cr.execute("UPDATE res_partner SET commercial_company_name=Null") + + partner = self.env.ref("base.res_partner_address_7") + self.assertFalse(partner.commercial_company_name) + + # Run the cron to process computed field + self.env["recompute.field"]._run_all() + self.assertEqual(recompute_field.state, "done") + + # Check that field have been recomputed correctly + self.assertEqual(partner.commercial_company_name, "Ready Mat") diff --git a/compute_field_after_install/views/recompute_field_view.xml b/compute_field_after_install/views/recompute_field_view.xml new file mode 100644 index 00000000000..a4b73f95d11 --- /dev/null +++ b/compute_field_after_install/views/recompute_field_view.xml @@ -0,0 +1,75 @@ + + + + + recompute.field + + + + + + + +