Browse Source

[FIX] various improvement. Courtesy @dreispt review

pull/812/head
Sylvain LE GAL 8 years ago
parent
commit
7a382a6c33
  1. 2
      sql_export/demo/sql_export.xml
  2. 6
      sql_export/sql_export_view.xml
  3. 27
      sql_export/tests/test_sql_query.py
  4. 11
      sql_request_abstract/models/sql_request_mixin.py
  5. 2
      sql_request_abstract/security/ir_module_category.xml

2
sql_export/demo/sql_export.xml

@ -12,6 +12,6 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
<field name="query">SELECT name, street FROM res_partner;</field> <field name="query">SELECT name, street FROM res_partner;</field>
</record> </record>
<function model="sql.export" name="button_clean_check_request" eval="([ref('sql_export.sql_export_partner')])"/>
<function model="sql.export" name="button_validate_sql_expression" eval="([ref('sql_export.sql_export_partner')])"/>
</data></openerp> </data></openerp>

6
sql_export/sql_export_view.xml

@ -10,8 +10,8 @@
<form string="SQL export"> <form string="SQL export">
<sheet> <sheet>
<header> <header>
<button name="button_clean_check_request" type="object" states="draft"
string="Clean and Check Request" class="oe_highlight"/>
<button name="button_validate_sql_expression" type="object" states="draft"
string="Validate SQL Expression" class="oe_highlight"/>
<button name="button_set_draft" type="object" states="sql_valid" <button name="button_set_draft" type="object" states="sql_valid"
string="Set to Draft" groups="sql_request_abstract.group_sql_request_manager"/> string="Set to Draft" groups="sql_request_abstract.group_sql_request_manager"/>
<button name="export_sql_query" string="Execute Query" states="sql_valid" type="object" class="oe_highlight" <button name="export_sql_query" string="Execute Query" states="sql_valid" type="object" class="oe_highlight"
@ -74,6 +74,7 @@
<record id="sql_parameter_view_form" model="ir.ui.view"> <record id="sql_parameter_view_form" model="ir.ui.view">
<field name="name">Sql_parameter_form_view</field> <field name="name">Sql_parameter_form_view</field>
<field name="model">ir.model.fields</field> <field name="model">ir.model.fields</field>
<field name="priority">100</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<form string="SQL export"> <form string="SQL export">
</form> </form>
@ -83,6 +84,7 @@
<record id="sql_parameter_view_tree" model="ir.ui.view"> <record id="sql_parameter_view_tree" model="ir.ui.view">
<field name="name">Sql_parameter_tree_view</field> <field name="name">Sql_parameter_tree_view</field>
<field name="model">ir.model.fields</field> <field name="model">ir.model.fields</field>
<field name="priority">100</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<tree string="SQL Parameter"> <tree string="SQL Parameter">
<field name="name"/> <field name="name"/>

27
sql_export/tests/test_sql_query.py

@ -1,23 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
##############################################################################
#
# Author: Florian da Costa
# Copyright 2015 Akretion
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distnaributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# Copyright (C) 2015 Akretion (<http://www.akretion.com>)
# @author: Florian da Costa
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import base64 import base64
from openerp.tests.common import TransactionCase from openerp.tests.common import TransactionCase
from openerp.exceptions import Warning as UserError from openerp.exceptions import Warning as UserError
@ -57,7 +42,7 @@ class TestExportSqlQuery(TransactionCase):
sql_export = self.sql_export_obj.create({ sql_export = self.sql_export_obj.create({
'name': 'test_prohibited', 'name': 'test_prohibited',
'query': query}) 'query': query})
sql_export.button_clean_check_request()
sql_export.button_validate_sql_expression()
def test_authorized_queries(self): def test_authorized_queries(self):
authorized_queries = [ authorized_queries = [
@ -68,7 +53,7 @@ class TestExportSqlQuery(TransactionCase):
sql_export = self.sql_export_obj.create({ sql_export = self.sql_export_obj.create({
'name': 'test_authorized', 'name': 'test_authorized',
'query': query}) 'query': query})
sql_export.button_clean_check_request()
sql_export.button_validate_sql_expression()
self.assertEqual( self.assertEqual(
sql_export.state, 'sql_valid', sql_export.state, 'sql_valid',
"%s is a valid request" % (query)) "%s is a valid request" % (query))

11
sql_request_abstract/models/sql_request_mixin.py

@ -14,7 +14,7 @@ from openerp import _, api, fields, models
from openerp.exceptions import Warning as UserError from openerp.exceptions import Warning as UserError
class SQLRequestMixin(models.Model):
class SQLRequestMixin(models.AbstractModel):
_name = 'sql.request.mixin' _name = 'sql.request.mixin'
_clean_query_enabled = True _clean_query_enabled = True
@ -60,7 +60,7 @@ class SQLRequestMixin(models.Model):
query = fields.Text( query = fields.Text(
string='Query', required=True, help="You can't use the following words" string='Query', required=True, help="You can't use the following words"
": DELETE, DROP, CREATE, INSERT, ALTER, TRUNCATE, EXECUTE, UPDATE")
": DELETE, DROP, CREATE, INSERT, ALTER, TRUNCATE, EXECUTE, UPDATE.")
state = fields.Selection( state = fields.Selection(
string='State', selection=STATE_SELECTION, default='draft', string='State', selection=STATE_SELECTION, default='draft',
@ -82,7 +82,7 @@ class SQLRequestMixin(models.Model):
# Action Section # Action Section
@api.multi @api.multi
def button_clean_check_request(self):
def button_validate_sql_expression(self):
for item in self: for item in self:
if item._clean_query_enabled: if item._clean_query_enabled:
item._clean_query() item._clean_query()
@ -198,7 +198,10 @@ class SQLRequestMixin(models.Model):
self.env.cr.execute("SHOW server_version;") self.env.cr.execute("SHOW server_version;")
res = self.env.cr.fetchone()[0].split('.') res = self.env.cr.fetchone()[0].split('.')
minor_version = float('.'.join(res[:2])) minor_version = float('.'.join(res[:2]))
return minor_version >= 9.3
if minor_version < 9.3:
raise UserError(_(
"Materialized View requires PostgreSQL 9.3 or greater but"
" PostgreSQL %s is currently installed.") % (minor_version))
@api.multi @api.multi
def _clean_query(self): def _clean_query(self):

2
sql_request_abstract/security/ir_module_category.xml

@ -3,7 +3,7 @@
<openerp><data> <openerp><data>
<record model="ir.module.category" id="category_sql_abstract"> <record model="ir.module.category" id="category_sql_abstract">
<field name="name">Sql Request</field>
<field name="name">SQL Request</field>
</record> </record>
</data></openerp> </data></openerp>
Loading…
Cancel
Save