From 7eca332ba30106b691120171bfbbd16d857d96f6 Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Sat, 26 Sep 2015 02:31:31 +0200 Subject: [PATCH] [FIX] Compatibility for OpenERP versions prior to 6.1 --- openupgradelib/openupgrade.py | 55 ++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/openupgradelib/openupgrade.py b/openupgradelib/openupgrade.py index 8b67648a..16c0b56f 100644 --- a/openupgradelib/openupgrade.py +++ b/openupgradelib/openupgrade.py @@ -23,19 +23,31 @@ import os import inspect import logging -from openerp import release, tools, SUPERUSER_ID -from openerp.tools.yaml_import import yaml_import -from openerp.osv import orm -from openerp.modules.registry import RegistryManager -import openerp.osv.fields from . import openupgrade_tools -if release.version_info[0] >= 7: - from openerp.tools.mail import plaintext2html -if release.version_info[0] >= 8: - from openerp.fields import Many2many, One2many +try: + from openerp import release +except ImportError: + import release +Many2many = False +One2many = False +if hasattr(release, 'version_info'): + from openerp import tools, SUPERUSER_ID + from openerp.tools.yaml_import import yaml_import + from openerp.osv.orm import except_orm + from openerp.modules.registry import RegistryManager + from openerp.osv.fields import many2many, one2many + if release.version_info[0] >= 7: + from openerp.tools.mail import plaintext2html + if release.version_info[0] >= 8: + from openerp.fields import Many2many, One2many else: - Many2many = False - One2many = False + # version < 6.1 + import tools + SUPERUSER_ID = 1 + from tools.yaml_import import yaml_import + from osv.osv import except_osv as except_orm + RegistryManager = None + from osv.fields import many2many, one2many if sys.version_info[0] == 3: unicode = str @@ -499,7 +511,7 @@ def write_value(ids, field, value): for model in default_spec.keys(): obj = pool.get(model) if not obj: - raise orm.except_orm( + raise except_orm( "Error", "Migration: error setting default, no such model: %s" % model) for field, value in default_spec[model]: @@ -536,7 +548,7 @@ def write_value(ids, field, value): field, model)) logger.error(error) # this exception seems to get lost in a higher up try block - orm.except_orm("OpenUpgrade", error) + except_orm("OpenUpgrade", error) else: write_value(ids, field, value) @@ -648,13 +660,15 @@ def m2o_to_x2m(cr, model, table, field, source_field): .. versionadded:: 8.0 """ if not model._columns.get(field): - raise orm.except_orm( + raise except_orm( "Error", "m2o_to_x2m: field %s doesn't exist in model %s" % ( field, model._name)) - if isinstance(model._columns[field], (openerp.osv.fields.many2many, - Many2many)): - rel, id1, id2 = openerp.osv.fields.many2many._sql_names( - model._columns[field], model) + if isinstance(model._columns[field], (many2many, Many2many)): + if hasattr(release, 'version_info'): + rel, id1, id2 = many2many._sql_names(model._columns[field], model) + else: + # version < 6.1 + rel, id1, id2 = model._rel, model._id1, model._id2 logged_query( cr, """ @@ -664,8 +678,7 @@ def m2o_to_x2m(cr, model, table, field, source_field): WHERE %s is not null """ % (rel, id1, id2, source_field, table, source_field)) - elif isinstance(model._columns[field], (One2many, - openerp.osv.fields.one2many)): + elif isinstance(model._columns[field], (One2many, one2many)): if isinstance(model._columns[field], One2many): target_table = ( model.pool[model._columns[field].comodel_name]._table) @@ -685,7 +698,7 @@ def m2o_to_x2m(cr, model, table, field, source_field): 'source_field': source_field, 'source_table': table}) else: - raise orm.except_orm( + raise except_orm( "Error", "m2o_to_x2m: field %s of model %s is not a " "many2many/one2many one" % (field, model._name))