From 7c20e1729d90aa6fb0217e83ce3a68e85699a7d1 Mon Sep 17 00:00:00 2001 From: Victor Martin Date: Fri, 18 May 2018 18:31:23 +0800 Subject: [PATCH] [10.0] partner_identification install post-hook migration --- .travis.yml | 1 + partner_identification/__init__.py | 82 ++++++++++++++++++++++++++ partner_identification/__manifest__.py | 6 ++ 3 files changed, 89 insertions(+) diff --git a/.travis.yml b/.travis.yml index f043f3881..b2e422893 100644 --- a/.travis.yml +++ b/.travis.yml @@ -31,6 +31,7 @@ install: - git clone --depth=1 https://github.com/OCA/maintainer-quality-tools.git ${HOME}/maintainer-quality-tools - export PATH=${HOME}/maintainer-quality-tools/travis:${PATH} - travis_install_nightly + - pip install --ignore-installed git+https://github.com/OCA/openupgradelib.git@master script: - travis_run_tests diff --git a/partner_identification/__init__.py b/partner_identification/__init__.py index a77a6fcbc..837f7235f 100644 --- a/partner_identification/__init__.py +++ b/partner_identification/__init__.py @@ -2,3 +2,85 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from . import models +from odoo import api, SUPERUSER_ID +import logging +_logger = logging.getLogger(__name__) + +try: + from openupgradelib import openupgrade +except (ImportError, IOError) as err: + _logger.debug(err) + + +def not_demo_db(cr): + """Check demo DB or not""" + cr.execute("""SELECT id + FROM public.ir_module_module + WHERE demo = 'f' + LIMIT 1""") + module_record = cr.fetchone() + if module_record[0]: + return False + return True + + +def get_create_res_partner_id_category(env, old_category_id): + # license_type migration + cr = env.cr + query = """ + SELECT id, name + FROM license_type + WHERE id = %i""" % (old_category_id) + cr.execute(query) + record = cr.fetchone() + rec_id, name = record + res_id = env['res.partner.id_category'].search([ + ('name', '=', name) + ]) + if not res_id: + res_id = env['res.partner.id_category'].create({ + 'code': name[:15], + 'name': name, + }) + return res_id + + +def create_res_partner_id_number( + env, + partner_id, + license_date, + about2expired, + category_id): + env['res.partner.id_number'].create({ + 'partner_id': partner_id, + 'category_id': category_id, + 'valid_until': license_date, + 'status': ('pending' if about2expired == 't' else 'open'), + }) + + +def migrate_partner_licence_information(env): + # partner_license migration + cr = env.cr + query = """ + SELECT id, partner_id, about2expired, license_date, name + FROM partner_license""" + cr.execute(query) + records = cr.fetchall() + for record in records: + rec_id, partner_id, about2expired, license_date, name = record + res_partner_id_category_id = get_create_res_partner_id_category(name) + create_res_partner_id_number( + env, + partner_id, + license_date, + about2expired, + res_partner_id_category_id) + + +def post_init_hook(cr, registry): + env = api.Environment(cr, SUPERUSER_ID, {}) + if not_demo_db(cr): + if openupgrade.table_exists(env.cr, 'partner_license') and ( + openupgrade.table_exists(env.cr, 'licence_type')): + migrate_partner_licence_information(env) diff --git a/partner_identification/__manifest__.py b/partner_identification/__manifest__.py index e54d2c0aa..af3ff7ddd 100644 --- a/partner_identification/__manifest__.py +++ b/partner_identification/__manifest__.py @@ -12,6 +12,12 @@ 'name': 'Partner Identification Numbers', 'category': 'Customer Relationship Management', 'version': '10.0.1.1.1', + 'external_dependencies': { + 'python': [ + 'openupgradelib', + ] + }, + 'post_init_hook': 'post_init_hook', 'depends': [ 'sales_team', ],