377 from cubicweb import UniqueTogetherError |
377 from cubicweb import UniqueTogetherError |
378 |
378 |
379 class IUserFriendlyError(view.EntityAdapter): |
379 class IUserFriendlyError(view.EntityAdapter): |
380 __regid__ = 'IUserFriendlyError' |
380 __regid__ = 'IUserFriendlyError' |
381 __abstract__ = True |
381 __abstract__ = True |
|
382 |
382 def __init__(self, *args, **kwargs): |
383 def __init__(self, *args, **kwargs): |
383 self.exc = kwargs.pop('exc') |
384 self.exc = kwargs.pop('exc') |
384 super(IUserFriendlyError, self).__init__(*args, **kwargs) |
385 super(IUserFriendlyError, self).__init__(*args, **kwargs) |
385 |
386 |
386 |
387 |
387 class IUserFriendlyUniqueTogether(IUserFriendlyError): |
388 class IUserFriendlyUniqueTogether(IUserFriendlyError): |
388 __select__ = match_exception(UniqueTogetherError) |
389 __select__ = match_exception(UniqueTogetherError) |
|
390 |
389 def raise_user_exception(self): |
391 def raise_user_exception(self): |
390 etype, rtypes = self.exc.args |
392 etype, rtypes = self.exc.args |
391 msg = self._cw._('violates unique_together constraints (%s)') % ( |
393 # Because of index name size limits (e.g: postgres around 64, |
392 ', '.join([self._cw._(rtype) for rtype in rtypes])) |
394 # sqlserver around 128), we cannot be sure of what we got, |
393 raise ValidationError(self.entity.eid, dict((col, msg) for col in rtypes)) |
395 # especially for the rtypes part. |
|
396 # Hence we will try to validate them, and handle invalid ones |
|
397 # in the most user-friendly manner ... |
|
398 _ = self._cw._ |
|
399 schema = self.entity._cw.vreg.schema |
|
400 rtypes_msg = {} |
|
401 for rtype in rtypes: |
|
402 if rtype in schema: |
|
403 rtypes_msg[rtype] = _('%s is part of violated unicity constraint') % rtype |
|
404 globalmsg = _('some relations %sviolate a unicity constraint') |
|
405 if len(rtypes) != len(rtypes_msg): # we got mangled/missing rtypes |
|
406 globalmsg = globalmsg % _('(not all shown here) ') |
|
407 else: |
|
408 globalmsg = globalmsg % '' |
|
409 rtypes_msg['unicity constraint'] = globalmsg |
|
410 raise ValidationError(self.entity.eid, rtypes_msg) |
394 |
411 |
395 # deprecated ################################################################### |
412 # deprecated ################################################################### |
396 |
413 |
397 |
414 |
398 class adapter_deprecated(view.auto_unwrap_bw_compat): |
415 class adapter_deprecated(view.auto_unwrap_bw_compat): |