From 6fa3d047c94ece65db2121c2ad796244f4a85fb1 Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Mon, 8 Jul 2019 12:36:47 +0200 Subject: [PATCH] fixup! fixup! fixup! fixup! [ADD] new module module_analysis --- module_analysis/__init__.py | 19 +----------- module_analysis/__manifest__.py | 2 +- module_analysis/models/ir_module_author.py | 15 +++------ module_analysis/models/ir_module_module.py | 6 ++-- module_analysis/models/ir_module_type.py | 3 ++ module_analysis/post_init_hook.py | 13 ++++++++ module_analysis/tests/__init__.py | 1 + module_analysis/tests/test_module.py | 31 +++++++++++++++++++ .../views/view_ir_module_author.xml | 2 -- 9 files changed, 59 insertions(+), 33 deletions(-) create mode 100644 module_analysis/post_init_hook.py create mode 100644 module_analysis/tests/__init__.py create mode 100644 module_analysis/tests/test_module.py diff --git a/module_analysis/__init__.py b/module_analysis/__init__.py index b4e782e8d..9f5fd1d3b 100644 --- a/module_analysis/__init__.py +++ b/module_analysis/__init__.py @@ -1,19 +1,2 @@ from . import models - -# Copyright 2016-2018 Akretion France -# @author: Alexis de Lattre -# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). - -from odoo import api, SUPERUSER_ID - - -def post_init_hook(cr, registry): - """This post_install script is required because, when the module - is installed, Odoo creates the column in the DB and compute the field - and THEN it loads the file data/res_country_department_data.yml... - So, when it computes the field on module installation, the - departments are not available in the DB, so the department_id field - on res.partner stays null. This post_install script fixes this.""" - print("=====================") - print("=====================") - print("=====================") +from . post_init_hook import analyse_installed_modules diff --git a/module_analysis/__manifest__.py b/module_analysis/__manifest__.py index 7c1e35341..96767a561 100644 --- a/module_analysis/__manifest__.py +++ b/module_analysis/__manifest__.py @@ -29,6 +29,6 @@ 'external_dependencies': { 'lib': ['cloc'], }, - 'post_init_hook': 'post_init_hook', + 'post_init_hook': 'analyse_installed_modules', 'installable': True, } diff --git a/module_analysis/models/ir_module_author.py b/module_analysis/models/ir_module_author.py index 371ab77c2..53510d15d 100644 --- a/module_analysis/models/ir_module_author.py +++ b/module_analysis/models/ir_module_author.py @@ -12,15 +12,12 @@ class IrModuleAuthor(models.Model): name = fields.Char(string='Name', required=True) module_ids = fields.Many2many( - string='Modules', comodel_name='ir.module.module') - - module_qty = fields.Integer( - string="Modules Quantity", - compute='_compute_modules', store=True) + string='Modules', comodel_name='ir.module.module', + relation='ir_module_module_author_rel') installed_module_qty = fields.Integer( string="Installed Modules Quantity", - compute='_compute_modules', store=True) + compute='_compute_installed_module_qty', store=True) _sql_constraints = [ ('name_uniq', 'unique(name)', @@ -29,11 +26,9 @@ class IrModuleAuthor(models.Model): @api.multi @api.depends('module_ids') - def _compute_modules(self): + def _compute_installed_module_qty(self): for author in self: - author.module_qty = len(author.module_ids) - author.installed_module_qty = len( - author.module_ids.filtered(lambda x: x.state == 'installed')) + author.installed_module_qty = len(author.module_ids) @api.model def _get_or_create(self, name): diff --git a/module_analysis/models/ir_module_module.py b/module_analysis/models/ir_module_module.py index e16b07797..68fef96d2 100644 --- a/module_analysis/models/ir_module_module.py +++ b/module_analysis/models/ir_module_module.py @@ -18,7 +18,8 @@ class IrModuleModule(models.Model): _inherit = 'ir.module.module' author_ids = fields.Many2many( - string='Authors', comodel_name='ir.module.author', readonly=True) + string='Authors', comodel_name='ir.module.author', readonly=True, + relation='ir_module_module_author_rel') module_type_id = fields.Many2one( string='Module Type', comodel_name='ir.module.type', readonly=True) @@ -43,7 +44,8 @@ class IrModuleModule(models.Model): res = super(IrModuleModule, self).write(vals) if vals.get('state', False) == 'installed': self.button_analyze_code() - elif vals.get('state', False) == 'uninstalled': + elif vals.get('state', False) == 'uninstalled'\ + and 'module_analysis' not in [x.name for x in self]: self.write({ 'author_ids': [6, 0, []], 'module_type_id': False, diff --git a/module_analysis/models/ir_module_type.py b/module_analysis/models/ir_module_type.py index b3b901039..aa56f4c41 100644 --- a/module_analysis/models/ir_module_type.py +++ b/module_analysis/models/ir_module_type.py @@ -8,9 +8,12 @@ from odoo import api, fields, models class IrModuleType(models.Model): _name = 'ir.module.type' _description = 'Modules Types' + _order = 'sequence' name = fields.Char(string='Name', required=True) + sequence = fields.Integer(string='Sequence') + module_ids = fields.One2many( string='Modules', comodel_name='ir.module.module', inverse_name='module_type_id') diff --git a/module_analysis/post_init_hook.py b/module_analysis/post_init_hook.py new file mode 100644 index 000000000..3b1ba4697 --- /dev/null +++ b/module_analysis/post_init_hook.py @@ -0,0 +1,13 @@ +# Copyright (C) 2019-Today: GRAP () +# @author: Sylvain LE GAL (https://twitter.com/legalsylvain) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import api, SUPERUSER_ID + + +def analyse_installed_modules(cr, registry): + with api.Environment.manage(): + env = api.Environment(cr, SUPERUSER_ID, {}) + installed_modules = env['ir.module.module'].search( + [('state', '=', 'installed')]) + installed_modules.button_analyze_code() diff --git a/module_analysis/tests/__init__.py b/module_analysis/tests/__init__.py new file mode 100644 index 000000000..d9b96c4fa --- /dev/null +++ b/module_analysis/tests/__init__.py @@ -0,0 +1 @@ +from . import test_module diff --git a/module_analysis/tests/test_module.py b/module_analysis/tests/test_module.py new file mode 100644 index 000000000..9279aa805 --- /dev/null +++ b/module_analysis/tests/test_module.py @@ -0,0 +1,31 @@ +# Copyright (C) 2019-Today: GRAP () +# @author: Sylvain LE GAL (https://twitter.com/legalsylvain) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo.tests.common import TransactionCase, post_install + + +@post_install(True) +class TestModule(TransactionCase): + + def setUp(self): + super(TestModule, self).setUp() + self.IrModuleModule = self.env['ir.module.module'] + + def test_installed_modules(self): + installed_modules = self.IrModuleModule.search( + [('state', '=', 'installed')]) + for module in installed_modules: + self.assertTrue( + module.python_lines_qty > 0, + "module '%s' doesn't have python lines defined, whereas it is" + " installed.") + + def test_uninstalled_modules(self): + uninstalled_modules = self.IrModuleModule.search( + [('state', '!=', 'installed')]) + for module in uninstalled_modules: + self.assertTrue( + module.python_lines_qty == 0, + "module '%s' has python lines defined, whereas it is" + " not installed.") diff --git a/module_analysis/views/view_ir_module_author.xml b/module_analysis/views/view_ir_module_author.xml index 136a45827..e3aea0721 100644 --- a/module_analysis/views/view_ir_module_author.xml +++ b/module_analysis/views/view_ir_module_author.xml @@ -18,7 +18,6 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - @@ -32,7 +31,6 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -