From d39f88ebb90a3c402970ff44608f5329306d678e Mon Sep 17 00:00:00 2001 From: Samir Ladoui Date: Sun, 5 Jan 2025 10:34:13 +0100 Subject: [PATCH] [ADD] data_patch_hook: a module to patch data if needed --- odex25_base/data_patch_hook/__init__.py | 29 +++++++++++++++++++++ odex25_base/data_patch_hook/__manifest__.py | 15 +++++++++++ odex25_base/data_patch_hook/hooks.py | 1 + 3 files changed, 45 insertions(+) create mode 100644 odex25_base/data_patch_hook/__init__.py create mode 100644 odex25_base/data_patch_hook/__manifest__.py create mode 100644 odex25_base/data_patch_hook/hooks.py diff --git a/odex25_base/data_patch_hook/__init__.py b/odex25_base/data_patch_hook/__init__.py new file mode 100644 index 000000000..3a85f93e7 --- /dev/null +++ b/odex25_base/data_patch_hook/__init__.py @@ -0,0 +1,29 @@ +from odoo import api, SUPERUSER_ID +from odoo.exceptions import ValidationError + + +def post_init_hook(cr, registry): + """ + This function serves as the hook to apply data patches and commit changes + before raising an intentional error. + """ + # Example functions to call (define these functions elsewhere in this file) + update_due_date_in_transaction_model(cr, registry) + + # Commit the changes + cr.commit() + + # Raise an intentional error to prevent module installation + raise ValidationError("Intentional error: This module is not designed to be installed.\nData Applied Successfully!") + +def update_due_date_in_transaction_model(cr, registry): + """ + Recalculate the is_late field for all records after module upgrade. + """ + env = api.Environment(cr, SUPERUSER_ID, {}) + + transaction_models = ['outgoing.transaction', 'internal.transaction', 'incoming.transaction'] + for transaction_model in transaction_models: + model = env[transaction_model] + records = model.search([]) # Fetch all records + records.compute_due_date() # Trigger computation \ No newline at end of file diff --git a/odex25_base/data_patch_hook/__manifest__.py b/odex25_base/data_patch_hook/__manifest__.py new file mode 100644 index 000000000..796d6999e --- /dev/null +++ b/odex25_base/data_patch_hook/__manifest__.py @@ -0,0 +1,15 @@ +{ + 'name': 'Data Patches with Hook', + 'version': '1.0', + 'summary': 'A module to apply data patches using a hook.', + 'category': 'Tools', + 'author': 'Expert Co.', + 'description': """ + This module apply data patches via hooks. + """, + 'depends': ['base'], + 'data': [], + 'installable': True, + 'application': False, + 'post_init_hook': 'post_init_hook', +} \ No newline at end of file diff --git a/odex25_base/data_patch_hook/hooks.py b/odex25_base/data_patch_hook/hooks.py new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/odex25_base/data_patch_hook/hooks.py @@ -0,0 +1 @@ +