entities/adapters.py
changeset 10446 1e6655cff5ab
parent 10301 729f36a1bcfa
child 10473 23a2fa8cb725
equal deleted inserted replaced
10445:f1773842077d 10446:1e6655cff5ab
    22 __docformat__ = "restructuredtext en"
    22 __docformat__ = "restructuredtext en"
    23 _ = unicode
    23 _ = unicode
    24 
    24 
    25 from itertools import chain
    25 from itertools import chain
    26 from warnings import warn
    26 from warnings import warn
       
    27 from hashlib import md5
    27 
    28 
    28 from logilab.mtconverter import TransformError
    29 from logilab.mtconverter import TransformError
    29 from logilab.common.decorators import cached
    30 from logilab.common.decorators import cached
    30 
    31 
    31 from cubicweb import ValidationError, view
    32 from cubicweb import ValidationError, view, ViolatedConstraint
       
    33 from cubicweb.schema import CONSTRAINTS
    32 from cubicweb.predicates import is_instance, relation_possible, match_exception
    34 from cubicweb.predicates import is_instance, relation_possible, match_exception
    33 
    35 
    34 
    36 
    35 class IEmailableAdapter(view.EntityAdapter):
    37 class IEmailableAdapter(view.EntityAdapter):
    36     __regid__ = 'IEmailable'
    38     __regid__ = 'IEmailable'
   370             errors[rtype] = _('%(KEY-rtype)s is part of violated unicity constraint')
   372             errors[rtype] = _('%(KEY-rtype)s is part of violated unicity constraint')
   371             msgargs[rtype + '-rtype'] = rtype
   373             msgargs[rtype + '-rtype'] = rtype
   372             i18nvalues.append(rtype + '-rtype')
   374             i18nvalues.append(rtype + '-rtype')
   373         errors[''] = _('some relations violate a unicity constraint')
   375         errors[''] = _('some relations violate a unicity constraint')
   374         raise ValidationError(self.entity.eid, errors, msgargs=msgargs, i18nvalues=i18nvalues)
   376         raise ValidationError(self.entity.eid, errors, msgargs=msgargs, i18nvalues=i18nvalues)
       
   377 
       
   378 
       
   379 class IUserFriendlyCheckConstraint(IUserFriendlyError):
       
   380     __select__ = match_exception(ViolatedConstraint)
       
   381 
       
   382     def raise_user_exception(self):
       
   383         _ = self._cw._
       
   384         cstrname = self.exc.cstrname
       
   385         eschema = self.entity.e_schema
       
   386         for rschema, attrschema in eschema.attribute_definitions():
       
   387             rdef = rschema.rdef(eschema, attrschema)
       
   388             for constraint in rdef.constraints:
       
   389                 if cstrname == 'cstr' + md5(eschema.type + rschema.type + constraint.type() + (constraint.serialize() or '')).hexdigest():
       
   390                     break
       
   391             else:
       
   392                 continue
       
   393             break
       
   394         else:
       
   395             assert 0
       
   396         key = rschema.type + '-subject'
       
   397         msg, args = constraint.failed_message(key, self.entity.cw_edited[rschema.type])
       
   398         raise ValidationError(self.entity.eid, {key: msg}, args)