From 15cfe178541c33b6aba51185a34bca884ca48271 Mon Sep 17 00:00:00 2001 From: Simone Rubino Date: Tue, 31 Jul 2018 16:46:44 +0200 Subject: [PATCH 1/3] [FIX][bi_view_editor] apostrophe in model name raised ValueError --- bi_view_editor/models/bve_view.py | 18 ++++++------------ bi_view_editor/tests/test_bi_view.py | 25 ++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/bi_view_editor/models/bve_view.py b/bi_view_editor/models/bve_view.py index d8419296..b559fa6e 100644 --- a/bi_view_editor/models/bve_view.py +++ b/bi_view_editor/models/bve_view.py @@ -39,7 +39,7 @@ class BveView(models.Model): ('created', 'Created')], default='draft', copy=False) - data = fields.Text( + data = fields.Serialized( help="Use the special query builder to define the query " "to generate your report dataset. " "NOTE: To be edited, the query should be in 'Draft' status.") @@ -63,12 +63,6 @@ class BveView(models.Model): _('Custom BI View names must be unique!')), ] - @classmethod - def _get_format_data(cls, data): - data = data.replace('\'', '"') - data = data.replace(': u"', ':"') - return data - @api.multi def _create_view_arch(self): self.ensure_one() @@ -96,7 +90,7 @@ class BveView(models.Model): view_fields.append(field_def) return view_fields - fields_info = json.loads(self._get_format_data(self.data)) + fields_info = json.loads(self.data) view_fields = _get_field_list(fields_info) return view_fields @@ -118,7 +112,7 @@ class BveView(models.Model): view_fields.append(field_def) return view_fields - fields_info = json.loads(self._get_format_data(self.data)) + fields_info = json.loads(self.data) view_fields = _get_field_list(fields_info) return view_fields @@ -218,7 +212,7 @@ class BveView(models.Model): a.perm_''' + access_mode, (model_name,)) return [x[0] for x in self.env.cr.fetchall()] - info = json.loads(self._get_format_data(self.data)) + info = json.loads(self.data) model_names = list(set([f['model'] for f in info])) read_groups = set.intersection(*[set( group_ids_with_access(model_name, 'read') @@ -285,7 +279,7 @@ class BveView(models.Model): check_empty_data(self.data) - formatted_data = json.loads(self._get_format_data(self.data)) + formatted_data = json.loads(self.data) info = get_fields_info(formatted_data) select_fields = get_fields(info) tables = get_tables(info) @@ -352,7 +346,7 @@ class BveView(models.Model): self._create_sql_view() # create model and fields - data = json.loads(self._get_format_data(self.data)) + data = json.loads(self.data) model_vals = { 'name': self.name, 'model': self.model_name, diff --git a/bi_view_editor/tests/test_bi_view.py b/bi_view_editor/tests/test_bi_view.py index 375227aa..95a1053a 100644 --- a/bi_view_editor/tests/test_bi_view.py +++ b/bi_view_editor/tests/test_bi_view.py @@ -2,6 +2,8 @@ # Copyright 2017 Onestein () # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import json + from odoo.tests.common import TransactionCase, at_install, post_install from odoo.exceptions import UserError @@ -100,7 +102,7 @@ class TestBiViewEditor(TransactionCase): 'measure': 0 } ] - format_data = self.env['bve.view']._get_format_data(str(data)) + format_data = json.dumps(data) self.bi_view1_vals = { 'state': 'draft', @@ -228,3 +230,24 @@ class TestBiViewEditor(TransactionCase): # try to remove view with self.assertRaises(UserError): bi_view.unlink() + + @at_install(False) + @post_install(True) + def test_10_create_open_bve_object_apostrophe(self): + vals = self.bi_view1_vals + employees_group = self.env.ref('base.group_user') + vals.update({ + 'name': "Test View5", + 'group_ids': [(6, 0, [employees_group.id])], + }) + l = list() + for r in json.loads(vals['data']): + r['model_name'] = "model'name" + l.append(r) + new_format_data = json.dumps(l) + vals.update({'data': new_format_data}) + bi_view = self.env['bve.view'].create(vals) + self.assertEqual(len(bi_view), 1) + + # create bve object + bi_view.action_create() From 92b778a496633d24b23552201a16c03a2fa1e23d Mon Sep 17 00:00:00 2001 From: SimoRubi Date: Mon, 13 Aug 2018 11:59:20 +0200 Subject: [PATCH 2/3] fixup! [FIX][bi_view_editor] apostrophe in model name raised ValueError --- bi_view_editor/__manifest__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bi_view_editor/__manifest__.py b/bi_view_editor/__manifest__.py index 94177ed9..680569e0 100644 --- a/bi_view_editor/__manifest__.py +++ b/bi_view_editor/__manifest__.py @@ -10,7 +10,7 @@ 'license': 'AGPL-3', 'website': 'http://www.onestein.eu', 'category': 'Reporting', - 'version': '10.0.1.0.1', + 'version': '10.0.1.0.2', 'depends': [ 'base', 'web' From a9df4f6ccb645583dbec2d150819954ff1e46da8 Mon Sep 17 00:00:00 2001 From: SimoRubi Date: Tue, 14 Aug 2018 16:47:43 +0200 Subject: [PATCH 3/3] Added migration script --- .../migrations/10.0.1.0.2/post-migrate.py | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 bi_view_editor/migrations/10.0.1.0.2/post-migrate.py diff --git a/bi_view_editor/migrations/10.0.1.0.2/post-migrate.py b/bi_view_editor/migrations/10.0.1.0.2/post-migrate.py new file mode 100644 index 00000000..806bb2d6 --- /dev/null +++ b/bi_view_editor/migrations/10.0.1.0.2/post-migrate.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Simone Rubino - Agile Business Group +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from openupgradelib.openupgrade import logged_query, migrate +import json + + +@migrate() +def migrate(env, version): + cr = env.cr + convert_text_to_serialized( + cr, env['bve.view']._table, env['bve.view']._fields['data'].name) + pass + + +def convert_text_to_serialized( + cr, table, text_field_name, serialized_field_name=None): + """ + Convert Text field value to Serialized value. + """ + if not serialized_field_name: + serialized_field_name = text_field_name + select_query = """ +SELECT + id, + %(text_field_name)s +FROM %(table)s +WHERE %(text_field_name)s IS NOT NULL +""" + cr.execute( + select_query % { + 'text_field_name': text_field_name, + 'table': table, + } + ) + update_query = """ +UPDATE %(table)s + SET %(serialized_field_name)s = %%(field_value)s + WHERE id = %(record_id)d +""" + for row in cr.fetchall(): + # Fill in the field_value later because it needs escaping + row_update_query = update_query % { + 'serialized_field_name': serialized_field_name, + 'table': table, + 'record_id': row[0]} + logged_query( + cr, row_update_query, { + 'field_value': json.dumps(row[1]) + })