From ee0dfb29d671015e2a3ac27e85aaaf25cf2cd186 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Thu, 23 Nov 2017 17:11:38 +0100 Subject: [PATCH] Add post_install.py script for data migration --- base_partner_one2many_phone/__init__.py | 1 + base_partner_one2many_phone/__manifest__.py | 1 + base_partner_one2many_phone/post_install.py | 46 +++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 base_partner_one2many_phone/post_install.py diff --git a/base_partner_one2many_phone/__init__.py b/base_partner_one2many_phone/__init__.py index b0c0b4c..7a40fbf 100644 --- a/base_partner_one2many_phone/__init__.py +++ b/base_partner_one2many_phone/__init__.py @@ -21,3 +21,4 @@ ############################################################################## from . import partner_phone +from .post_install import migrate_to_partner_phone diff --git a/base_partner_one2many_phone/__manifest__.py b/base_partner_one2many_phone/__manifest__.py index adb29b7..7b4df33 100644 --- a/base_partner_one2many_phone/__manifest__.py +++ b/base_partner_one2many_phone/__manifest__.py @@ -27,4 +27,5 @@ It has been developped by brother Bernard from Barroux Abbey and Alexis de Lattr 'security/ir.model.access.csv', ], 'installable': True, + 'post_init_hook': 'migrate_to_partner_phone', } diff --git a/base_partner_one2many_phone/post_install.py b/base_partner_one2many_phone/post_install.py new file mode 100644 index 0000000..a65619d --- /dev/null +++ b/base_partner_one2many_phone/post_install.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +# © 2017 Akretion (Alexis de Lattre ) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import api, SUPERUSER_ID +import logging +logger = logging.getLogger(__name__) + + +def create_partner_phone(cr, phone_field, phone_type): + cr.execute( + 'SELECT id, ' + phone_field + ' FROM res_partner WHERE ' + + phone_field + ' IS NOT null') + to_create = [] + for partner in cr.fetchall(): + to_create.append({ + 'partner_id': partner[0], + 'type': phone_type, + 'phone': partner[1], + }) + return to_create + + +def migrate_to_partner_phone(cr, registry): + """This post_install script is required because, when the module + is installed, Odoo creates the column in the DB and compute the field + and THEN it loads the file data/res_country_department_data.yml... + So, when it computes the field on module installation, the + departments are not available in the DB, so the department_id field + on res.partner stays null. This post_install script fixes this.""" + logger.info('start data migration for one2many_phone') + with api.Environment.manage(): + env = api.Environment(cr, SUPERUSER_ID, {}) + rppo = env['res.partner.phone'] + to_create = [] + to_create += create_partner_phone(cr, 'phone', '1_home') + to_create += create_partner_phone(cr, 'mobile', '2_mobile') + to_create += create_partner_phone(cr, 'fax', '5_office_fax') + # I need to create all at the end for invalidation purposes + for vals in to_create: + rppo.create(vals) + logger.info( + 'partner_phone type %s phone %s created for partner ID %d', + vals['type'], vals['phone'], vals['partner_id']) + logger.info('end data migration for one2many_phone') + return