Merge pull request #2067 from expsa/samir-aladawi-patch-data-module

[ADD] data_patch_hook: a module to patch data if needed
This commit is contained in:
SamirLADOUI-sa 2025-01-05 10:35:34 +01:00 committed by GitHub
commit 8b72c963ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 0 deletions

View File

@ -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

View File

@ -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',
}

View File

@ -0,0 +1 @@