Browse Source

[IMP] add support of variables in queries

pull/351/head
Benoit 9 years ago
committed by Sylvain LE GAL
parent
commit
dd113a5071
  1. 55
      sql_export/sql_export.py
  2. 38
      sql_export/sql_export_view.xml
  3. 2
      sql_export/tests/test_sql_query.py
  4. 90
      sql_export/wizard/wizard_file.py
  5. 16
      sql_export/wizard/wizard_file_view.xml

55
sql_export/sql_export.py

@ -19,13 +19,8 @@
#
##############################################################################
import StringIO
import base64
import datetime
import re
from openerp import models, fields, api, _, exceptions
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
import uuid
class SqlExport(models.Model):
@ -83,6 +78,14 @@ class SqlExport(models.Model):
'sql_id',
'user_id',
'Allowed Users')
field_ids = fields.Many2many(
'ir.model.fields',
'fields_sqlquery_rel',
'sql_id',
'field_id',
'Parameters',
domain=[('model', '=', 'sql.file.wizard')])
valid = fields.Boolean()
_constraints = [(_check_query_allowed,
'The query you want make is not allowed : prohibited '
@ -91,27 +94,10 @@ class SqlExport(models.Model):
@api.multi
def export_sql_query(self):
for obj in self:
today = datetime.datetime.now()
today_tz = fields.Datetime.context_timestamp(
obj, today)
date = today_tz.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
output = StringIO.StringIO()
query = "COPY (" + obj.query + ") TO STDOUT WITH " + \
obj.copy_options
name = 'export_query_%s' % uuid.uuid1().hex
self.env.cr.execute("SAVEPOINT %s" % name)
try:
self.env.cr.copy_expert(query, output)
output.getvalue()
new_output = base64.b64encode(output.getvalue())
output.close()
finally:
self.env.cr.execute("ROLLBACK TO SAVEPOINT %s" % name)
wiz = self.env['sql.file.wizard'].create(
{
'binary_file': new_output,
'file_name': obj.name + '_' + date + '.csv'})
self.ensure_one()
wiz = self.env['sql.file.wizard'].create({
'valid': self.valid,
'sql_export_id': self.id})
return {
'view_type': 'form',
'view_mode': 'form',
@ -129,18 +115,21 @@ class SqlExport(models.Model):
vals['query'] = vals['query'].strip()
if vals['query'][-1] == ';':
vals['query'] = vals['query'][:-1]
try:
self.env.cr.execute(vals['query'])
except:
raise exceptions.Warning(
_("The Sql query is not valid."))
finally:
self.env.cr.rollback()
# Can't test the query because of variables
# try:
# self.env.cr.execute(vals['query'])
# except:
# raise exceptions.Warning(
# _("The Sql query is not valid."))
# finally:
# self.env.cr.rollback()
return vals
@api.multi
def write(self, vals):
vals = self.check_query_syntax(vals)
if 'query' in vals:
vals['valid'] = False
return super(SqlExport, self).write(vals)
@api.model

38
sql_export/sql_export_view.xml

@ -18,6 +18,9 @@
<label for="Copy Options" colspan="1"/>
<field name="copy_options" nolabel="1" colspan="4"/>
</group>
<group groups="sql_export.group_sql_request_editor" string="Parameters">
<field name="field_ids" nolabel="1"/>
</group>
<group colspan="2" col="2" groups="sql_export.group_sql_request_editor">
<separator string="Allowed Users" colspan="1"/>
<separator string="Allowed Users Groups" colspan="1"/>
@ -33,8 +36,9 @@
<field name="name">Sql_export_tree_view</field>
<field name="model">sql.export</field>
<field name="arch" type="xml">
<tree string="SQL Export">
<field name="name"/>
<tree string="SQL Export" colors="red:valid == False">
<field name="name"/>
<field name="valid" invisible="1"/>
</tree>
</field>
</record>
@ -51,6 +55,36 @@
<menuitem id="sql_export_menu_view" name="Sql Export" parent="sql_export_menu" action="sql_export_tree_action" sequence="1"/>
<record id="sql_parameter_view_form" model="ir.ui.view">
<field name="name">Sql_parameter_form_view</field>
<field name="model">ir.model.fields</field>
<field name="arch" type="xml">
<form string="SQL export">
</form>
</field>
</record>
<record id="sql_parameter_view_tree" model="ir.ui.view">
<field name="name">Sql_parameter_tree_view</field>
<field name="model">ir.model.fields</field>
<field name="arch" type="xml">
<tree string="SQL Parameter">
<field name="name"/>
</tree>
</field>
</record>
<record id="sql_parameter_tree_action" model="ir.actions.act_window">
<field name="name">SQL Parameter</field>
<field name="res_model">ir.model.fields</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="context" eval="{'default_model_id': ref('sql_export.model_sql_file_wizard'), 'default_size': 64, 'search_default_state': 'manual'}"/>
<field name="domain">[('model','=','sql.file.wizard')]</field>
</record>
<menuitem id="sql_parameter_menu_view" name="Sql Export Variables" parent="sql_export_menu" action="sql_parameter_tree_action" sequence="5"/>
</data>
</openerp>

2
sql_export/tests/test_sql_query.py

@ -40,6 +40,8 @@ class TestExportSqlQuery(TransactionCase):
def test_sql_query(self):
test = self.sql_model.export_sql_query(
self.cr, self.uid, [self.query_id])
self.registry('sql.file.wizard').export_sql(
self.cr, self.uid, test['res_id'])
wizard = self.registry('sql.file.wizard').browse(
self.cr, self.uid, test['res_id'])
export = base64.b64decode(wizard.binary_file)

90
sql_export/wizard/wizard_file.py

@ -18,12 +18,98 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp import models, fields
from openerp import models, fields, api
from openerp.osv.orm import setup_modifiers
import StringIO
import base64
import datetime
from lxml import etree
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
import uuid
class SqlFileWizard(models.TransientModel):
_name = "sql.file.wizard"
_description = "Allow the user to save the file with sql request's data"
binary_file = fields.Binary('File', required=True, readonly=True)
binary_file = fields.Binary('File', readonly=True)
file_name = fields.Char('File Name', readonly=True)
valid = fields.Boolean()
sql_export_id = fields.Many2one(comodel_name='sql.export', required=True)
@api.model
def fields_view_get(self, view_id=None, view_type='form',
toolbar=False, submenu=False):
"""
Display dinamicaly parameter fields depending on the sql_export.
"""
res = super(SqlFileWizard, self).fields_view_get(
view_id=view_id, view_type=view_type, toolbar=toolbar,
submenu=submenu)
export_obj = self.env['sql.export']
if view_type == 'form':
sql_export = export_obj.browse(self._context.get('active_id'))
if sql_export.field_ids:
eview = etree.fromstring(res['arch'])
group = etree.Element(
'group', name="variables_group", colspan="4")
toupdate_fields = []
for field in sql_export.field_ids:
kwargs = {'name': "%s" % field.name}
toupdate_fields.append(field.name)
view_field = etree.SubElement(group, 'field', **kwargs)
setup_modifiers(view_field, self.fields_get(field.name))
res['fields'].update(self.fields_get(toupdate_fields))
placeholder = eview.xpath(
"//separator[@string='variables_placeholder']")[0]
placeholder.getparent().replace(
placeholder, group)
res['arch'] = etree.tostring(eview, pretty_print=True)
return res
@api.multi
def export_sql(self):
self.ensure_one()
sql_export = self.sql_export_id
today = datetime.datetime.now()
today_tz = fields.Datetime.context_timestamp(
sql_export, today)
date = today_tz.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
output = StringIO.StringIO()
variable_dict = {}
if sql_export.field_ids:
for field in sql_export.field_ids:
variable_dict[field.name] = self[field.name]
if "%(company_id)s" in sql_export.query:
variable_dict['company_id'] = self.env.user.company_id.id
if "%(user_id)s" in sql_export.query:
variable_dict['user_id'] = self._uid
format_query = self.env.cr.mogrify(sql_export.query, variable_dict)
query = "COPY (" + format_query + ") TO STDOUT WITH " + \
sql_export.copy_options
name = 'export_query_%s' % uuid.uuid1().hex
self.env.cr.execute("SAVEPOINT %s" % name)
try:
self.env.cr.copy_expert(query, output)
output.getvalue()
new_output = base64.b64encode(output.getvalue())
output.close()
finally:
self.env.cr.execute("ROLLBACK TO SAVEPOINT %s" % name)
wiz = self.write(
{
'binary_file': new_output,
'file_name': sql_export.name + '_' + date + '.csv'})
if not sql_export.valid:
sql_export.sudo().valid = True
return {
'view_type': 'form',
'view_mode': 'form',
'res_model': 'sql.file.wizard',
'res_id': self.id,
'type': 'ir.actions.act_window',
'target': 'new',
'context': self._context,
'nodestroy': True,
}

16
sql_export/wizard/wizard_file_view.xml

@ -7,8 +7,20 @@
<field name="model">sql.file.wizard</field>
<field name="arch" type="xml">
<form string="Csv File">
<field name="binary_file" filename="file_name"/>
<field name="file_name" invisible="1"/>
<separator string="Warning untested export" attrs="{'invisible': [('valid', '=', True)]}"/>
<separator string="variables_placeholder" colspan="4"/>
<separator string="Export file" colspan="4"
attrs="{'invisible': [('binary_file', '=', False)]}"/>
<field name="valid" invisible="1"/>
<field name="binary_file" filename="file_name"/>
<field name="file_name" invisible="1"/>
<footer>
<button name="export_sql" string="Export" type="object"
icon="gtk-apply" />
or
<button special="cancel" string="Cancel" type="object"
icon="gtk-cancel" />
</footer>
</form>
</field>
</record>

Loading…
Cancel
Save