From ae02c1b5371f90c4c9178acf4ca54d644e50157f Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Fri, 21 May 2010 17:59:02 +0200 Subject: [PATCH] [IMP] osv: improved user-friendly error message for violation of NOT_NULL constraints bzr revid: odo@openerp.com-20100521155902-mkqvqsgq04djnoak --- bin/osv/osv.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/bin/osv/osv.py b/bin/osv/osv.py index fb6a5066814..e09d8648ce8 100644 --- a/bin/osv/osv.py +++ b/bin/osv/osv.py @@ -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])