Catch _sql_constraints and _sql_error_messages in ModelSQL instead of
dispatcher
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user