controllers.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Copyright (c) 2013 ZestyBeanz Technologies Pvt. Ltd.
  5. # (http://wwww.zbeanztech.com)
  6. # contact@zbeanztech.com
  7. # prajul@zbeanztech.com
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. #
  22. ##############################################################################
  23. try:
  24. import json
  25. except ImportError:
  26. import simplejson as json
  27. import openerp.addons.web.http as openerpweb
  28. from openerp.addons.web.controllers.main import ExcelExport
  29. from openerp.addons.web.controllers.main import Export
  30. import re
  31. from cStringIO import StringIO
  32. from lxml import etree
  33. import trml2pdf
  34. import time, os
  35. import locale
  36. import openerp.tools as tools
  37. try:
  38. import xlwt
  39. except ImportError:
  40. xlwt = None
  41. class ZbExcelExport(ExcelExport):
  42. _cp_path = '/web/export/zb_excel_export'
  43. def from_data(self, fields, rows):
  44. workbook = xlwt.Workbook()
  45. worksheet = workbook.add_sheet('Sheet 1')
  46. style = xlwt.easyxf('align: wrap yes')
  47. font = xlwt.Font()
  48. font.bold = True
  49. style.font = font
  50. ignore_index = []
  51. count = 0
  52. for i, fieldname in enumerate(fields):
  53. if fieldname.get('header_data_id', False):
  54. field_name = fieldname.get('header_name', '')
  55. worksheet.write(0, i-count, field_name, style)
  56. worksheet.col(i).width = 8000
  57. else:
  58. count += 1
  59. ignore_index.append(i)
  60. style = xlwt.easyxf('align: wrap yes')
  61. bold_style = xlwt.easyxf('align: wrap yes')
  62. font = xlwt.Font()
  63. font.bold = True
  64. bold_style.font = font
  65. for row_index, row in enumerate(rows):
  66. count = 0
  67. for cell_index, cell_value in enumerate(row):
  68. if cell_index not in ignore_index:
  69. cell_style = style
  70. if cell_value.get('bold', False):
  71. cell_style = bold_style
  72. cellvalue = cell_value.get('data', '')
  73. if isinstance(cellvalue, basestring):
  74. cellvalue = re.sub("\r", " ", cellvalue)
  75. if cell_value.get('number', False) and cellvalue:
  76. cellvalue = float(cellvalue)
  77. if cellvalue is False: cellvalue = None
  78. worksheet.write(row_index + 1, cell_index - count, cellvalue, cell_style)
  79. else:
  80. count += 1
  81. fp = StringIO()
  82. workbook.save(fp)
  83. fp.seek(0)
  84. data = fp.read()
  85. fp.close()
  86. return data
  87. @openerpweb.httprequest
  88. def index(self, req, data, token):
  89. data = json.loads(data)
  90. return req.make_response(
  91. self.from_data(data.get('headers', []), data.get('rows', [])),
  92. headers=[
  93. ('Content-Disposition', 'attachment; filename="%s"'
  94. % data.get('model', 'Export.xls')),
  95. ('Content-Type', self.content_type)
  96. ],
  97. cookies={'fileToken': token}
  98. )
  99. class ExportPdf(Export):
  100. _cp_path = '/web/export/zb_pdf'
  101. fmt = {
  102. 'tag': 'pdf',
  103. 'label': 'PDF',
  104. 'error': None
  105. }
  106. @property
  107. def content_type(self):
  108. return 'application/pdf'
  109. def filename(self, base):
  110. return base + '.pdf'
  111. def from_data(self, uid, fields, rows, company_name):
  112. pageSize=[210.0,297.0]
  113. new_doc = etree.Element("report")
  114. config = etree.SubElement(new_doc, 'config')
  115. def _append_node(name, text):
  116. n = etree.SubElement(config, name)
  117. n.text = text
  118. _append_node('date', time.strftime(str(locale.nl_langinfo(locale.D_FMT).replace('%y', '%Y'))))
  119. _append_node('PageSize', '%.2fmm,%.2fmm' % tuple(pageSize))
  120. _append_node('PageWidth', '%.2f' % (pageSize[0] * 2.8346,))
  121. _append_node('PageHeight', '%.2f' %(pageSize[1] * 2.8346,))
  122. _append_node('PageFormat', 'a4')
  123. _append_node('header-date', time.strftime(str(locale.nl_langinfo(locale.D_FMT).replace('%y', '%Y'))))
  124. _append_node('company', company_name)
  125. l = []
  126. t = 0
  127. temp = []
  128. tsum = []
  129. skip_index = []
  130. header = etree.SubElement(new_doc, 'header')
  131. i = 0
  132. for f in fields:
  133. if f.get('header_data_id', False):
  134. value = f.get('header_name', "")
  135. field = etree.SubElement(header, 'field')
  136. field.text = tools.ustr(value)
  137. else:
  138. skip_index.append(i)
  139. i += 1
  140. lines = etree.SubElement(new_doc, 'lines')
  141. for row_lines in rows:
  142. node_line = etree.SubElement(lines, 'row')
  143. j = 0
  144. for row in row_lines:
  145. if not j in skip_index:
  146. para = "yes"
  147. tree = "no"
  148. value = row.get('data', '')
  149. if row.get('bold', False):
  150. para = "group"
  151. if row.get('number', False):
  152. tree = "float"
  153. col = etree.SubElement(node_line, 'col', para=para, tree=tree)
  154. col.text = tools.ustr(value)
  155. j += 1
  156. transform = etree.XSLT(
  157. etree.parse(os.path.join(tools.config['root_path'],
  158. 'addons/base/report/custom_new.xsl')))
  159. rml = etree.tostring(transform(new_doc))
  160. self.obj = trml2pdf.parseNode(rml, title='Printscreen')
  161. return self.obj
  162. class ZbPdfExport(ExportPdf):
  163. _cp_path = '/web/export/zb_pdf_export'
  164. @openerpweb.httprequest
  165. def index(self, req, data, token):
  166. data = json.loads(data)
  167. uid = data.get('uid', False)
  168. return req.make_response(self.from_data(uid, data.get('headers', []), data.get('rows', []),
  169. data.get('company_name','')),
  170. headers=[('Content-Disposition',
  171. 'attachment; filename=PDF Export'),
  172. ('Content-Type', self.content_type)],
  173. cookies={'fileToken': token})
  174. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: