diff --git a/bi_view_editor/README.rst b/bi_view_editor/README.rst index fcb01c55..ba2a50f4 100644 --- a/bi_view_editor/README.rst +++ b/bi_view_editor/README.rst @@ -49,7 +49,8 @@ Known issues / Roadmap * Extend the capabilities of the tree views (e.g. add sums) * Provide a tutorial (eg. a working example of usage) * Implement a more advanced UI, with possibilities to use LEFT JOIN as default instead of INNER JOIN -* Find better ways to extend the *_auto_init()* without override (possibly avoid the monkey patch) +* Find better ways to extend the *_auto_init()* without override +* Possibly avoid the monkey patches * Data the user has no access to (e.g. in a multi company situation) can be viewed by making a view * Store the JSON data structure in ORM * Would be nice if models available to select when creating a view are limited to the ones that have intersecting groups (for non technical users) diff --git a/bi_view_editor/models/models.py b/bi_view_editor/models/models.py index f945bb47..7037b861 100644 --- a/bi_view_editor/models/models.py +++ b/bi_view_editor/models/models.py @@ -3,9 +3,12 @@ import logging -from odoo import _, api, models, tools +from odoo import SUPERUSER_ID +from odoo import _, api, models, modules, tools from odoo.exceptions import UserError +from odoo.tools import (existing_tables, topological_sort) + _logger = logging.getLogger(__name__) @@ -14,6 +17,42 @@ def _bi_view(_name): return _name[0:6] == 'x_bve.' +def check_tables_exist(self, cr): + """ + Verify that all tables are present and try to initialize + those that are missing. + """ + # This monkey patch is meant to avoid that the _logger writes + # warning and error messages, while running an update all, + # in case the model is a bi-view-generated model. + + env = api.Environment(cr, SUPERUSER_ID, {}) + table2model = { + model._table: name for name, model in env.items() + if not model._abstract and not _bi_view(name) # here is the patch + } + missing_tables = set(table2model).difference( + existing_tables(cr, table2model)) + + if missing_tables: + missing = {table2model[table] for table in missing_tables} + _logger.warning("Models have no table: %s.", ", ".join(missing)) + # recreate missing tables following model dependencies + deps = {name: model._depends for name, model in env.items()} + for name in topological_sort(deps): + if name in missing: + _logger.info("Recreate table of model %s.", name) + env[name].init() + # check again, and log errors if tables are still missing + missing_tables = set(table2model).difference( + existing_tables(cr, table2model)) + for table in missing_tables: + _logger.error("Model %s has no table.", table2model[table]) + + +modules.registry.Registry.check_tables_exist = check_tables_exist + + @api.model_cr_context def _auto_init(self): """ Initialize the database schema of ``self``: @@ -38,7 +77,6 @@ def _auto_init(self): # an update all is the one of BaseModel, and not the one of Base. # START OF patch - # TODO: find better ways to do this patch if _bi_view(self._name): return # END of patch