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.
57 lines
2.2 KiB
Python
57 lines
2.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
##############################################################################
|
|
#
|
|
# OpenERP, Open Source Management Solution
|
|
# Copyright (C) 2013 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 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 General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
#
|
|
##############################################################################
|
|
|
|
from openerp.osv import fields, osv
|
|
from openerp.tools.translate import _
|
|
from openerp.exceptions import UserError
|
|
|
|
|
|
class grant_badge_wizard(osv.TransientModel):
|
|
""" Wizard allowing to grant a badge to a user"""
|
|
|
|
_name = 'gamification.badge.user.wizard'
|
|
_columns = {
|
|
'user_id': fields.many2one("res.users", string='User', required=True),
|
|
'badge_id': fields.many2one("gamification.badge", string='Badge', required=True),
|
|
'comment': fields.text('Comment'),
|
|
}
|
|
|
|
def action_grant_badge(self, cr, uid, ids, context=None):
|
|
"""Wizard action for sending a badge to a chosen user"""
|
|
|
|
badge_user_obj = self.pool.get('gamification.badge.user')
|
|
|
|
for wiz in self.browse(cr, uid, ids, context=context):
|
|
if uid == wiz.user_id.id:
|
|
raise UserError(_('You can not grant a badge to yourself'))
|
|
|
|
#create the badge
|
|
values = {
|
|
'user_id': wiz.user_id.id,
|
|
'sender_id': uid,
|
|
'badge_id': wiz.badge_id.id,
|
|
'comment': wiz.comment,
|
|
}
|
|
badge_user = badge_user_obj.create(cr, uid, values, context=context)
|
|
result = badge_user_obj._send_badge(cr, uid, badge_user, context=context)
|
|
|
|
return result
|