Merge lp:~florian-dacosta/server-env-tools/sql_query_export_csv_file into lp:~server-env-tools-core-editors/server-env-tools/7.0

Proposed by Florian da Costa
Status: Work in progress
Proposed branch: lp:~florian-dacosta/server-env-tools/sql_query_export_csv_file
Merge into: lp:~server-env-tools-core-editors/server-env-tools/7.0
Diff against target: 544 lines (+491/-0)
10 files modified
sql_export/__init__.py (+24/-0)
sql_export/__openerp__.py (+50/-0)
sql_export/i18n/fr.po (+132/-0)
sql_export/security/ir.model.access.csv (+3/-0)
sql_export/security/sql_export_security.xml (+21/-0)
sql_export/sql.py (+116/-0)
sql_export/sql_view.xml (+63/-0)
sql_export/wizard/__init__.py (+23/-0)
sql_export/wizard/wizard_file.py (+41/-0)
sql_export/wizard/wizard_file_view.xml (+18/-0)
To merge this branch: bzr merge lp:~florian-dacosta/server-env-tools/sql_query_export_csv_file
Reviewer Review Type Date Requested Status
Sébastien BEAU - http://www.akretion.com Pending
Review via email: mp+213826@code.launchpad.net

Description of the change

Add a module named sql_export which allow a group of user to create and edit a new object with a sql query.
This query can be executed by the chosen groups or users in order to export the data of the query in a CSV file.

To post a comment you must log in.
68. By Florian da Costa

[FIX] Fix encoding error in sql export module

69. By Florian da Costa

[FIX] Change logic and copy directly the result of the query to csv file

70. By Florian da Costa

[FIX] Fix previous commit mistake

Unmerged revisions

70. By Florian da Costa

[FIX] Fix previous commit mistake

69. By Florian da Costa

[FIX] Change logic and copy directly the result of the query to csv file

68. By Florian da Costa

[FIX] Fix encoding error in sql export module

67. By Florian da Costa

[ADD] Add sql export module : allow user to export data in csv file from a sql query

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added directory 'sql_export'
=== added file 'sql_export/__init__.py'
--- sql_export/__init__.py 1970-01-01 00:00:00 +0000
+++ sql_export/__init__.py 2014-04-04 14:08:54 +0000
@@ -0,0 +1,24 @@
1# -*- coding: utf-8 -*-
2###############################################################################
3#
4# action_server_email for OpenERP
5# Copyright (C) 2013-TODAY Akretion <http://www.akretion.com>.
6# @author Florian DA COSTA <florian.dacosta@akretion.com>
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU Affero General Public License as
10# published by the Free Software Foundation, either version 3 of the
11# License, or (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU Affero General Public License for more details.
17#
18# You should have received a copy of the GNU Affero General Public License
19# along with this program. If not, see <http://www.gnu.org/licenses/>.
20#
21###############################################################################
22
23import sql
24import wizard
025
=== added file 'sql_export/__openerp__.py'
--- sql_export/__openerp__.py 1970-01-01 00:00:00 +0000
+++ sql_export/__openerp__.py 2014-04-04 14:08:54 +0000
@@ -0,0 +1,50 @@
1# -*- coding: utf-8 -*-
2###############################################################################
3#
4# action_server_email for OpenERP
5# Copyright (C) 2013-TODAY Akretion <http://www.akretion.com>.
6# @author Florian DA COSTA <florian.dacosta@akretion.com>
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU Affero General Public License as
10# published by the Free Software Foundation, either version 3 of the
11# License, or (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU Affero General Public License for more details.
17#
18# You should have received a copy of the GNU Affero General Public License
19# along with this program. If not, see <http://www.gnu.org/licenses/>.
20#
21###############################################################################
22
23
24{
25 'name': 'SQL Export',
26 'version': '0.1',
27 'category': 'Generic Modules/Others',
28 'license': 'AGPL-3',
29 'description':
30 """
31 Allow to execute sql query from openerp interface in order to export datas in csv files.
32 To edit or create a query, the user have to be in sql_query editor group.
33 To execute a query, the user or group of user have to be specified in the sql_export record.
34
35 """,
36 'author': 'Akretion',
37 'website': 'http://www.akretion.com/',
38 'depends': [
39 "base",
40 ],
41 'init_xml': [],
42 'update_xml': [
43 'sql_view.xml',
44 'wizard/wizard_file_view.xml',
45 'security/sql_export_security.xml',
46 'security/ir.model.access.csv',
47 ],
48 'demo_xml': [],
49 'installable': True,
50}
051
=== added directory 'sql_export/i18n'
=== added file 'sql_export/i18n/fr.po'
--- sql_export/i18n/fr.po 1970-01-01 00:00:00 +0000
+++ sql_export/i18n/fr.po 2014-04-04 14:08:54 +0000
@@ -0,0 +1,132 @@
1# Translation of OpenERP Server.
2# This file contains the translation of the following modules:
3# * sql_export
4#
5msgid ""
6msgstr ""
7"Project-Id-Version: OpenERP Server 7.0\n"
8"Report-Msgid-Bugs-To: \n"
9"POT-Creation-Date: 2014-04-01 08:39+0000\n"
10"PO-Revision-Date: 2014-04-01 08:39+0000\n"
11"Last-Translator: <>\n"
12"Language-Team: \n"
13"MIME-Version: 1.0\n"
14"Content-Type: text/plain; charset=UTF-8\n"
15"Content-Transfer-Encoding: \n"
16"Plural-Forms: \n"
17
18#. module: sql_export
19#: model:ir.ui.menu,name:sql_export.menu_export
20msgid "Export"
21msgstr "Export"
22
23#. module: sql_export
24#: code:addons/sql_export/sql.py:103
25#: code:addons/sql_export/sql.py:114
26#, python-format
27msgid "Invalid Query"
28msgstr "Requête Invalide"
29
30#. module: sql_export
31#: model:res.groups,name:sql_export.group_sql_request_editor
32msgid "Sql Request Editor"
33msgstr "Edition de Requête SQL"
34
35#. module: sql_export
36#: field:sql.export,name:0
37msgid "Name"
38msgstr "Nom"
39
40#. module: sql_export
41#: model:ir.model,name:sql_export.model_sql_file_wizard
42msgid "Allow to the user to save the file with sql request's data"
43msgstr "Permet à l'utilisateur de sauvegarder le fichier contenant les données de la requête SQL"
44
45#. module: sql_export
46#: code:addons/sql_export/sql.py:104
47#: code:addons/sql_export/sql.py:115
48#, python-format
49msgid "The Sql query is not valid."
50msgstr "La requête SQL n'est pas valide"
51
52#. module: sql_export
53#: code:addons/sql_export/sql.py:72
54#, python-format
55msgid "No data"
56msgstr "Pas de données"
57
58#. module: sql_export
59#: model:ir.model,name:sql_export.model_sql_export
60#: view:sql.export:0
61msgid "SQL export"
62msgstr "SQL export"
63
64#. module: sql_export
65#: field:sql.file.wizard,file_name:0
66msgid "File Name"
67msgstr "Nom du fichier"
68
69#. module: sql_export
70#: model:ir.actions.act_window,name:sql_export.sql_export_tree_action
71#: view:sql.export:0
72msgid "SQL Export"
73msgstr "SQL Export"
74
75#. module: sql_export
76#: field:sql.export,group_ids:0
77msgid "Allowed Groups"
78msgstr "Groupes Autorisés"
79
80#. module: sql_export
81#: model:ir.ui.menu,name:sql_export.sql_export_menu
82#: model:ir.ui.menu,name:sql_export.sql_export_menu_view
83msgid "Sql Export"
84msgstr "Sql Export"
85
86#. module: sql_export
87#: constraint:sql.export:0
88msgid "The query you want make is not allowed : prohibited actions (Delete, drop...)"
89msgstr "La requête que vous voulez sauvegarder n'est pas autorisée : actions interdites (Delete, drop...)"
90
91#. module: sql_export
92#: view:sql.file.wizard:0
93msgid "Csv File"
94msgstr "Fichier CSV"
95
96#. module: sql_export
97#: view:sql.export:0
98msgid "Execute Query"
99msgstr "Exécuter"
100
101#. module: sql_export
102#: field:sql.file.wizard,file:0
103msgid "File"
104msgstr "Fichier"
105
106#. module: sql_export
107#: field:sql.export,query:0
108msgid "Query"
109msgstr "Query"
110
111#. module: sql_export
112#: view:sql.export:0
113msgid "Allowed Users Groups"
114msgstr "Groupes d'utilisateurs Autorisés"
115
116#. module: sql_export
117#: help:sql.export,query:0
118msgid "You can't use the following word : delete, drop, create, insert, update, alter, truncate, execute"
119msgstr "Vous ne pouvez pas utilisés les mots suivants : delete, drop, create, insert, update, alter, truncate, execute"
120
121#. module: sql_export
122#: code:addons/sql_export/sql.py:73
123#, python-format
124msgid "The query did not return any data."
125msgstr "La requête n'a retourné aucune donnée"
126
127#. module: sql_export
128#: view:sql.export:0
129#: field:sql.export,user_ids:0
130msgid "Allowed Users"
131msgstr "Utilisateurs Autorisés"
132
0133
=== added directory 'sql_export/security'
=== added file 'sql_export/security/ir.model.access.csv'
--- sql_export/security/ir.model.access.csv 1970-01-01 00:00:00 +0000
+++ sql_export/security/ir.model.access.csv 2014-04-04 14:08:54 +0000
@@ -0,0 +1,3 @@
1"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
2"access_sql_export_all","access_sql_export_all","model_sql_export",,1,0,0,0
3"access_sql_export_editor","access_sql_export_editor","model_sql_export",group_sql_request_editor,1,1,1,1
04
=== added file 'sql_export/security/sql_export_security.xml'
--- sql_export/security/sql_export_security.xml 1970-01-01 00:00:00 +0000
+++ sql_export/security/sql_export_security.xml 2014-04-04 14:08:54 +0000
@@ -0,0 +1,21 @@
1<?xml version="1.0" encoding="utf-8"?>
2<openerp>
3<data noupdate="0">
4
5 <record model="res.groups" id="group_sql_request_editor">
6 <field name="name">Sql Request Editor</field>
7 <field name="users" eval="[(4, ref('base.user_root'))]"/>
8 </record>
9
10 <record model="ir.rule" id="sql_export_restric_access_user_or_group">
11 <field name="name" >SQL Export users and groups rules</field>
12 <field name="model_id" ref="model_sql_export"/>
13 <field eval="1" name="perm_read"/>
14 <field eval="0" name="perm_create"/>
15 <field eval="0" name="perm_write"/>
16 <field eval="0" name="perm_unlink"/>
17 <field name="domain_force">['|', ('user_ids','=',user.id), ('group_ids','=', [x.id for x in user.groups_id])]</field>
18 </record>
19
20</data>
21</openerp>
022
=== added file 'sql_export/sql.py'
--- sql_export/sql.py 1970-01-01 00:00:00 +0000
+++ sql_export/sql.py 2014-04-04 14:08:54 +0000
@@ -0,0 +1,116 @@
1# -*- coding: utf-8 -*-
2###############################################################################
3#
4# action_server_email for OpenERP
5# Copyright (C) 2013-TODAY Akretion <http://www.akretion.com>.
6# @author Florian DA COSTA <florian.dacosta@akretion.com>
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU Affero General Public License as
10# published by the Free Software Foundation, either version 3 of the
11# License, or (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU Affero General Public License for more details.
17#
18# You should have received a copy of the GNU Affero General Public License
19# along with this program. If not, see <http://www.gnu.org/licenses/>.
20#
21###############################################################################
22
23from openerp.osv.orm import Model
24from openerp.osv import fields, orm
25from openerp.tools.translate import _
26import StringIO
27import csv
28import base64
29import time
30
31
32PROHIBITED_WORDS = ['delete', 'drop', 'insert', 'alter', 'truncate', 'execute']
33
34
35class SqlExport(Model):
36
37 _name = "sql.export"
38
39 _description = "SQL export"
40
41
42 def _check_query(self, cr, uid, ids, context=None):
43 for obj in self.browse(cr, uid, ids, context=context):
44 if any(word in obj.query.lower() for word in PROHIBITED_WORDS):
45 return False
46 if obj.query.split(' ')[0].lower() != 'select':
47 return False
48 return True
49
50 def _get_editor_group(self, cr, uid, *args):
51 gr_obj = self.pool.get('res.groups')
52 editors = gr_obj.search(cr, uid, [('name', '=', 'Sql Request Editor')])
53 return editors
54
55
56 _columns = {
57 'name': fields.char('Name', required=True),
58 'query': fields.text('Query', required=True, help="You can't use the following word : delete, drop, create, insert, update, alter, truncate, execute"),
59 'group_ids': fields.many2many('res.groups', 'groups_sqlquery_rel', 'sql_id', 'group_id', 'Allowed Groups'),
60 'user_ids': fields.many2many('res.users', 'users_sqlquery_rel', 'sql_id', 'user_id', 'Allowed Users'),
61 }
62
63
64 _defaults = {
65 'group_ids' : _get_editor_group,
66 }
67
68 _constraints = [(_check_query, 'The query you want make is not allowed : prohibited actions (Delete, drop...)', ['query'])]
69
70 def export_sql_query(self, cr, uid, ids, context=None):
71 if not context:
72 context = {}
73 for obj in self.browse(cr, uid, ids, context=context):
74 now = time.strftime('%Y-%m-%d',time.localtime())
75 output = StringIO.StringIO()
76 query = "COPY (" + obj.query + ") TO STDOUT WITH CSV HEADER DELIMITER ';'"
77 cr.copy_expert(query, output)
78 output.getvalue()
79 new_output = base64.b64encode(output.getvalue())
80 output.close()
81 wiz = self.pool.get('sql.file.wizard').create(cr, uid, {'file': new_output, 'file_name': obj.name + '_' + now + '.csv'})
82 cr.commit()
83 return {
84 'view_type': 'form',
85 'view_mode': 'form',
86 'res_model': 'sql.file.wizard',
87 'res_id': wiz,
88 'type': 'ir.actions.act_window',
89 'target': 'new',
90 'context': context,
91 'nodestroy': True,
92 }
93
94
95 def write(self, cr, uid, ids, vals, context=None):
96 if vals:
97 if 'query' in vals:
98 try:
99 cr.execute(vals['query'])
100 except:
101 raise orm.except_orm(_("Invalid Query"),
102 _("The Sql query is not valid."))
103 return super(SqlExport, self).write(cr, uid, ids, vals, context=context)
104
105
106 def create(self, cr, uid, vals, context=None):
107 if vals:
108 if 'query' in vals:
109 try:
110 cr.execute(vals['query'])
111 except:
112 raise orm.except_orm(_("Invalid Query"),
113 _("The Sql query is not valid."))
114 return super(SqlExport, self).create(cr, uid, vals, context=context)
115
116
0117
=== added file 'sql_export/sql_view.xml'
--- sql_export/sql_view.xml 1970-01-01 00:00:00 +0000
+++ sql_export/sql_view.xml 2014-04-04 14:08:54 +0000
@@ -0,0 +1,63 @@
1<?xml version="1.0" encoding="utf-8"?>
2<openerp>
3<data>
4
5<menuitem name="Export"
6 id="menu_export"
7 groups="base.group_user"
8 sequence="190"/>
9
10
11
12
13
14
15 <record id="sql_export_view_form" model="ir.ui.view">
16 <field name="name">Sql_export_form_view</field>
17 <field name="model">sql.export</field>
18 <field name="arch" type="xml">
19 <form string="SQL export">
20 <group col="2">
21 <group colspan="2" col="5">
22 <label for="name" colspan="1"/>
23 <field name="name" colspan="2" nolabel="1"/>
24 <button name="export_sql_query" string="Execute Query" type="object" class="oe_highlight" icon="gtk-execute" colspan="2"/>
25 <label for="query" colspan="1"/>
26 <field name="query" nolabel="1" colspan="4"/>
27 </group>
28 <group colspan="2" col="2" groups="sql_export.group_sql_request_editor">
29 <separator string="Allowed Users" colspan="1"/>
30 <separator string="Allowed Users Groups" colspan="1"/>
31 <field name="user_ids" nolabel="1"/>
32 <field name="group_ids" nolabel="1"/>
33 </group>
34 </group>
35 </form>
36 </field>
37 </record>
38
39 <record id="sql_export_view_tree" model="ir.ui.view">
40 <field name="name">Sql_export_tree_view</field>
41 <field name="model">sql.export</field>
42 <field name="arch" type="xml">
43 <tree string="SQL Export">
44 <field name="name"/>
45 </tree>
46 </field>
47 </record>
48
49 <record id="sql_export_tree_action" model="ir.actions.act_window">
50 <field name="name">SQL Export</field>
51 <field name="res_model">sql.export</field>
52 <field name="view_type">form</field>
53 <field name="view_mode">tree,form</field>
54 </record>
55
56
57 <menuitem id="sql_export_menu" name="Sql Export" parent="menu_export" sequence="1"/>
58
59 <menuitem id="sql_export_menu_view" name="Sql Export" parent="sql_export_menu" action="sql_export_tree_action" sequence="1"/>
60
61
62</data>
63</openerp>
064
=== added directory 'sql_export/wizard'
=== added file 'sql_export/wizard/__init__.py'
--- sql_export/wizard/__init__.py 1970-01-01 00:00:00 +0000
+++ sql_export/wizard/__init__.py 2014-04-04 14:08:54 +0000
@@ -0,0 +1,23 @@
1# -*- coding: utf-8 -*-
2###############################################################################
3#
4# action_server_email for OpenERP
5# Copyright (C) 2013-TODAY Akretion <http://www.akretion.com>.
6# @author Florian DA COSTA <florian.dacosta@akretion.com>
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU Affero General Public License as
10# published by the Free Software Foundation, either version 3 of the
11# License, or (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU Affero General Public License for more details.
17#
18# You should have received a copy of the GNU Affero General Public License
19# along with this program. If not, see <http://www.gnu.org/licenses/>.
20#
21###############################################################################
22
23import wizard_file
024
=== added file 'sql_export/wizard/wizard_file.py'
--- sql_export/wizard/wizard_file.py 1970-01-01 00:00:00 +0000
+++ sql_export/wizard/wizard_file.py 2014-04-04 14:08:54 +0000
@@ -0,0 +1,41 @@
1# -*- coding: utf-8 -*-
2###############################################################################
3#
4# action_server_email for OpenERP
5# Copyright (C) 2013-TODAY Akretion <http://www.akretion.com>.
6# @author Florian DA COSTA <florian.dacosta@akretion.com>
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU Affero General Public License as
10# published by the Free Software Foundation, either version 3 of the
11# License, or (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU Affero General Public License for more details.
17#
18# You should have received a copy of the GNU Affero General Public License
19# along with this program. If not, see <http://www.gnu.org/licenses/>.
20#
21###############################################################################
22
23from openerp.osv.orm import Model
24from openerp.osv import fields, orm
25from openerp.tools.translate import _
26
27
28class SqlFileWizard(orm.TransientModel):
29
30 _name = "sql.file.wizard"
31
32 _description = "Allow to the user to save the file with sql request's data"
33
34
35 _columns = {
36 'file': fields.binary('File', required=True, readonly=True),
37 'file_name': fields.char('File Name', readonly=True),
38 }
39
40
41
042
=== added file 'sql_export/wizard/wizard_file_view.xml'
--- sql_export/wizard/wizard_file_view.xml 1970-01-01 00:00:00 +0000
+++ sql_export/wizard/wizard_file_view.xml 2014-04-04 14:08:54 +0000
@@ -0,0 +1,18 @@
1<?xml version="1.0" encoding="utf-8"?>
2<openerp>
3 <data>
4
5 <record id="sql_file_wizard_view_form" model="ir.ui.view">
6 <field name="name">sql.file.wizard.view.form</field>
7 <field name="model">sql.file.wizard</field>
8 <field name="arch" type="xml">
9 <form string="Csv File">
10 <field name="file" filename="file_name"/>
11 <field name="file_name" invisible="1"/>
12 </form>
13 </field>
14 </record>
15
16
17 </data>
18</openerp>