pre-migration.py 2.39 KB
Newer Older
François C. committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
# -*- coding: utf-8 -*-
# © 2015-2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


def migrate(cr, version):
    if not version:
        return

    # key : source region XMLID - value : destination region XMLID
    new_region_map = {
        'res_country_state_rhonealpes': 'res_country_state_auvergne',
        'res_country_state_champagneardenne': 'res_country_state_alsace',
        'res_country_state_lorraine': 'res_country_state_alsace',
        'res_country_state_limousin': 'res_country_state_aquitaine',
        'res_country_state_poitoucharentes': 'res_country_state_aquitaine',
        'res_country_state_franchecomte': 'res_country_state_bourgogne',
        'res_country_state_midipyrenees':
        'res_country_state_languedocroussillon',
        'res_country_state_picardie': 'res_country_state_nordpasdecalais',
        'res_country_state_hautenormandie': 'res_country_state_bassenormandie',
        }

    # same as new_region_map but with IDs instead of XMLIDs
    new_region_map_id = {}

    for src, dest in new_region_map.iteritems():
        cr.execute("""
            SELECT res_id from ir_model_data where model='res.country.state'
            AND module='l10n_fr_state' AND name=%s
            """, (src, ))
        res_src_id = cr.fetchall()
        cr.execute("""
            SELECT res_id from ir_model_data where model='res.country.state'
            AND module='l10n_fr_state' AND name=%s
            """, (dest, ))
        res_dest_id = cr.fetchall()
        if res_src_id and res_dest_id:
            new_region_map_id[res_src_id[0][0]] = res_dest_id[0][0]

    for src_id, dest_id in new_region_map_id.iteritems():
        cr.execute("""
            UPDATE res_partner set state_id=%s where state_id=%s
            """, (dest_id, src_id))

        cr.execute("""
            SELECT id from ir_model where model='res.country.department'
            """)
        dpt = cr.fetchall()
        if dpt:
            cr.execute("""
                UPDATE res_country_department set state_id=%s where state_id=%s
                """, (dest_id, src_id))

        cr.execute("""
            SELECT id from ir_model where model='res.better.zip'
            """)
        bzip = cr.fetchall()
        if bzip:
            cr.execute("""
                UPDATE res_better_zip set state_id=%s where state_id=%s
                """, (dest_id, src_id))