From 46b3a932db32ebe1ed1050bdf1845be5db6d8516 Mon Sep 17 00:00:00 2001 From: trisdoan Date: Tue, 24 Dec 2024 11:04:34 +0700 Subject: [PATCH] [ADD] server_action_mass_edit_onchange: extension to support playing onchange --- .../wizard/mass_editing_wizard.py | 63 +-- server_action_mass_edit_onchange/README.rst | 84 ++++ server_action_mass_edit_onchange/__init__.py | 2 + .../__manifest__.py | 15 + .../models/__init__.py | 2 + .../models/ir_actions_server.py | 21 + .../ir_actions_server_mass_edit_line.py | 10 + .../pyproject.toml | 3 + .../readme/CONTRIBUTORS.md | 2 + .../readme/DESCRIPTION.md | 1 + .../readme/USAGE.md | 0 .../static/description/index.html | 429 ++++++++++++++++++ .../tests/__init__.py | 1 + .../tests/test_mass_editing.py | 75 +++ .../views/ir_actions_server.xml | 15 + .../wizard/__init__.py | 1 + .../wizard/mass_editing_wizard.py | 98 ++++ 17 files changed, 767 insertions(+), 55 deletions(-) create mode 100644 server_action_mass_edit_onchange/README.rst create mode 100644 server_action_mass_edit_onchange/__init__.py create mode 100644 server_action_mass_edit_onchange/__manifest__.py create mode 100644 server_action_mass_edit_onchange/models/__init__.py create mode 100644 server_action_mass_edit_onchange/models/ir_actions_server.py create mode 100644 server_action_mass_edit_onchange/models/ir_actions_server_mass_edit_line.py create mode 100644 server_action_mass_edit_onchange/pyproject.toml create mode 100644 server_action_mass_edit_onchange/readme/CONTRIBUTORS.md create mode 100644 server_action_mass_edit_onchange/readme/DESCRIPTION.md create mode 100644 server_action_mass_edit_onchange/readme/USAGE.md create mode 100644 server_action_mass_edit_onchange/static/description/index.html create mode 100644 server_action_mass_edit_onchange/tests/__init__.py create mode 100644 server_action_mass_edit_onchange/tests/test_mass_editing.py create mode 100644 server_action_mass_edit_onchange/views/ir_actions_server.xml create mode 100644 server_action_mass_edit_onchange/wizard/__init__.py create mode 100644 server_action_mass_edit_onchange/wizard/mass_editing_wizard.py diff --git a/server_action_mass_edit/wizard/mass_editing_wizard.py b/server_action_mass_edit/wizard/mass_editing_wizard.py index 07585bea4f..d87e91501e 100644 --- a/server_action_mass_edit/wizard/mass_editing_wizard.py +++ b/server_action_mass_edit/wizard/mass_editing_wizard.py @@ -69,53 +69,6 @@ def default_get(self, fields): return res - def onchange(self, values, field_names, fields_spec): - first_call = not field_names - if first_call: - field_names = [fname for fname in values if fname != "id"] - missing_names = [fname for fname in fields_spec if fname not in values] - defaults = self.default_get(missing_names) - for field_name in missing_names: - values[field_name] = defaults.get(field_name, False) - if field_name in defaults: - field_names.append(field_name) - - server_action_id = self.env.context.get("server_action_id") - server_action = self.env["ir.actions.server"].sudo().browse(server_action_id) - if not server_action: - return super().onchange(values, field_names, fields_spec) - dynamic_fields = {} - - for line in server_action.mapped("mass_edit_line_ids"): - values["selection__" + line.field_id.name] = "ignore" - values[line.field_id.name] = False - - dynamic_fields["selection__" + line.field_id.name] = fields.Selection( - [], default="ignore" - ) - - dynamic_fields[line.field_id.name] = fields.Text([()], default=False) - - self._fields.update(dynamic_fields) - - res = super().onchange(values, field_names, fields_spec) - if not res["value"]: - value = {key: value for key, value in values.items() if value is not False} - res["value"] = value - - for field in dynamic_fields: - self._fields.pop(field) - - view_temp = ( - self.env["ir.ui.view"] - .sudo() - .search([("name", "=", "Temporary Mass Editing Wizard")], limit=1) - ) - if view_temp: - view_temp.unlink() - - return res - @api.model def _prepare_fields(self, line, field, field_info): result = {} @@ -276,8 +229,7 @@ def _clean_check_company_field_domain(self, TargetModel, field, field_info): def create(self, vals_list): server_action_id = self.env.context.get("server_action_id") server_action = self.env["ir.actions.server"].sudo().browse(server_action_id) - active_ids = self.env.context.get("active_ids", []) - if server_action and active_ids: + if server_action: for vals in vals_list: values = {} for key, val in vals.items(): @@ -308,14 +260,15 @@ def create(self, vals_list): values.update({split_key: vals.get(split_key, False)}) if values: - for active_id in active_ids: - self.env[server_action.model_id.model].browse( - active_id - ).with_context( - mass_edit=True, - ).write(values) + self._exec_write(server_action, values) return super().create([{}]) + def _exec_write(self, server_action, vals): + active_ids = self.env.context.get("active_ids", []) + model = self.env[server_action.model_id.model].with_context(mass_edit=True) + records = model.browse(active_ids) + records.write(vals) + def _prepare_create_values(self, vals_list): return vals_list diff --git a/server_action_mass_edit_onchange/README.rst b/server_action_mass_edit_onchange/README.rst new file mode 100644 index 0000000000..cc149f5582 --- /dev/null +++ b/server_action_mass_edit_onchange/README.rst @@ -0,0 +1,84 @@ +================================ +Server Action Mass Edit Onchange +================================ + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:d4e832e364fe018a3d6bc0ea28ee7e5436ba8799a7741c7e68c4b26ed875c3db + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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--ux-lightgray.png?logo=github + :target: https://github.com/OCA/server-ux/tree/18.0/server_action_mass_edit_onchange + :alt: OCA/server-ux +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/server-ux-18-0/server-ux-18-0-server_action_mass_edit_onchange + :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-ux&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module is an extension of module Mass Editing to support playing +onchange before writing. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + + + +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 +------- + +* Camptocamp + +Contributors +------------ + +- Akim Juillerat akim.juillerat@camptocamp.com + +- Tris Doan + +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-ux `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/server_action_mass_edit_onchange/__init__.py b/server_action_mass_edit_onchange/__init__.py new file mode 100644 index 0000000000..9b4296142f --- /dev/null +++ b/server_action_mass_edit_onchange/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import wizard diff --git a/server_action_mass_edit_onchange/__manifest__.py b/server_action_mass_edit_onchange/__manifest__.py new file mode 100644 index 0000000000..c883e011f8 --- /dev/null +++ b/server_action_mass_edit_onchange/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2024 Camptocamp +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Server Action Mass Edit Onchange", + "summary": """Extension of server_action_mass_edit""", + "version": "18.0.1.0.0", + "license": "AGPL-3", + "author": "Camptocamp, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/server-ux", + "depends": ["server_action_mass_edit", "onchange_helper"], + "data": [ + "views/ir_actions_server.xml", + ], +} diff --git a/server_action_mass_edit_onchange/models/__init__.py b/server_action_mass_edit_onchange/models/__init__.py new file mode 100644 index 0000000000..ba247e3c81 --- /dev/null +++ b/server_action_mass_edit_onchange/models/__init__.py @@ -0,0 +1,2 @@ +from . import ir_actions_server_mass_edit_line +from . import ir_actions_server diff --git a/server_action_mass_edit_onchange/models/ir_actions_server.py b/server_action_mass_edit_onchange/models/ir_actions_server.py new file mode 100644 index 0000000000..16133fba3f --- /dev/null +++ b/server_action_mass_edit_onchange/models/ir_actions_server.py @@ -0,0 +1,21 @@ +# Copyright 2024 Camptocamp +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models + + +class IrActionsServer(models.Model): + _inherit = "ir.actions.server" + + mass_edit_play_onchanges = fields.Json( + string="Play onchanges from lines", + compute="_compute_mass_edit_play_onchanges", + ) + + @api.depends("mass_edit_line_ids.apply_onchanges") + def _compute_mass_edit_play_onchanges(self): + for record in self: + record.mass_edit_play_onchanges = { + line.field_id.name: line.apply_onchanges + for line in record.mass_edit_line_ids + } diff --git a/server_action_mass_edit_onchange/models/ir_actions_server_mass_edit_line.py b/server_action_mass_edit_onchange/models/ir_actions_server_mass_edit_line.py new file mode 100644 index 0000000000..6911fd7437 --- /dev/null +++ b/server_action_mass_edit_onchange/models/ir_actions_server_mass_edit_line.py @@ -0,0 +1,10 @@ +# Copyright 2024 Camptocamp +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class IrActionsServerMassEditLine(models.Model): + _inherit = "ir.actions.server.mass.edit.line" + + apply_onchanges = fields.Boolean(help="Play field onchanges before writing value") diff --git a/server_action_mass_edit_onchange/pyproject.toml b/server_action_mass_edit_onchange/pyproject.toml new file mode 100644 index 0000000000..4231d0cccb --- /dev/null +++ b/server_action_mass_edit_onchange/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/server_action_mass_edit_onchange/readme/CONTRIBUTORS.md b/server_action_mass_edit_onchange/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..ae063262d8 --- /dev/null +++ b/server_action_mass_edit_onchange/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +* Akim Juillerat +- Tris Doan \<\> diff --git a/server_action_mass_edit_onchange/readme/DESCRIPTION.md b/server_action_mass_edit_onchange/readme/DESCRIPTION.md new file mode 100644 index 0000000000..2116de8fa9 --- /dev/null +++ b/server_action_mass_edit_onchange/readme/DESCRIPTION.md @@ -0,0 +1 @@ +This module is an extension of module Mass Editing to support playing onchange before writing. diff --git a/server_action_mass_edit_onchange/readme/USAGE.md b/server_action_mass_edit_onchange/readme/USAGE.md new file mode 100644 index 0000000000..e69de29bb2 diff --git a/server_action_mass_edit_onchange/static/description/index.html b/server_action_mass_edit_onchange/static/description/index.html new file mode 100644 index 0000000000..f2ec3a4de4 --- /dev/null +++ b/server_action_mass_edit_onchange/static/description/index.html @@ -0,0 +1,429 @@ + + + + + +Server Action Mass Edit Onchange + + + +
+

Server Action Mass Edit Onchange

+ + +

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

+

This module is an extension of module Mass Editing to support playing +onchange before writing.

+

Table of contents

+ +
+

Usage

+
+
+

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

+
    +
  • Camptocamp
  • +
+
+
+

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.

+

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

+

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

+
+
+
+ + diff --git a/server_action_mass_edit_onchange/tests/__init__.py b/server_action_mass_edit_onchange/tests/__init__.py new file mode 100644 index 0000000000..4f51e8e6b1 --- /dev/null +++ b/server_action_mass_edit_onchange/tests/__init__.py @@ -0,0 +1 @@ +from . import test_mass_editing diff --git a/server_action_mass_edit_onchange/tests/test_mass_editing.py b/server_action_mass_edit_onchange/tests/test_mass_editing.py new file mode 100644 index 0000000000..d2dd4cf61b --- /dev/null +++ b/server_action_mass_edit_onchange/tests/test_mass_editing.py @@ -0,0 +1,75 @@ +# Copyright 2024 Camptocamp +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from unittest.mock import patch + +from odoo.addons.server_action_mass_edit.tests.test_mass_editing import ( + TestMassEditing, +) + + +class TestMassEditingWithOnChange(TestMassEditing): + @patch("odoo.addons.base.models.res_partner.Partner.onchange_email") + def test_wizard_field_onchange(self, patched): + server_action_partner = self.env["ir.actions.server"].create( + { + "name": "Mass Edit Partner", + "state": "mass_edit", + "model_id": self.env.ref("base.model_res_partner").id, + } + ) + self.env["ir.actions.server.mass.edit.line"].create( + [ + { + "server_action_id": server_action_partner.id, + "field_id": self.env.ref("base.field_res_partner__country_id").id, + "apply_onchanges": True, + }, + { + "server_action_id": server_action_partner.id, + "field_id": self.env.ref("base.field_res_partner__email").id, + "apply_onchanges": False, + }, + ] + ) + self.assertEqual( + server_action_partner.mass_edit_play_onchanges, + { + "country_id": True, + "email": False, + }, + ) + us_country = self.env.ref("base.us") + mx_country = self.env.ref("base.mx") + partners = self.env["res.partner"].create( + [ + { + "name": "ACME", + "country_id": us_country.id, + "state_id": self.env.ref("base.state_us_1").id, + }, + { + "name": "Example.com", + "country_id": us_country.id, + "state_id": self.env.ref("base.state_us_2").id, + }, + ] + ) + self.MassEditingWizard.with_context( + server_action_id=server_action_partner.id, + active_ids=partners.ids, + ).create( + { + "selection__country_id": "set", + "selection__email": "set", + "country_id": mx_country, + "email": "dummy@email.com", + } + ) + for partner in partners: + self.assertEqual(partner.country_id, mx_country) + # state_id is set to False by _onchange_country_id + self.assertFalse(partner.state_id) + self.assertEqual(partner.email, "dummy@email.com") + # onchange_email is not called + patched.assert_not_called() diff --git a/server_action_mass_edit_onchange/views/ir_actions_server.xml b/server_action_mass_edit_onchange/views/ir_actions_server.xml new file mode 100644 index 0000000000..3368be46f8 --- /dev/null +++ b/server_action_mass_edit_onchange/views/ir_actions_server.xml @@ -0,0 +1,15 @@ + + + + ir.actions.server + + + + + + + + diff --git a/server_action_mass_edit_onchange/wizard/__init__.py b/server_action_mass_edit_onchange/wizard/__init__.py new file mode 100644 index 0000000000..01affac4bd --- /dev/null +++ b/server_action_mass_edit_onchange/wizard/__init__.py @@ -0,0 +1 @@ +from . import mass_editing_wizard diff --git a/server_action_mass_edit_onchange/wizard/mass_editing_wizard.py b/server_action_mass_edit_onchange/wizard/mass_editing_wizard.py new file mode 100644 index 0000000000..f6e5065b76 --- /dev/null +++ b/server_action_mass_edit_onchange/wizard/mass_editing_wizard.py @@ -0,0 +1,98 @@ +# Copyright 2024 Camptocamp +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models + + +class MassEditingWizard(models.TransientModel): + _inherit = "mass.editing.wizard" + + play_onchanges = fields.Json(readonly=True) + + @api.model + def default_get(self, fields): + res = super().default_get(fields) + server_action_id = self.env.context.get("server_action_id") + server_action = self.env["ir.actions.server"].sudo().browse(server_action_id) + + if not server_action: + return res + res.update( + { + "play_onchanges": server_action.mass_edit_play_onchanges, + } + ) + return res + + def onchange(self, values, field_names, fields_spec): + first_call = not field_names + if first_call: + field_names = [fname for fname in values if fname != "id"] + missing_names = [fname for fname in fields_spec if fname not in values] + defaults = self.default_get(missing_names) + for field_name in missing_names: + values[field_name] = defaults.get(field_name, False) + if field_name in defaults: + field_names.append(field_name) + + server_action_id = self.env.context.get("server_action_id") + server_action = self.env["ir.actions.server"].sudo().browse(server_action_id) + if not server_action: + return super().onchange(values, field_names, fields_spec) + dynamic_fields = {} + + for line in server_action.mapped("mass_edit_line_ids"): + values["selection__" + line.field_id.name] = "ignore" + values[line.field_id.name] = False + + dynamic_fields["selection__" + line.field_id.name] = fields.Selection( + [], default="ignore" + ) + + dynamic_fields[line.field_id.name] = fields.Text([()], default=False) + + self._fields.update(dynamic_fields) + + res = super().onchange(values, field_names, fields_spec) + if not res["value"]: + value = {key: value for key, value in values.items() if value is not False} + res["value"] = value + + for field in dynamic_fields: + self._fields.pop(field) + + view_temp = ( + self.env["ir.ui.view"] + .sudo() + .search([("name", "=", "Temporary Mass Editing Wizard")], limit=1) + ) + if view_temp: + view_temp.unlink() + + return res + + def _exec_write(self, server_action, vals): + active_ids = self.env.context.get("active_ids", []) + model = self.env[server_action.model_id.model].with_context(mass_edit=True) + records = model.browse(active_ids) + # Check if a field in values is set to play onchanges, in which case + # each record is to be updated sequentially + onchanges_to_play = [ + fname + for fname, val in server_action.mass_edit_play_onchanges.items() + if val + ] + if onchanges_to_play: + onchange_values = {k: v for k, v in vals.items() if k in onchanges_to_play} + not_onchange_values = { + k: v for k, v in vals.items() if k not in onchanges_to_play + } + for rec in records: + rec_values = onchange_values.copy() + rec_values = rec.play_onchanges(rec_values, list(rec_values.keys())) + rec_values.update(not_onchange_values) + rec.write(rec_values) + else: + # If there is not any onchange to play we can write + # all the records at once + return super()._exec_write(server_action, vals)