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.

95 lines
3.5 KiB

10 years ago
10 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Copyright (C) 2012 Agile Business Group sagl (<http://www.agilebg.com>)
  5. # Copyright (C) 2012 Domsense srl (<http://www.domsense.com>)
  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 published
  9. # by the Free Software Foundation, either version 3 of the License, or
  10. # (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 re
  22. import xlwt
  23. from cStringIO import StringIO
  24. try:
  25. import json
  26. except ImportError:
  27. import simplejson as json
  28. import web.common.http as openerpweb
  29. from web.controllers.main import ExcelExport
  30. class ExcelExportView(ExcelExport):
  31. _cp_path = '/web/export/xls_view'
  32. def from_data(self, fields, rows, separators):
  33. workbook = xlwt.Workbook()
  34. worksheet = workbook.add_sheet('Sheet 1')
  35. for i, fieldname in enumerate(fields):
  36. worksheet.write(0, i, fieldname)
  37. worksheet.col(i).width = 8000 # around 220 pixels
  38. style = xlwt.easyxf('align: wrap yes')
  39. m = "^[\d%s]+(\%s\d+)?$" % (
  40. separators['thousands_sep'],
  41. separators['decimal_point']
  42. )
  43. for row_index, row in enumerate(rows):
  44. for cell_index, cell_value in enumerate(row):
  45. if isinstance(cell_value, basestring):
  46. cell_value = re.sub("\r", " ", cell_value)
  47. if re.match(m, cell_value):
  48. cell_value = float(
  49. cell_value.replace(
  50. separators['thousands_sep'], ''
  51. ).replace(
  52. separators['decimal_point'], '.'
  53. )
  54. )
  55. style = xlwt.easyxf(num_format_str='#,##0.00')
  56. if cell_value is False:
  57. cell_value = None
  58. worksheet.write(row_index + 1, cell_index, cell_value, style)
  59. fp = StringIO()
  60. workbook.save(fp)
  61. fp.seek(0)
  62. data = fp.read()
  63. fp.close()
  64. return data
  65. @openerpweb.httprequest
  66. def index(self, req, data, token):
  67. data = json.loads(data)
  68. model = data.get('model', [])
  69. columns_headers = data.get('headers', [])
  70. rows = data.get('rows', [])
  71. context = req.session.eval_context(req.context)
  72. lang = context.get('lang', 'en_US')
  73. Model = req.session.model('res.lang')
  74. ids = Model.search([['code', '=', lang]])
  75. record = Model.read(ids, ['decimal_point', 'thousands_sep'])
  76. return req.make_response(
  77. self.from_data(columns_headers, rows, record[0]),
  78. headers=[
  79. ('Content-Disposition', 'attachment; filename="%s"'
  80. % self.filename(model)),
  81. ('Content-Type', self.content_type)
  82. ],
  83. cookies={'fileToken': int(token)})