__init__.py
branchstable
changeset 8743 27a83746aebd
parent 8696 0bb18407c053
child 8748 f5027f8d2478
--- a/__init__.py	Tue Mar 19 16:56:46 2013 +0100
+++ b/__init__.py	Wed Mar 20 17:40:25 2013 +0100
@@ -18,8 +18,6 @@
 """CubicWeb is a generic framework to quickly build applications which describes
 relations between entitites.
 """
-from __future__ import with_statement
-
 __docformat__ = "restructuredtext en"
 
 # ignore the pygments UserWarnings
@@ -199,3 +197,26 @@
         CW_EVENT_MANAGER.bind(event, func, *args, **kwargs)
         return func
     return _decorator
+
+
+from yams.schema import role_name as rname
+
+def validation_error(entity, errors, substitutions=None, i18nvalues=None):
+    """easy way to retrieve a :class:`cubicweb.ValidationError` for an entity or eid.
+
+    You may also have 2-tuple as error keys, :func:`yams.role_name` will be
+    called automatically for them.
+
+    Messages in errors **should not be translated yet**, though marked for
+    internationalization. You may give an additional substition dictionary that
+    will be used for interpolation after the translation.
+    """
+    if substitutions is None:
+        # set empty dict else translation won't be done for backward
+        # compatibility reason (see ValidationError.translate method)
+        substitutions = {}
+    for key in list(errors):
+        if isinstance(key, tuple):
+            errors[rname(*key)] = errors.pop(key)
+    return ValidationError(getattr(entity, 'eid', entity), errors,
+                           substitutions, i18nvalues)