Guewen Baconnier
10 years ago
8 changed files with 243 additions and 27 deletions
-
1sql_view/__init__.py
-
38sql_view/__openerp__.py
-
78sql_view/models/sql_view.py
-
3sql_view/security/ir.model.access.csv
-
28sql_view/views/sql_view_views.xml
-
3sql_view/wizards/__init__.py
-
82sql_view/wizards/sql_view_csv_preview.py
-
37sql_view/wizards/sql_view_csv_preview_views.xml
@ -1,3 +1,4 @@ |
|||||
# -*- coding: utf-8 -*- |
# -*- coding: utf-8 -*- |
||||
|
|
||||
from . import models |
from . import models |
||||
|
from . import wizards |
@ -0,0 +1,3 @@ |
|||||
|
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink |
||||
|
access_sql_view_admin,sql_view admin,model_sql_view,base.group_system,1,1,1,1 |
||||
|
|
@ -0,0 +1,3 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
|
||||
|
from . import sql_view_csv_preview |
@ -0,0 +1,82 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
# |
||||
|
# |
||||
|
# Authors: Guewen Baconnier |
||||
|
# Copyright 2015 Camptocamp SA |
||||
|
# |
||||
|
# 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 distributed 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/>. |
||||
|
# |
||||
|
# |
||||
|
|
||||
|
import base64 |
||||
|
from StringIO import StringIO |
||||
|
|
||||
|
import unicodecsv |
||||
|
|
||||
|
from openerp.osv import orm, fields |
||||
|
|
||||
|
|
||||
|
class SQLViewCSVPreview(orm.TransientModel): |
||||
|
_name = 'sql.view.csv.preview' |
||||
|
|
||||
|
_columns = { |
||||
|
'limit': fields.integer(string='Limit', |
||||
|
help='Number of records. 0 means infinite.'), |
||||
|
'data': fields.binary('CSV', readonly=True), |
||||
|
'filename': fields.char('File Name', readonly=True), |
||||
|
} |
||||
|
|
||||
|
_defaults = { |
||||
|
'filename': 'csv-preview.csv', |
||||
|
'limit': 100, |
||||
|
} |
||||
|
|
||||
|
def _query(self, cr, uid, form, sql_view, context=None): |
||||
|
view_name = sql_view.complete_sql_name |
||||
|
query = "SELECT * FROM {view_name} " |
||||
|
if form.limit: |
||||
|
query += "LIMIT {limit}" |
||||
|
return query.format(view_name=view_name, limit=form.limit) |
||||
|
|
||||
|
def export_csv(self, cr, uid, ids, context=None): |
||||
|
if context is None: |
||||
|
return |
||||
|
sql_view_ids = context.get('active_ids', []) |
||||
|
assert len(ids) == 1, "1 wizard ID expected" |
||||
|
assert len(sql_view_ids) == 1, "1 active ID expected" |
||||
|
|
||||
|
form = self.browse(cr, uid, ids[0], context=context) |
||||
|
sql_view = self.pool['sql.view'].browse(cr, uid, sql_view_ids[0], |
||||
|
context=context) |
||||
|
query = self._query(cr, uid, form, sql_view, context=context) |
||||
|
cr.execute(query) |
||||
|
headers = [desc[0] for desc in cr.description] |
||||
|
records = cr.fetchall() |
||||
|
filedata = StringIO() |
||||
|
try: |
||||
|
writer = unicodecsv.writer(filedata, encoding='utf-8') |
||||
|
writer.writerow(headers) |
||||
|
writer.writerows(records) |
||||
|
form.write({'data': base64.encodestring(filedata.getvalue())}) |
||||
|
finally: |
||||
|
filedata.close() |
||||
|
return { |
||||
|
'type': 'ir.actions.act_window', |
||||
|
'res_model': self._name, |
||||
|
'view_mode': 'form', |
||||
|
'view_type': 'form', |
||||
|
'res_id': ids[0], |
||||
|
'views': [(False, 'form')], |
||||
|
'target': 'new', |
||||
|
} |
@ -0,0 +1,37 @@ |
|||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||
|
<openerp> |
||||
|
<data noupdate="0"> |
||||
|
<record id="view_sql_view_csv_preview_form" model="ir.ui.view"> |
||||
|
<field name="name">sql.view.csv.preview.form</field> |
||||
|
<field name="model">sql.view.csv.preview</field> |
||||
|
<field name="arch" type="xml"> |
||||
|
<form string="SQL View CSV Preview" version="7.0"> |
||||
|
<group> |
||||
|
<field name="limit" attrs="{'invisible': [('data', '!=', False)]}"/> |
||||
|
<field name="filename" invisible="1"/> |
||||
|
<field name="data" attrs="{'invisible': [('data', '=', False)]}" |
||||
|
filename="filename"/> |
||||
|
</group> |
||||
|
<footer attrs="{'invisible': [('data', '!=', False)]}"> |
||||
|
<button string="Preview" name="export_csv" type="object" class="oe_highlight"/> |
||||
|
or |
||||
|
<button string="Cancel" class="oe_link" special="cancel"/> |
||||
|
</footer> |
||||
|
<footer attrs="{'invisible': [('data', '=', False)]}"> |
||||
|
<button special="cancel" string="Close" type="object"/> |
||||
|
</footer> |
||||
|
</form> |
||||
|
</field> |
||||
|
</record> |
||||
|
|
||||
|
<record id="action_sql_view_csv_preview" model="ir.actions.act_window"> |
||||
|
<field name="name">SQL View CSV Preview</field> |
||||
|
<field name="res_model">sql.view.csv.preview</field> |
||||
|
<field name="view_type">form</field> |
||||
|
<field name="view_mode">form</field> |
||||
|
<field name="view_id" ref="view_sql_view_csv_preview_form"/> |
||||
|
<field name="target">new</field> |
||||
|
</record> |
||||
|
|
||||
|
</data> |
||||
|
</openerp> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue