From 8d450dfe21673089b9cde5ae906c8b01b8f3a299 Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Wed, 2 Aug 2017 01:44:08 +0200 Subject: [PATCH] [FIX] really uninstall modules and avoid a crash on cached data [FIX] don't try to uninstall uninstalled modules [DEL] weird code [FIX] actually cleanup where we can --- database_cleanup/models/purge_models.py | 18 ++++++------------ database_cleanup/models/purge_modules.py | 19 ++++++------------- 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/database_cleanup/models/purge_models.py b/database_cleanup/models/purge_models.py index 5adcf4787..36e9e2812 100644 --- a/database_cleanup/models/purge_models.py +++ b/database_cleanup/models/purge_models.py @@ -10,11 +10,9 @@ class IrModel(models.Model): _inherit = 'ir.model' def _drop_table(self): - # Allow to skip this step during model unlink - # The super method crashes if the model cannot be instantiated - if self.env.context.get('no_drop_table'): - return True - return super(IrModel, self)._drop_table() + """this function crashes for undefined models""" + existing_model_ids = self.filtered(lambda x: x.model in self.env) + return super(IrModel, existing_model_ids)._drop_table() @api.depends() def _inherited_models(self): @@ -28,11 +26,9 @@ class IrModelFields(models.Model): @api.multi def _prepare_update(self): - # Allow to skip this step during model unlink - # The super method crashes if the model cannot be instantiated - if self.env.context.get('no_prepare_update'): - return True - return super(IrModelFields, self)._prepare_update() + """this function crashes for undefined models""" + existing = self.filtered(lambda x: x.model in self.env) + return super(IrModelFields, existing)._prepare_update() class CleanupPurgeLineModel(models.TransientModel): @@ -50,9 +46,7 @@ class CleanupPurgeLineModel(models.TransientModel): """ context_flags = { MODULE_UNINSTALL_FLAG: True, - 'no_drop_table': True, 'purge': True, - 'no_prepare_update': True, } if self: diff --git a/database_cleanup/models/purge_modules.py b/database_cleanup/models/purge_modules.py index bdfe3fcfa..40776f68e 100644 --- a/database_cleanup/models/purge_modules.py +++ b/database_cleanup/models/purge_modules.py @@ -3,7 +3,6 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from odoo import _, api, fields, models from odoo.exceptions import UserError -from odoo.modules.registry import RegistryManager from odoo.modules.module import get_module_path from odoo.addons.base.ir.ir_model import MODULE_UNINSTALL_FLAG @@ -41,25 +40,19 @@ class CleanupPurgeLineModule(models.TransientModel): Uninstall modules upon manual confirmation, then reload the database. """ - if self: - objs = self - else: - objs = self.env['cleanup.purge.line.module']\ - .browse(self._context.get('active_ids')) - module_names = objs.filtered(lambda x: not x.purged).mapped('name') + module_names = self.filtered(lambda x: not x.purged).mapped('name') modules = self.env['ir.module.module'].search([ ('name', 'in', module_names) ]) if not modules: return True self.logger.info('Purging modules %s', ', '.join(module_names)) - modules.button_uninstall() - # we need this commit because reloading the registry would roll back - # our changes - self.env.cr.commit() # pylint: disable=invalid-commit - RegistryManager.new(self.env.cr.dbname, update_module=True) + modules.filtered( + lambda x: x.state not in ('uninstallable', 'uninstalled') + ).button_immediate_uninstall() + modules.refresh() modules.unlink() - return objs.write({'purged': True}) + return self.write({'purged': True}) class CleanupPurgeWizardModule(models.TransientModel):