From a352575ef0f34aec618aa0b8da2bfb7df313e329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 7 Apr 2009 18:59:00 +0200 Subject: [PATCH] Catch _sql_constraints and _sql_error_messages in ModelSQL instead of dispatcher --- trytond/model/modelsql.py | 26 ++++++++++++++++++++++++-- trytond/pool.py | 9 --------- trytond/protocols/dispatcher.py | 20 -------------------- 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/trytond/model/modelsql.py b/trytond/model/modelsql.py index ce15d98c09..0c5bed9100 100644 --- a/trytond/model/modelsql.py +++ b/trytond/model/modelsql.py @@ -316,7 +316,7 @@ class ModelSQL(ModelStorage): cursor.execute('INSERT INTO "' + self._table + '" ' \ '(id' + upd0 + ') ' \ 'VALUES (' + str(id_new) + upd1 + ')', tuple(upd2)) - except DatabaseIntegrityError: + except DatabaseIntegrityError, exception: database = Database(cursor.database_name).connect() cursor2 = database.cursor() try: @@ -341,6 +341,12 @@ class ModelSQL(ModelStorage): error_args=self._get_error_args( cursor2, user, field_name, context=context), context=context) + for name, _, error in self._sql_constraints: + if name in exception[0]: + self.raise_user_error(cursor2, error, context=context) + for name, error in self._sql_error_messages: + if name in exception[0]: + self.raise_user_error(cursor2, error, context=context) finally: cursor2.close() raise @@ -774,7 +780,7 @@ class ModelSQL(ModelStorage): 'SET ' + \ ','.join([x[0] + ' = '+ x[1] for x in upd0]) + ' ' \ 'WHERE id IN (' + ids_str + ') ', upd1 + sub_ids) - except DatabaseIntegrityError: + except DatabaseIntegrityError, exception: database = Database(cursor.database_name).connect() cursor2 = database.cursor() try: @@ -804,6 +810,14 @@ class ModelSQL(ModelStorage): error_args=self._get_error_args( cursor2, user, field_name, context=context), context=context) + for name, _, error in self._sql_constraints: + if name in exception[0]: + self.raise_user_error(cursor2, error, + context=context) + for name, error in self._sql_error_messages: + if name in exception[0]: + self.raise_user_error(cursor2, error, + context=context) finally: cursor2.close() raise @@ -1010,6 +1024,14 @@ class ModelSQL(ModelStorage): user, field_name, context=context))) self.raise_user_error(cursor2, 'foreign_model_exist', error_args=tuple(error_args), context=context) + for name, _, error in self._sql_constraints: + if name in exception[0]: + self.raise_user_error(cursor2, error, + context=context) + for name, error in self._sql_error_messages: + if name in exception[0]: + self.raise_user_error(cursor2, error, + context=context) finally: cursor2.close() raise diff --git a/trytond/pool.py b/trytond/pool.py index 57f5ab42db..650ce6be60 100644 --- a/trytond/pool.py +++ b/trytond/pool.py @@ -40,8 +40,6 @@ class Pool(object): self._pool[database_name].setdefault(type, {}) self._pools.setdefault(database_name, self) lock.release() - if not hasattr(self, '_sql_errors'): - self._sql_errors = {} @staticmethod def register(cls, type='model'): @@ -145,11 +143,4 @@ class Pool(object): self.add(obj, type=_type) obj.__init__() res[_type].append(obj) - - if hasattr(obj, '_sql_constraints'): - for key, _, msg in obj._sql_constraints: - self._sql_errors[obj._table + '_' + key] = msg - if hasattr(obj, '_sql_error_messages'): - for key, msg in obj._sql_error_messages.iteritems(): - self._sql_errors[obj._table + '_' + key] = msg return res diff --git a/trytond/protocols/dispatcher.py b/trytond/protocols/dispatcher.py index 6ab87a54cc..6c18931ed2 100644 --- a/trytond/protocols/dispatcher.py +++ b/trytond/protocols/dispatcher.py @@ -127,26 +127,6 @@ def dispatch(host, port, protocol, database_name, user, session, object_type, '%s %s from %s@%s:%d/%s:\n' % \ (method, object_type, object_name, user, host, port, database_name) + tb_s.decode('utf-8', 'ignore')) - if isinstance(exception, DatabaseIntegrityError): - for key in pool._sql_errors.keys(): - if key in exception[0]: - msg = pool._sql_errors[key] - cursor2 = database.cursor() - try: - cursor2.execute('SELECT value ' \ - 'FROM ir_translation ' \ - 'WHERE lang=%s ' \ - 'AND type=%s ' \ - 'AND src=%s', - (find_language_context(args, kargs), - 'error', msg)) - if cursor2.rowcount: - res = cursor2.fetchone()[0] - if res: - msg = res - finally: - cursor2.close() - raise Exception('UserError', 'Constraint Error', msg) cursor.rollback() raise finally: