diff --git a/addons/account/report/report_vat.py b/addons/account/report/report_vat.py index 5a5e212df9f..81e3bb64415 100644 --- a/addons/account/report/report_vat.py +++ b/addons/account/report/report_vat.py @@ -19,6 +19,7 @@ # ############################################################################## +from openerp.osv import osv from openerp.addons.web import http from openerp.addons.web.http import request from common_report_header import common_report_header @@ -29,14 +30,12 @@ except ImportError: import xlwt -class tax_report(http.Controller, common_report_header): +class tax_report(osv.Model, common_report_header): + _name = 'report.account.report_vat' - @http.route(['/report/account.report_vat', '/report/pdf/report/account.report_vat'], type='http', auth='user', website=True, multilang=True) - def report_account_tax(self, **data): + def render_html(self, cr, uid, ids, data=None, context=None): report_obj = request.registry['report'] - self.cr, self.uid, self.pool = request.cr, request.uid, request.registry - - data = report_obj.eval_params(data) + self.cr, self.uid = cr, uid res = {} self.period_ids = [] @@ -56,12 +55,7 @@ class tax_report(http.Controller, common_report_header): 'period_to': self.get_end_period(data), 'taxlines': self._get_lines(self._get_basedon(data), company_id=data['form']['company_id']), } - if request.httprequest.path.startswith('/report/pdf/'): - html = request.registry['report'].render(self.cr, self.uid, [], 'account.report_vat', docargs) - pdf = request.registry['report'].get_pdf(self.cr, self.uid, [], 'account.report_vat', html=html) - pdfhttpheaders = [('Content-Type', 'application/pdf'), ('Content-Length', len(pdf))] - return request.make_response(pdf, headers=pdfhttpheaders) - return request.registry['report'].render(self.cr, self.uid, [], 'account.report_vat', docargs) + return report_obj.render(self.cr, self.uid, [], 'account.report_vat', docargs) def _get_basedon(self, form): return form['form']['based_on'] diff --git a/addons/report/controllers/main.py b/addons/report/controllers/main.py index a7350fdd4fe..4925374cba2 100644 --- a/addons/report/controllers/main.py +++ b/addons/report/controllers/main.py @@ -22,15 +22,20 @@ from openerp.addons.web.http import Controller, route, request import simplejson +import urlparse from werkzeug import exceptions from reportlab.graphics.barcode import createBarcodeDrawing class ReportController(Controller): + #------------------------------------------------------ + # Generic reports controller + #------------------------------------------------------ + @route(['/report//'], 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) + return request.registry['report'].get_html(request.cr, request.uid, docids, reportname, context=request.context) @route(['/report/pdf/report//'], type='http', auth="user", website=True) def report_pdf(self, reportname, docids): @@ -38,6 +43,30 @@ class ReportController(Controller): pdfhttpheaders = [('Content-Type', 'application/pdf'), ('Content-Length', len(pdf))] return request.make_response(pdf, headers=pdfhttpheaders) + #------------------------------------------------------ + # Particular reports controller + #------------------------------------------------------ + + @route(['/report/'], type='http', auth='user', website=True, multilang=True) + def report_html_particular(self, reportname, **data): + report_obj = request.registry['report'] + data = report_obj.eval_params(data) + return report_obj.get_html(request.cr, request.uid, [], reportname, data=data, context=request.context) + + @route(['/report/pdf/report/'], type='http', auth='user', website=True, multilang=True) + def report_pdf_particular(self, reportname, **data): + cr, uid, context = request.cr, request.uid, request.context + report_obj = request.registry['report'] + data = report_obj.eval_params(data) + html = report_obj.get_html(cr, uid, [], reportname, data=data, context=context) + pdf = report_obj.get_pdf(cr, uid, [], reportname, html=html, context=context) + pdfhttpheaders = [('Content-Type', 'application/pdf'), ('Content-Length', len(pdf))] + return request.make_response(pdf, headers=pdfhttpheaders) + + #------------------------------------------------------ + # Misc utils + #------------------------------------------------------ + @route(['/report/barcode', '/report/barcode//'], type='http', auth="user") def report_barcode(self, type, value, width=300, height=50): """Contoller able to render barcode images thanks to reportlab. @@ -72,9 +101,20 @@ class ReportController(Controller): requestcontent = simplejson.loads(data) url, type = requestcontent[0], requestcontent[1] if type == 'qweb-pdf': - reportname, docids = url.split('/')[-2:] - response = self.report_pdf(reportname, docids) - response.headers.add('Content-Disposition', 'attachment; filename=report.pdf;') + reportname = url.split('/report/pdf/report/')[1].split('?')[0].split('/')[0] + + if '?' not in url: + # Generic report: + docids = url.split('/')[-1] + response = self.report_pdf(reportname, docids) + else: + # Particular report: + querystring = url.split('?')[1] + querystring = urlparse.parse_qsl(querystring) + dict_querystring = dict(querystring) + response = self.report_pdf_particular(reportname, **dict_querystring) + + response.headers.add('Content-Disposition', 'attachment; filename=%s.pdf;' % reportname) response.set_cookie('fileToken', token) return response else: diff --git a/addons/report/models/report.py b/addons/report/models/report.py index 2275a46ae93..5c0dc8c7703 100644 --- a/addons/report/models/report.py +++ b/addons/report/models/report.py @@ -220,10 +220,22 @@ class Report(osv.Model): # Public report API #-------------------------------------------------------------------------- - def get_html(self, cr, uid, ids, report_name, context=None): + def get_html(self, cr, uid, ids, report_name, data=None, context=None): + """This method generates and returns html version of generic report. + """ if context is None: context = {} + # If the report is using a custom model to render its html, we must use it. + # Otherwise, fallback on the generic html rendering. + if data is not None: + try: + report_model_name = 'report.%s' % report_name + particularreport_obj = self.pool[report_model_name] + return particularreport_obj.render_html(cr, uid, [], data=data, context=context) + except: + pass + if isinstance(ids, str): ids = [int(i) for i in ids.split(',')] if isinstance(ids, list): @@ -243,11 +255,21 @@ class Report(osv.Model): return self.render(cr, uid, [], report.report_name, docargs, context=context) def get_pdf(self, cr, uid, ids, report_name, html=None, context=None): + """This method generates and returns pdf version of generic report. + """ if context is None: context = {} + if isinstance(ids, (str, unicode)): + ids = [int(i) for i in ids.split(',')] + if isinstance(ids, list): + ids = list(set(ids)) + if isinstance(ids, int): + ids = [ids] + if html is None: html = self.get_html(cr, uid, ids, report_name, context=context) + html = html.decode('utf-8') # Get the report we are working on. @@ -628,7 +650,7 @@ class Report(osv.Model): return content def eval_params(self, dict_param): - """Parse a dictionary generated by the webclient (javascript) into a dictionary + """Parse a dict generated by the webclient (javascript) into a dictionary understandable by a wizard controller (python). """ for key, value in dict_param.iteritems():