From d0dee320199662492c3779deef06c412238eec73 Mon Sep 17 00:00:00 2001 From: "P. Christeas" Date: Thu, 18 Nov 2010 20:46:38 +0200 Subject: [PATCH] osv: Improve the translation logic for the osv.check wrapper Done after bug [655783], the osv layer is too early to call the _(). Also, we cannot trust the cursor to perform a further SQL query on it, since it carries the IntegrityError. bzr revid: p_christ@hol.gr-20101118184638-2yrfpvx9har48h1g --- bin/osv/osv.py | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/bin/osv/osv.py b/bin/osv/osv.py index 11db4096a9a..9b1bb3ad86f 100644 --- a/bin/osv/osv.py +++ b/bin/osv/osv.py @@ -32,7 +32,7 @@ import traceback import logging from psycopg2 import IntegrityError, errorcodes from tools.func import wraps -from tools.translate import _ +from tools.translate import translate module_list = [] module_class_list = {} @@ -51,6 +51,46 @@ class osv_pool(netsvc.Service): def check(f): @wraps(f) def wrapper(self, dbname, *args, **kwargs): + """ Wraps around OSV functions and normalises a few exceptions + """ + + def tr(src, ttype): + # We try to do the same as the _(), but without the frame + # inspection, since we aready are wrapping an osv function + # trans_obj = self.get('ir.translation') cannot work yet :( + ctx = {} + if not kwargs: + if args and isinstance(args[-1], dict): + ctx = args[-1] + else: + ctx = {} + elif isinstance(kwargs, dict): + ctx = kwargs.get('context', {}) + else: + ctx = {} + + uid = 1 + if args and isinstance(args[0], (long, int)): + uid = args[0] + + lang = ctx and ctx.get('lang', False) + if not lang: + return src + + # We open a *new* cursor here, one reason is that failed SQL + # queries (as in IntegrityError) will invalidate the current one. + cr = False + try: + cr = pooler.get_db_only(dbname).cursor() + #return trans_obj._get_source( name=?) + return translate(cr, name=False, source_type=ttype, + lang=lang, source=src) + finally: + if cr: cr.close() + + def _(src): + return tr(src, 'code') + try: if not pooler.get_pool(dbname)._ready: raise except_osv('Database not ready', 'Currently, this database is not fully loaded and can not be used.') @@ -64,7 +104,8 @@ class osv_pool(netsvc.Service): except IntegrityError, inst: for key in self._sql_error.keys(): if key in inst[0]: - self.abortResponse(1, _('Constraint Error'), 'warning', _(self._sql_error[key])) + self.abortResponse(1, _('Constraint Error'), 'warning', + tr(self._sql_error[key], 'sql_constraint')) if inst.pgcode in (errorcodes.NOT_NULL_VIOLATION, errorcodes.FOREIGN_KEY_VIOLATION, errorcodes.RESTRICT_VIOLATION): msg = _('The operation cannot be completed, probably due to the following:\n- deletion: you may be trying to delete a record while other records still reference it\n- creation/update: a mandatory field is not correctly set') self.logger.debug("IntegrityError", exc_info=True)