diff --git a/bi_view_editor/models/bve_view.py b/bi_view_editor/models/bve_view.py index bfcca69d..0397e644 100644 --- a/bi_view_editor/models/bve_view.py +++ b/bi_view_editor/models/bve_view.py @@ -293,9 +293,8 @@ class BveView(models.Model): join_nodes = get_join_nodes(info) table_name = self.model_name.replace('.', '_') - tools.drop_view_if_exists(self.env.cr, table_name) - # this line is only for robustness in case something goes wrong + # robustness in case something went wrong self._cr.execute('DROP TABLE IF EXISTS "%s"' % table_name) basic_fields = [ @@ -344,6 +343,9 @@ class BveView(models.Model): vals.update({'selection': selection_domain}) return vals + # clean dirty view (in case something went wrong) + self.action_reset() + # create sql view self._create_sql_view() @@ -358,7 +360,8 @@ class BveView(models.Model): for field in data if 'join_node' not in field] } - model = self.env['ir.model'].sudo().create(model_vals) + Model = self.env['ir.model'].sudo().with_context(bve=True) + model = Model.create(model_vals) # give access rights self._build_access_rules(model) diff --git a/bi_view_editor/models/ir_model.py b/bi_view_editor/models/ir_model.py index 06de5c8c..40a73620 100644 --- a/bi_view_editor/models/ir_model.py +++ b/bi_view_editor/models/ir_model.py @@ -3,6 +3,7 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from odoo import api, models +from odoo.modules.registry import RegistryManager NO_BI_MODELS = [ 'temp.range', @@ -269,3 +270,26 @@ class IrModel(models.Model): reverse=True ) return sorted_fields + + @api.model + def create(self, vals): + if self._context and self._context.get('bve'): + vals['state'] = 'base' + res = super(IrModel, self).create(vals) + + # this sql update is necessary since a write method here would + # be not working (an orm constraint is restricting the modification + # of the state field while updating ir.model) + q = ("""UPDATE ir_model SET state = 'manual' + WHERE id = """ + str(res.id)) + self.env.cr.execute(q) + + # # update registry + if self._context.get('bve'): + # setup models; this reloads custom models in registry + self.pool.setup_models(self._cr, partial=(not self.pool.ready)) + + # signal that registry has changed + RegistryManager.signal_registry_change(self.env.cr.dbname) + + return res