From 714748ce5da48a7e4f3761f16cd5fc6d66f9d136 Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Wed, 29 Apr 2015 18:13:55 +0200 Subject: [PATCH 1/3] [IMP] deal with modules whose models can't be loaded --- database_cleanup/model/purge_modules.py | 36 +++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/database_cleanup/model/purge_modules.py b/database_cleanup/model/purge_modules.py index 12734d157..98e2531e5 100644 --- a/database_cleanup/model/purge_modules.py +++ b/database_cleanup/model/purge_modules.py @@ -23,6 +23,42 @@ from openerp import pooler from openerp.osv import orm, fields from openerp.modules.module import get_module_path from openerp.tools.translate import _ +from openerp.addons.base.ir.ir_model import MODULE_UNINSTALL_FLAG + + +class IrModelConstraint(orm.Model): + _inherit = 'ir.model.constraint' + + def _module_data_uninstall(self, cr, uid, ids, context=None): + '''this function crashes for constraints on undefined models''' + for this in self.browse(cr, uid, ids, context=context): + if not self.pool.get(this.model.model): + ids.remove(this.id) + this.unlink() + return super(IrModelConstraint, self)._module_data_uninstall( + cr, uid, ids, context=context) + + +class IrModelData(orm.Model): + _inherit = 'ir.model.data' + + def _module_data_uninstall(self, cr, uid, modules_to_remove, context=None): + '''this function crashes for xmlids on undefined models or fields + referring to undefined models''' + ids = self.search(cr, uid, [('module', 'in', modules_to_remove)]) + for this in self.browse(cr, uid, ids, context=context): + if this.model == 'ir.model.fields': + field = self.pool[this.model].browse( + cr, uid, [this.res_id], + context=dict( + context or {}, **{MODULE_UNINSTALL_FLAG: True}))[0] + if not self.pool.get(field.model): + this.unlink() + continue + if not self.pool.get(this.model): + this.unlink() + return super(IrModelData, self)._module_data_uninstall( + cr, uid, modules_to_remove, context=context) class CleanupPurgeLineModule(orm.TransientModel): From 1c3abbc22537d6a3548a11cf71198dfa102da18d Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Mon, 11 May 2015 15:37:41 +0200 Subject: [PATCH 2/3] [IMP] double quotes for docstring --- database_cleanup/model/purge_modules.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/database_cleanup/model/purge_modules.py b/database_cleanup/model/purge_modules.py index 98e2531e5..37651f39f 100644 --- a/database_cleanup/model/purge_modules.py +++ b/database_cleanup/model/purge_modules.py @@ -30,7 +30,7 @@ class IrModelConstraint(orm.Model): _inherit = 'ir.model.constraint' def _module_data_uninstall(self, cr, uid, ids, context=None): - '''this function crashes for constraints on undefined models''' + """this function crashes for constraints on undefined models""" for this in self.browse(cr, uid, ids, context=context): if not self.pool.get(this.model.model): ids.remove(this.id) @@ -43,8 +43,8 @@ class IrModelData(orm.Model): _inherit = 'ir.model.data' def _module_data_uninstall(self, cr, uid, modules_to_remove, context=None): - '''this function crashes for xmlids on undefined models or fields - referring to undefined models''' + """this function crashes for xmlids on undefined models or fields + referring to undefined models""" ids = self.search(cr, uid, [('module', 'in', modules_to_remove)]) for this in self.browse(cr, uid, ids, context=context): if this.model == 'ir.model.fields': From 3d36d6539b44ffe55f19dc262a850ab1bad250ee Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Wed, 20 May 2015 11:13:26 +0200 Subject: [PATCH 3/3] [IMP] readability --- database_cleanup/model/purge_modules.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/database_cleanup/model/purge_modules.py b/database_cleanup/model/purge_modules.py index 37651f39f..d371b5f05 100644 --- a/database_cleanup/model/purge_modules.py +++ b/database_cleanup/model/purge_modules.py @@ -45,13 +45,15 @@ class IrModelData(orm.Model): def _module_data_uninstall(self, cr, uid, modules_to_remove, context=None): """this function crashes for xmlids on undefined models or fields referring to undefined models""" + if context is None: + context = {} ids = self.search(cr, uid, [('module', 'in', modules_to_remove)]) for this in self.browse(cr, uid, ids, context=context): if this.model == 'ir.model.fields': + ctx = context.copy() + ctx[MODULE_UNINSTALL_FLAG] = True field = self.pool[this.model].browse( - cr, uid, [this.res_id], - context=dict( - context or {}, **{MODULE_UNINSTALL_FLAG: True}))[0] + cr, uid, this.res_id, context=ctx) if not self.pool.get(field.model): this.unlink() continue