[IMP] osv: improved user-friendly error message for violation of NOT_NULL constraints
bzr revid: odo@openerp.com-20100521155902-mkqvqsgq04djnoak
This commit is contained in:
parent
5a7e96c384
commit
ae02c1b537
|
@ -30,7 +30,7 @@ import copy
|
|||
import sys
|
||||
import traceback
|
||||
import logging
|
||||
from psycopg2 import IntegrityError
|
||||
from psycopg2 import IntegrityError, errorcodes
|
||||
from tools.func import wraps
|
||||
|
||||
|
||||
|
@ -65,13 +65,19 @@ class osv_pool(netsvc.Service):
|
|||
for key in self._sql_error.keys():
|
||||
if key in inst[0]:
|
||||
self.abortResponse(1, 'Constraint Error', 'warning', self._sql_error[key])
|
||||
if inst.pgcode == "23502":
|
||||
context = inst.pgerror.split('"public".')[1]
|
||||
table = context.split('"')[1]
|
||||
model = table.replace("_",".")
|
||||
if not self.obj_pool.get(model,False):
|
||||
model = table
|
||||
msg = 'You can not delete current record because it refered by ' + model
|
||||
if inst.pgcode == errorcodes.NOT_NULL_VIOLATION:
|
||||
msg = 'Sorry, this record cannot be deleted at the moment because other records still reference it.'
|
||||
self.logger.debug("IntegrityError", exc_info=True)
|
||||
try:
|
||||
context = inst.pgerror.split('"public".')[1]
|
||||
model_name = table = context.split('"')[1]
|
||||
model = table.replace("_",".")
|
||||
model_obj = self.get(model)
|
||||
if model_obj:
|
||||
model_name = model_obj._description or model_obj._name
|
||||
msg += '\n\n[object with reference: %s - %s]' % (model_name, model)
|
||||
except Exception:
|
||||
pass
|
||||
self.abortResponse(1, 'Integrity Error', 'warning', msg)
|
||||
else:
|
||||
self.abortResponse(1, 'Integrity Error', 'warning', inst[0])
|
||||
|
|
Loading…
Reference in New Issue