Browse Source

[FIX] bi_view_editor: Apostrophe + migration script

Apostrophe in model name raised ValueError. Added needed migration script.
pull/202/head
SimoRubi 6 years ago
committed by Pedro M. Baeza
parent
commit
f4eab1c79d
  1. 3
      bi_view_editor/__manifest__.py
  2. 51
      bi_view_editor/migrations/10.0.1.0.2/post-migrate.py
  3. 20
      bi_view_editor/models/bve_view.py
  4. 24
      bi_view_editor/tests/test_bi_view.py

3
bi_view_editor/__manifest__.py

@ -12,7 +12,8 @@
'version': '11.0.1.0.0', 'version': '11.0.1.0.0',
'depends': [ 'depends': [
'base', 'base',
'web'
'web',
'base_sparse_field'
], ],
'data': [ 'data': [
'security/ir.model.access.csv', 'security/ir.model.access.csv',

51
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])
})

20
bi_view_editor/models/bve_view.py

@ -7,6 +7,8 @@ from odoo import api, fields, models, tools
from odoo.exceptions import UserError from odoo.exceptions import UserError
from odoo.tools.translate import _ from odoo.tools.translate import _
from odoo.addons.base_sparse_field.models.fields import Serialized
class BveView(models.Model): class BveView(models.Model):
_name = 'bve.view' _name = 'bve.view'
@ -38,7 +40,7 @@ class BveView(models.Model):
('created', 'Created')], ('created', 'Created')],
default='draft', default='draft',
copy=False) copy=False)
data = fields.Text(
data = Serialized(
help="Use the special query builder to define the query " help="Use the special query builder to define the query "
"to generate your report dataset. " "to generate your report dataset. "
"NOTE: To be edited, the query should be in 'Draft' status.") "NOTE: To be edited, the query should be in 'Draft' status.")
@ -62,12 +64,6 @@ class BveView(models.Model):
_('Custom BI View names must be unique!')), _('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 @api.multi
def _create_view_arch(self): def _create_view_arch(self):
self.ensure_one() self.ensure_one()
@ -95,7 +91,7 @@ class BveView(models.Model):
view_fields.append(field_def) view_fields.append(field_def)
return view_fields 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) view_fields = _get_field_list(fields_info)
return view_fields return view_fields
@ -117,7 +113,7 @@ class BveView(models.Model):
view_fields.append(field_def) view_fields.append(field_def)
return view_fields 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) view_fields = _get_field_list(fields_info)
return view_fields return view_fields
@ -218,7 +214,7 @@ class BveView(models.Model):
res = self.env.cr.fetchall() res = self.env.cr.fetchall()
return [x[0] for x in res] return [x[0] for x in res]
info = json.loads(self._get_format_data(self.data))
info = json.loads(self.data)
model_names = list(set([f['model'] for f in info])) model_names = list(set([f['model'] for f in info]))
read_groups = set.intersection(*[set( read_groups = set.intersection(*[set(
group_ids_with_access(model_name, 'read') group_ids_with_access(model_name, 'read')
@ -289,7 +285,7 @@ class BveView(models.Model):
check_empty_data(self.data) 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) info = get_fields_info(formatted_data)
select_fields = get_fields(info) select_fields = get_fields(info)
tables = get_tables(info) tables = get_tables(info)
@ -387,7 +383,7 @@ class BveView(models.Model):
self._create_sql_view() self._create_sql_view()
# create model and fields # create model and fields
data = json.loads(self._get_format_data(self.data))
data = json.loads(self.data)
model_vals = { model_vals = {
'name': self.name, 'name': self.name,
'model': self.model_name, 'model': self.model_name,

24
bi_view_editor/tests/test_bi_view.py

@ -1,6 +1,8 @@
# Copyright 2017-2018 Onestein (<http://www.onestein.eu>) # Copyright 2017-2018 Onestein (<http://www.onestein.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
import json
from odoo.tests.common import TransactionCase, at_install, post_install from odoo.tests.common import TransactionCase, at_install, post_install
from odoo.exceptions import UserError from odoo.exceptions import UserError
@ -99,7 +101,7 @@ class TestBiViewEditor(TransactionCase):
'measure': 0 'measure': 0
} }
] ]
format_data = self.env['bve.view']._get_format_data(str(data))
format_data = json.dumps(data)
self.bi_view1_vals = { self.bi_view1_vals = {
'state': 'draft', 'state': 'draft',
@ -227,3 +229,23 @@ class TestBiViewEditor(TransactionCase):
# try to remove view # try to remove view
with self.assertRaises(UserError): with self.assertRaises(UserError):
bi_view.unlink() 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()
Loading…
Cancel
Save