You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.0 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. #
  4. # Authors: Guewen Baconnier
  5. # Copyright 2015 Camptocamp SA
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. #
  21. import base64
  22. from StringIO import StringIO
  23. try:
  24. import unicodecsv
  25. except ImportError:
  26. unicodecsv = None
  27. from openerp.osv import orm, fields
  28. from openerp.tools.translate import _
  29. class SQLViewCSVPreview(orm.TransientModel):
  30. _name = 'sql.view.csv.preview'
  31. _description = 'SQL View CSV Preview'
  32. _columns = {
  33. 'limit': fields.integer(string='Limit',
  34. help='Number of records. 0 means infinite.'),
  35. 'data': fields.binary('CSV', readonly=True),
  36. 'filename': fields.char('File Name', readonly=True),
  37. }
  38. _defaults = {
  39. 'filename': 'csv-preview.csv',
  40. 'limit': 100,
  41. }
  42. def _query(self, cr, uid, form, sql_view, context=None):
  43. view_name = sql_view.complete_sql_name
  44. query = "SELECT * FROM {view_name} "
  45. if form.limit:
  46. query += "LIMIT {limit}"
  47. return query.format(view_name=view_name, limit=form.limit)
  48. def export_csv(self, cr, uid, ids, context=None):
  49. if context is None:
  50. return
  51. sql_view_ids = context.get('active_ids', [])
  52. assert len(ids) == 1, "1 wizard ID expected"
  53. assert len(sql_view_ids) == 1, "1 active ID expected"
  54. form = self.browse(cr, uid, ids[0], context=context)
  55. sql_view = self.pool['sql.view'].browse(cr, uid, sql_view_ids[0],
  56. context=context)
  57. query = self._query(cr, uid, form, sql_view, context=context)
  58. cr.execute(query)
  59. headers = [desc[0] for desc in cr.description]
  60. records = cr.fetchall()
  61. filedata = StringIO()
  62. if not unicodecsv:
  63. raise orm.except_orm(
  64. _('Error'), _('Please install the unicodecsv library'))
  65. try:
  66. writer = unicodecsv.writer(filedata, encoding='utf-8')
  67. writer.writerow(headers)
  68. writer.writerows(records)
  69. form.write({'data': base64.encodestring(filedata.getvalue())})
  70. finally:
  71. filedata.close()
  72. return {
  73. 'type': 'ir.actions.act_window',
  74. 'res_model': self._name,
  75. 'view_mode': 'form',
  76. 'view_type': 'form',
  77. 'res_id': ids[0],
  78. 'views': [(False, 'form')],
  79. 'target': 'new',
  80. }