Unify and refactor exception handling in framework and addons. The generic `except_osv` is now deprecated, and replaced by more specialized exception subtypes: - `UserError` (renamed from Warning, as it conflicts with the built-in `Warning`) raised when a non-technical error occurs during a business operation. It could be a missing information in the data provided by the user, or a misconfiguration. - `AccessError`: raised when any operation is denied because the user conducting it does not have the required access rights. - `AccessDenied`: raised when an operation that requires authenticated access is attempted via an unauthenticated request. - `MissingError`: raised when an operation is attempted on a record that does not exist. - `ValidationError`: raised when an operation violates a SQL or Python constraint. - All other exceptions are internal errors due to a system problem or bug, and raised untouched to the client-side, which should display a traceback. All exceptions take a single message argument. The `test_exceptions` module has been updated to showcase both new and old (deprecated) exceptions. A great many old `except_osv` had a useless title with "Error!" or "Warning", those have been removed, as this is handled by the client-side widget that displays the messages. This commit introduces a more consistent policy for logging errors and warnings: - All messages that do not require administrator attention should be logged at INFO level or lower. This includes all errors that are notified to the user in a friendly manner, even for access right problems or validation errors during business operations. - All messages that indicate a likely misconfiguration or malicious use by the users should be logged at WARNING level, as they typically require administrator attention. - All other unhandled internal errors cannot typically be handled by the user and should be logged at ERROR or higher level, as they require immediate administrator attention.
148 lines
6.8 KiB
Python
148 lines
6.8 KiB
Python
# -*- 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/>.
|
|
#
|
|
##############################################################################
|
|
|
|
from openerp.addons.web.http import Controller, route, request
|
|
from openerp.addons.web.controllers.main import _serialize_exception
|
|
from openerp.tools import html_escape
|
|
|
|
import simplejson
|
|
from werkzeug import exceptions, url_decode
|
|
from werkzeug.test import Client
|
|
from werkzeug.wrappers import BaseResponse
|
|
from werkzeug.datastructures import Headers
|
|
from reportlab.graphics.barcode import createBarcodeDrawing
|
|
|
|
|
|
class ReportController(Controller):
|
|
|
|
#------------------------------------------------------
|
|
# Report controllers
|
|
#------------------------------------------------------
|
|
@route([
|
|
'/report/<path:converter>/<reportname>',
|
|
'/report/<path:converter>/<reportname>/<docids>',
|
|
], type='http', auth='user', website=True)
|
|
def report_routes(self, reportname, docids=None, converter=None, **data):
|
|
report_obj = request.registry['report']
|
|
cr, uid, context = request.cr, request.uid, request.context
|
|
|
|
if docids:
|
|
docids = [int(i) for i in docids.split(',')]
|
|
options_data = None
|
|
if data.get('options'):
|
|
options_data = simplejson.loads(data['options'])
|
|
if data.get('context'):
|
|
# Ignore 'lang' here, because the context in data is the one from the webclient *but* if
|
|
# the user explicitely wants to change the lang, this mechanism overwrites it.
|
|
data_context = simplejson.loads(data['context'])
|
|
if data_context.get('lang'):
|
|
del data_context['lang']
|
|
context.update(data_context)
|
|
|
|
if converter == 'html':
|
|
html = report_obj.get_html(cr, uid, docids, reportname, data=options_data, context=context)
|
|
return request.make_response(html)
|
|
elif converter == 'pdf':
|
|
pdf = report_obj.get_pdf(cr, uid, docids, reportname, data=options_data, context=context)
|
|
pdfhttpheaders = [('Content-Type', 'application/pdf'), ('Content-Length', len(pdf))]
|
|
return request.make_response(pdf, headers=pdfhttpheaders)
|
|
else:
|
|
raise exceptions.HTTPException(description='Converter %s not implemented.' % converter)
|
|
|
|
#------------------------------------------------------
|
|
# Misc. route utils
|
|
#------------------------------------------------------
|
|
@route(['/report/barcode', '/report/barcode/<type>/<path:value>'], type='http', auth="user")
|
|
def report_barcode(self, type, value, width=600, height=100, humanreadable=0):
|
|
"""Contoller able to render barcode images thanks to reportlab.
|
|
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)"/>
|
|
|
|
:param type: Accepted types: 'Codabar', 'Code11', 'Code128', 'EAN13', 'EAN8', 'Extended39',
|
|
'Extended93', 'FIM', 'I2of5', 'MSI', 'POSTNET', 'QR', 'Standard39', 'Standard93',
|
|
'UPCA', 'USPS_4State'
|
|
:param humanreadable: Accepted values: 0 (default) or 1. 1 will insert the readable value
|
|
at the bottom of the output image
|
|
"""
|
|
try:
|
|
width, height, humanreadable = int(width), int(height), bool(humanreadable)
|
|
barcode = createBarcodeDrawing(
|
|
type, value=value, format='png', width=width, height=height,
|
|
humanReadable = humanreadable
|
|
)
|
|
barcode = barcode.asString('png')
|
|
except (ValueError, AttributeError):
|
|
raise exceptions.HTTPException(description='Cannot convert into barcode.')
|
|
|
|
return request.make_response(barcode, headers=[('Content-Type', 'image/png')])
|
|
|
|
@route(['/report/download'], type='http', auth="user")
|
|
def report_download(self, data, token):
|
|
"""This function is used by 'qwebactionmanager.js' in order to trigger the download of
|
|
a pdf/controller report.
|
|
|
|
:param data: a javascript array JSON.stringified containg report internal url ([0]) and
|
|
type [1]
|
|
:returns: Response with a filetoken cookie and an attachment header
|
|
"""
|
|
requestcontent = simplejson.loads(data)
|
|
url, type = requestcontent[0], requestcontent[1]
|
|
try:
|
|
if type == 'qweb-pdf':
|
|
reportname = url.split('/report/pdf/')[1].split('?')[0]
|
|
|
|
docids = None
|
|
if '/' in reportname:
|
|
reportname, docids = reportname.split('/')
|
|
|
|
if docids:
|
|
# Generic report:
|
|
response = self.report_routes(reportname, docids=docids, converter='pdf')
|
|
else:
|
|
# Particular report:
|
|
data = url_decode(url.split('?')[1]).items() # decoding the args represented in JSON
|
|
response = self.report_routes(reportname, converter='pdf', **dict(data))
|
|
|
|
response.headers.add('Content-Disposition', 'attachment; filename=%s.pdf;' % reportname)
|
|
response.set_cookie('fileToken', token)
|
|
return response
|
|
elif type =='controller':
|
|
reqheaders = Headers(request.httprequest.headers)
|
|
response = Client(request.httprequest.app, BaseResponse).get(url, headers=reqheaders, follow_redirects=True)
|
|
response.set_cookie('fileToken', token)
|
|
return response
|
|
else:
|
|
return
|
|
except Exception, e:
|
|
se = _serialize_exception(e)
|
|
error = {
|
|
'code': 200,
|
|
'message': "Odoo Server Error",
|
|
'data': se
|
|
}
|
|
return request.make_response(html_escape(simplejson.dumps(error)))
|
|
|
|
@route(['/report/check_wkhtmltopdf'], type='json', auth="user")
|
|
def check_wkhtmltopdf(self):
|
|
return request.registry['report']._check_wkhtmltopdf()
|