2014-02-12 18:00:25 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2014-Today OpenERP SA (<http://www.openerp.com>).
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2014-03-19 18:36:14 +00:00
|
|
|
from openerp.addons.web.http import Controller, route, request
|
2014-02-12 18:00:25 +00:00
|
|
|
|
2014-02-18 12:53:22 +00:00
|
|
|
import simplejson
|
2014-02-19 14:26:49 +00:00
|
|
|
from werkzeug import exceptions
|
2014-02-24 12:07:49 +00:00
|
|
|
from reportlab.graphics.barcode import createBarcodeDrawing
|
2014-02-12 18:09:34 +00:00
|
|
|
|
|
|
|
|
2014-03-19 18:36:14 +00:00
|
|
|
class ReportController(Controller):
|
2014-02-12 18:09:34 +00:00
|
|
|
|
2014-03-19 18:36:14 +00:00
|
|
|
@route(['/report/<reportname>/<docids>'], type='http', auth='user', website=True, multilang=True)
|
|
|
|
def report_html(self, reportname, docids):
|
|
|
|
return request.registry['report'].get_html(request.cr, request.uid, docids, reportname)
|
2014-02-12 18:09:34 +00:00
|
|
|
|
2014-03-19 18:36:14 +00:00
|
|
|
@route(['/report/pdf/report/<reportname>/<docids>'], type='http', auth="user", website=True)
|
|
|
|
def report_pdf(self, reportname, docids):
|
|
|
|
pdf = request.registry['report'].get_pdf(request.cr, request.uid, docids, reportname)
|
|
|
|
pdfhttpheaders = [('Content-Type', 'application/pdf'), ('Content-Length', len(pdf))]
|
2014-02-12 18:09:34 +00:00
|
|
|
return request.make_response(pdf, headers=pdfhttpheaders)
|
|
|
|
|
2014-03-19 18:36:14 +00:00
|
|
|
@route(['/report/barcode', '/report/barcode/<type>/<path:value>'], type='http', auth="user")
|
|
|
|
def report_barcode(self, type, value, width=300, height=50):
|
2014-02-20 11:13:23 +00:00
|
|
|
"""Contoller able to render barcode images thanks to reportlab.
|
2014-02-21 16:33:20 +00:00
|
|
|
Samples:
|
|
|
|
<img t-att-src="'/report/barcode/QR/%s' % o.name"/>
|
|
|
|
<img t-att-src="'/report/barcode/?type=%s&value=%s&width=%s&height=%s' % ('QR', o.name, 200, 200)"/>
|
2014-02-20 11:13:23 +00:00
|
|
|
|
|
|
|
:param type: Accepted types: 'Codabar', 'Code11', 'Code128', 'EAN13', 'EAN8', 'Extended39',
|
2014-02-19 14:26:49 +00:00
|
|
|
'Extended93', 'FIM', 'I2of5', 'MSI', 'POSTNET', 'QR', 'Standard39', 'Standard93',
|
|
|
|
'UPCA', 'USPS_4State'
|
|
|
|
"""
|
|
|
|
try:
|
2014-02-21 16:33:20 +00:00
|
|
|
width, height = int(width), int(height)
|
2014-02-24 12:07:49 +00:00
|
|
|
barcode = createBarcodeDrawing(
|
2014-02-21 10:15:20 +00:00
|
|
|
type, value=value, format='png', width=width, height=height
|
2014-02-20 11:13:23 +00:00
|
|
|
)
|
2014-02-24 12:07:49 +00:00
|
|
|
barcode = barcode.asString('png')
|
2014-02-19 14:26:49 +00:00
|
|
|
except (ValueError, AttributeError):
|
2014-02-20 11:13:23 +00:00
|
|
|
raise exceptions.HTTPException(description='Cannot convert into barcode.')
|
2014-02-19 14:26:49 +00:00
|
|
|
|
2014-02-21 10:15:20 +00:00
|
|
|
return request.make_response(barcode, headers=[('Content-Type', 'image/png')])
|
2014-02-21 16:33:20 +00:00
|
|
|
|
2014-03-19 18:36:14 +00:00
|
|
|
@route(['/report/download'], type='http', auth="user")
|
|
|
|
def report_download(self, data, token):
|
2014-02-24 10:33:29 +00:00
|
|
|
"""This function is used by 'qwebactionmanager.js' in order to trigger the download of
|
2014-03-19 18:36:14 +00:00
|
|
|
a pdf report.
|
2014-02-21 16:33:20 +00:00
|
|
|
|
2014-03-19 18:36:14 +00:00
|
|
|
:param data: a javascript array JSON.stringified containg report internal url ([0]) and
|
2014-02-24 10:33:29 +00:00
|
|
|
type [1]
|
2014-02-21 16:33:20 +00:00
|
|
|
:returns: Response with a filetoken cookie and an attachment header
|
|
|
|
"""
|
|
|
|
requestcontent = simplejson.loads(data)
|
|
|
|
url, type = requestcontent[0], requestcontent[1]
|
|
|
|
if type == 'qweb-pdf':
|
2014-03-19 18:36:14 +00:00
|
|
|
reportname, docids = url.split('/')[-2:]
|
|
|
|
response = self.report_pdf(reportname, docids)
|
2014-02-21 16:33:20 +00:00
|
|
|
response.headers.add('Content-Disposition', 'attachment; filename=report.pdf;')
|
2014-03-19 18:36:14 +00:00
|
|
|
response.set_cookie('fileToken', token)
|
|
|
|
return response
|
2014-02-21 16:33:20 +00:00
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
2014-03-19 18:36:14 +00:00
|
|
|
@route(['/report/check_wkhtmltopdf'], type='json', auth="user")
|
2014-02-27 14:48:10 +00:00
|
|
|
def check_wkhtmltopdf(self):
|
2014-03-19 18:36:14 +00:00
|
|
|
return request.registry['report'].check_wkhtmltopdf()
|