[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 sys
|
||||||
import traceback
|
import traceback
|
||||||
import logging
|
import logging
|
||||||
from psycopg2 import IntegrityError
|
from psycopg2 import IntegrityError, errorcodes
|
||||||
from tools.func import wraps
|
from tools.func import wraps
|
||||||
|
|
||||||
|
|
||||||
|
@ -65,13 +65,19 @@ class osv_pool(netsvc.Service):
|
||||||
for key in self._sql_error.keys():
|
for key in self._sql_error.keys():
|
||||||
if key in inst[0]:
|
if key in inst[0]:
|
||||||
self.abortResponse(1, 'Constraint Error', 'warning', self._sql_error[key])
|
self.abortResponse(1, 'Constraint Error', 'warning', self._sql_error[key])
|
||||||
if inst.pgcode == "23502":
|
if inst.pgcode == errorcodes.NOT_NULL_VIOLATION:
|
||||||
context = inst.pgerror.split('"public".')[1]
|
msg = 'Sorry, this record cannot be deleted at the moment because other records still reference it.'
|
||||||
table = context.split('"')[1]
|
self.logger.debug("IntegrityError", exc_info=True)
|
||||||
model = table.replace("_",".")
|
try:
|
||||||
if not self.obj_pool.get(model,False):
|
context = inst.pgerror.split('"public".')[1]
|
||||||
model = table
|
model_name = table = context.split('"')[1]
|
||||||
msg = 'You can not delete current record because it refered by ' + model
|
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)
|
self.abortResponse(1, 'Integrity Error', 'warning', msg)
|
||||||
else:
|
else:
|
||||||
self.abortResponse(1, 'Integrity Error', 'warning', inst[0])
|
self.abortResponse(1, 'Integrity Error', 'warning', inst[0])
|
||||||
|
|
Loading…
Reference in New Issue