From 603aeb4923e449c370d0111f587d79c70cdb4a23 Mon Sep 17 00:00:00 2001 From: Andrea Stirpe Date: Tue, 7 May 2024 19:38:39 +0000 Subject: [PATCH] [MIG] adapt code for V16 --- database_cleanup/README.rst | 1 + database_cleanup/__manifest__.py | 2 +- database_cleanup/models/purge_fields.py | 72 ++++++++++--------- database_cleanup/readme/CONTRIBUTORS.rst | 2 + database_cleanup/security/ir.model.access.csv | 2 + database_cleanup/views/menu.xml | 2 +- database_cleanup/views/purge_fields.xml | 12 +++- 7 files changed, 56 insertions(+), 37 deletions(-) diff --git a/database_cleanup/README.rst b/database_cleanup/README.rst index 251504692..477289b6d 100644 --- a/database_cleanup/README.rst +++ b/database_cleanup/README.rst @@ -82,6 +82,7 @@ Contributors * Holger Brunn * Stéphane Mangin * Mark Schuit +* Andrea Stirpe Maintainers ~~~~~~~~~~~ diff --git a/database_cleanup/__manifest__.py b/database_cleanup/__manifest__.py index a27253256..908d04fa9 100644 --- a/database_cleanup/__manifest__.py +++ b/database_cleanup/__manifest__.py @@ -14,7 +14,7 @@ "views/purge_menus.xml", "views/purge_modules.xml", "views/purge_models.xml", - 'views/purge_fields.xml', + "views/purge_fields.xml", "views/purge_columns.xml", "views/purge_tables.xml", "views/purge_data.xml", diff --git a/database_cleanup/models/purge_fields.py b/database_cleanup/models/purge_fields.py index dbb94c162..cf9b298d6 100644 --- a/database_cleanup/models/purge_fields.py +++ b/database_cleanup/models/purge_fields.py @@ -1,23 +1,25 @@ # Copyright 2014-2016 Therp BV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # pylint: disable=consider-merging-classes-inherited -from odoo import _, api, models, fields +from odoo import _, api, fields, models from odoo.exceptions import UserError + from odoo.addons.base.models.ir_model import MODULE_UNINSTALL_FLAG + from ..identifier_adapter import IdentifierAdapter class CleanupPurgeLineField(models.TransientModel): - _inherit = 'cleanup.purge.line' - _name = 'cleanup.purge.line.field' - _description = 'Purge fields' + _inherit = "cleanup.purge.line" + _name = "cleanup.purge.line.field" + _description = "Purge fields" wizard_id = fields.Many2one( - 'cleanup.purge.wizard.field', 'Purge Wizard', readonly=True + "cleanup.purge.wizard.field", "Purge Wizard", readonly=True ) field_id = fields.Many2one( - comodel_name='ir.model.fields', - string='Field', + comodel_name="ir.model.fields", + string="Field", ) model_id = fields.Many2one( comodel_name="ir.model", @@ -31,24 +33,24 @@ class CleanupPurgeLineField(models.TransientModel): store=True, ) - @api.multi def purge(self): """ Unlink fields upon manual confirmation. """ context_flags = { MODULE_UNINSTALL_FLAG: True, - 'purge': True, + "purge": True, } if self: objs = self else: - objs = self.env['cleanup.purge.line.action']\ - .browse(self._context.get('active_ids')) + objs = self.env["cleanup.purge.line.action"].browse( + self._context.get("active_ids") + ) to_unlink = objs.filtered(lambda x: not x.purged and x.field_id) - self.logger.info('Purging field entries:') + self.logger.info("Purging field entries:") for rec in to_unlink: - self.logger.info(' - %s.%s', rec.model_name, rec.field_id.name) + self.logger.info(" - %s.%s", rec.model_name, rec.field_id.name) field_id = rec.with_context(**context_flags).field_id model = self.env[rec.model_name] table_name = model._table @@ -71,17 +73,18 @@ class CleanupPurgeLineField(models.TransientModel): # Inheritance such as stock.picking.in from stock.picking # can lead to double attempts at removal self.env.cr.execute( - 'SELECT count(attname) FROM pg_attribute ' - 'WHERE attrelid = ' - '( SELECT oid FROM pg_class WHERE relname = %s ) ' - 'AND attname = %s', (table, column) + "SELECT count(attname) FROM pg_attribute " + "WHERE attrelid = " + "( SELECT oid FROM pg_class WHERE relname = %s ) " + "AND attname = %s", + (table, column), ) if not self.env.cr.fetchone()[0]: return - self.logger.info('Dropping column %s from table %s', column, table) + self.logger.info("Dropping column %s from table %s", column, table) self.env.cr.execute( - 'ALTER TABLE %s DROP COLUMN %s', - (IdentifierAdapter(table), IdentifierAdapter(column)) + "ALTER TABLE %s DROP COLUMN %s", + (IdentifierAdapter(table), IdentifierAdapter(column)), ) # we need this commit because the ORM will deadlock if # we still have a pending transaction @@ -89,9 +92,9 @@ class CleanupPurgeLineField(models.TransientModel): class CleanupPurgeWizardField(models.TransientModel): - _inherit = 'cleanup.purge.wizard' - _name = 'cleanup.purge.wizard.field' - _description = 'Purge fields' + _inherit = "cleanup.purge.wizard" + _name = "cleanup.purge.wizard.field" + _description = "Purge fields" @api.model def find(self): @@ -100,24 +103,29 @@ class CleanupPurgeWizardField(models.TransientModel): """ res = [] ignored_fields = models.MAGIC_COLUMNS + [ - "display_name", models.BaseModel.CONCURRENCY_CHECK_FIELD + "display_name", + models.BaseModel.CONCURRENCY_CHECK_FIELD, ] - domain = [('state', '=', 'base')] - for field_id in self.env['ir.model.fields'].search(domain): + domain = [("state", "=", "base")] + for field_id in self.env["ir.model.fields"].search(domain): if field_id.name in ignored_fields: continue model = self.env[field_id.model_id.model] if field_id.name not in model._fields.keys(): res.append( - (0, 0, { - 'name': field_id.name, - 'field_id': field_id.id, - }) + ( + 0, + 0, + { + "name": field_id.name, + "field_id": field_id.id, + }, + ) ) if not res: - raise UserError(_('No orphaned fields found')) + raise UserError(_("No orphaned fields found")) return res purge_line_ids = fields.One2many( - 'cleanup.purge.line.field', 'wizard_id', 'Fields to purge' + "cleanup.purge.line.field", "wizard_id", "Fields to purge" ) diff --git a/database_cleanup/readme/CONTRIBUTORS.rst b/database_cleanup/readme/CONTRIBUTORS.rst index 78e9d6292..2484a38be 100644 --- a/database_cleanup/readme/CONTRIBUTORS.rst +++ b/database_cleanup/readme/CONTRIBUTORS.rst @@ -2,3 +2,5 @@ * Holger Brunn * Stéphane Mangin * Mark Schuit +* `360ERP `_: + * Andrea Stirpe diff --git a/database_cleanup/security/ir.model.access.csv b/database_cleanup/security/ir.model.access.csv index dde616bd0..c70c420e9 100644 --- a/database_cleanup/security/ir.model.access.csv +++ b/database_cleanup/security/ir.model.access.csv @@ -5,6 +5,8 @@ access_cleanup_purge_line_module,access_cleanup_purge_line_module,model_cleanup_ access_cleanup_purge_wizard_module,access_cleanup_purge_wizard_module,model_cleanup_purge_wizard_module,base.group_user,1,1,1,1 access_cleanup_purge_line_model,access_cleanup_purge_line_model,model_cleanup_purge_line_model,base.group_user,1,1,1,1 access_cleanup_purge_wizard_model,access_cleanup_purge_wizard_model,model_cleanup_purge_wizard_model,base.group_user,1,1,1,1 +access_cleanup_purge_line_field,access_cleanup_purge_line_field,model_cleanup_purge_line_field,base.group_user,1,1,1,1 +access_cleanup_purge_wizard_field,access_cleanup_purge_wizard_field,model_cleanup_purge_wizard_field,base.group_user,1,1,1,1 access_cleanup_purge_line_column,access_cleanup_purge_line_column,model_cleanup_purge_line_column,base.group_user,1,1,1,1 access_cleanup_purge_wizard_column,access_cleanup_purge_wizard_column,model_cleanup_purge_wizard_column,base.group_user,1,1,1,1 access_cleanup_purge_line_table,access_cleanup_purge_line_table,model_cleanup_purge_line_table,base.group_user,1,1,1,1 diff --git a/database_cleanup/views/menu.xml b/database_cleanup/views/menu.xml index 3bfa89ebd..353288c71 100644 --- a/database_cleanup/views/menu.xml +++ b/database_cleanup/views/menu.xml @@ -26,7 +26,7 @@ Purge obsolete fields - + diff --git a/database_cleanup/views/purge_fields.xml b/database_cleanup/views/purge_fields.xml index 17841c6ca..1364a4b91 100644 --- a/database_cleanup/views/purge_fields.xml +++ b/database_cleanup/views/purge_fields.xml @@ -1,4 +1,4 @@ - + cleanup.purge.wizard.field @@ -13,7 +13,10 @@ Purge models ir.actions.server code - + action = env.get('cleanup.purge.wizard.field').get_wizard_action() @@ -38,6 +41,9 @@ code records.purge() - +