Browse Source

add uninstall hook. Add checks when deleting records.

pull/162/head
aheficent 7 years ago
committed by Darshan Patel
parent
commit
e04dd70cda
  1. 1
      bi_sql_editor/__init__.py
  2. 1
      bi_sql_editor/__openerp__.py
  3. 88
      bi_sql_editor/demo/bi_sql_view_demo.xml
  4. 52
      bi_sql_editor/hooks.py
  5. 37
      bi_sql_editor/models/bi_sql_view.py
  6. 5
      bi_sql_editor/tests/__init__.py
  7. 56
      bi_sql_editor/tests/test_bi_sql_view.py

1
bi_sql_editor/__init__.py

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from . import models
from .hooks import uninstall_hook

1
bi_sql_editor/__openerp__.py

@ -25,4 +25,5 @@
'demo/bi_sql_view_demo.xml',
],
'installable': True,
'uninstall_hook': 'uninstall_hook'
}

88
bi_sql_editor/demo/bi_sql_view_demo.xml

@ -5,48 +5,49 @@ Copyright (C) 2014 - Today GRAP (http://www.grap.coop)
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
-->
<openerp><data noupdate="1">
<record id="incorrect_sql_view" model="bi.sql.view">
<field name="name">Draft Incorrect SQL View</field>
<field name="technical_name">incorrect_view</field>
<field name="query"><![CDATA[
SELECT *
FROM unexisting_table
ORDER BY unexisting_field
]]>
</field>
</record>
<record id="partner_sql_view" model="bi.sql.view">
<field name="name">Partners View</field>
<field name="technical_name">partners_view</field>
<field name="query"><![CDATA[
SELECT
name as x_name,
street as x_street,
company_id as x_company_id
FROM res_partner
ORDER BY name
]]>
</field>
</record>
<record id="module_sql_view" model="bi.sql.view">
<field name="name">Modules by Authors</field>
<field name="technical_name">modules_view</field>
<field name="is_materialized" eval="0" />
<field name="query"><![CDATA[
SELECT
name as x_name,
case
when author ilike '%OpenERP SA%' THEN 'Odoo SA'
when author ilike '%Odoo Community Association (OCA)%' THEN 'OCA'
else 'Undefined Author' END as x_author_type
FROM ir_module_module
]]>
</field>
</record>
<odoo>
<data noupdate="1">
<record id="incorrect_sql_view" model="bi.sql.view">
<field name="name">Draft Incorrect SQL View</field>
<field name="technical_name">incorrect_view</field>
<field name="query"><![CDATA[
SELECT *
FROM unexisting_table
ORDER BY unexisting_field
]]>
</field>
</record>
<record id="partner_sql_view" model="bi.sql.view">
<field name="name">Partners View</field>
<field name="technical_name">partners_view</field>
<field name="query"><![CDATA[
SELECT
name as x_name,
street as x_street,
company_id as x_company_id
FROM res_partner
ORDER BY name
]]>
</field>
</record>
<record id="module_sql_view" model="bi.sql.view">
<field name="name">Modules by Authors</field>
<field name="technical_name">modules_view</field>
<field name="is_materialized" eval="0" />
<field name="query"><![CDATA[
SELECT
name as x_name,
case
when author ilike '%OpenERP SA%' THEN 'Odoo SA'
when author ilike '%Odoo Community Association (OCA)%' THEN 'OCA'
else 'Undefined Author' END as x_author_type
FROM ir_module_module
]]>
</field>
</record>
<function model="bi.sql.view" name="button_validate_sql_expression" eval="([ref('module_sql_view')])"/>
@ -54,4 +55,5 @@ FROM ir_module_module
<function model="bi.sql.view" name="button_create_ui" eval="([ref('module_sql_view')])"/>
</data></openerp>
</data>
</odoo>

52
bi_sql_editor/hooks.py

@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2017 Onestein (<http://www.onestein.eu>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openerp import SUPERUSER_ID
from openerp.api import Environment
def uninstall_hook(cr, registry):
env = Environment(cr, SUPERUSER_ID, {})
recs = env['bi.sql.view'].search([])
for rec in recs:
rec.button_set_draft()
rec.unlink()
# delete dirty data that could cause problems
# while re-installing the module
# Drop materialized views
cr.execute("""
select relname
from pg_class
where relname like 'x_bi_sql_view%' and relkind='m'
""")
for r in cr.fetchall():
cr.execute("""
DROP MATERIALIZED VIEW %s
""" % r)
cr.execute("""
select relname
from pg_class
where relname like 'x_bi_sql_view%' and relkind='r'
""")
for r in cr.fetchall():
cr.execute("""
DROP TABLE %s
""" % r)
cr.execute("""
select table_name from INFORMATION_SCHEMA.views
where table_name like 'x_bi_sql%'""")
# Drop not materialized views
for v in cr.fetchall():
cr.execute("""
DROP VIEW %s
""" % v)
# Drop table if uninstalling went wrong
cr.execute("""
delete from ir_model_fields where model like 'bi.sql.view%';
delete from ir_model_fields where model like 'bi_sql_%';
delete from ir_model where model like 'x_bi_sql_view.%';
""")

37
bi_sql_editor/models/bi_sql_view.py

@ -208,13 +208,20 @@ class BiSQLView(models.Model):
# Drop ORM
sql_view._drop_model_and_fields()
sql_view.tree_view_id.unlink()
sql_view.graph_view_id.unlink()
sql_view.pivot_view_id.unlink()
sql_view.search_view_id.unlink()
sql_view.action_id.unlink()
sql_view.menu_id.unlink()
sql_view.rule_id.unlink()
if sql_view.tree_view_id:
sql_view.tree_view_id.unlink()
if sql_view.graph_view_id:
sql_view.graph_view_id.unlink()
if sql_view.pivot_view_id:
sql_view.pivot_view_id.unlink()
if sql_view.search_view_id:
sql_view.search_view_id.unlink()
if sql_view.action_id:
sql_view.action_id.unlink()
if sql_view.menu_id:
sql_view.menu_id.unlink()
if sql_view.rule_id:
sql_view.rule_id.unlink()
if sql_view.cron_id:
sql_view.cron_id.unlink()
sql_view.write({'state': 'draft', 'has_group_changed': False})
@ -409,9 +416,14 @@ class BiSQLView(models.Model):
@api.multi
def _drop_view(self):
for sql_view in self:
self._log_execute(
"DROP %s VIEW IF EXISTS %s" % (
sql_view.materialized_text, sql_view.view_name))
try:
self._log_execute(
"DROP %s VIEW IF EXISTS %s" % (
sql_view.materialized_text, sql_view.view_name))
except ProgrammingError as e:
# If it is not a materialized view will raise an error,
# and we pass.
continue
sql_view.size = False
@api.multi
@ -464,7 +476,8 @@ class BiSQLView(models.Model):
@api.multi
def _drop_model_and_fields(self):
for sql_view in self:
sql_view.model_id.unlink()
if sql_view.model_id:
sql_view.model_id.unlink()
@api.multi
def _hook_executed_request(self):
@ -547,7 +560,7 @@ class BiSQLView(models.Model):
@api.multi
def _refresh_materialized_view(self):
for sql_view in self:
for sql_view in self.filtered(lambda v: v.is_materialized == True):
req = "REFRESH %s VIEW %s" % (
sql_view.materialized_text, sql_view.view_name)
self._log_execute(req)

5
bi_sql_editor/tests/__init__.py

@ -1,5 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Onestein (<http://www.onestein.eu>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import test_bi_sql_view

56
bi_sql_editor/tests/test_bi_sql_view.py

@ -1,56 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Onestein (<http://www.onestein.eu>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openerp.tests.common import TransactionCase, at_install, post_install
from openerp.exceptions import Warning as UserError
@at_install(False)
@post_install(True)
class TestBiSqlViewEditor(TransactionCase):
def setUp(self):
super(TestBiSqlViewEditor, self).setUp()
self.res_partner = self.env['res.partner']
self.res_users = self.env['res.users']
self.bi_sql_view = self.env['bi.sql.view']
self.view = self.env.ref(
'bi_sql_editor.partner_sql_view')
# deleting the existing views otherwise it fails
self.view.state = 'model_valid'
self.view.button_set_draft()
self.group_bi_user = self.env.ref(
'sql_request_abstract.group_sql_request_user')
self.group_user = self.env.ref(
'base.group_user')
self.company = self.env.ref('base.main_company')
def test_process_view(self):
self.assertEqual(self.view.state, 'draft', 'state not draft')
self.view.button_validate_sql_expression()
self.assertEqual(self.view.state, 'sql_valid', 'state not sql_valid')
self.view._check_execution()
for field in self.view.bi_sql_view_field_ids:
field.graph_type = 'row'
self.view.button_create_sql_view_and_model()
self.assertEqual(self.view.state, 'model_valid',
'state not model_valid')
self.view.button_create_ui()
self.assertEqual(self.view.state, 'ui_valid', 'state not ui_valid')
self.view.button_open_view()
self.view.button_set_draft()
def test_copy(self):
self.assertEqual(self.view.mod, 'draft', 'state not draft')
copy_view = self.view.copy()
self.assertEqual(copy_view.name, 'Partners View (Copy)', 'Wrong name')
def test_unlink(self):
self.assertEqual(self.view.state, 'draft', 'state not draft')
self.view.button_validate_sql_expression()
self.view.button_create_sql_view_and_model()
with self.assertRaises(UserError):
self.view.unlink()
self.view.button_set_draft()
self.view.unlink()
Loading…
Cancel
Save