16 # You should have received a copy of the GNU Lesser General Public License along |
16 # You should have received a copy of the GNU Lesser General Public License along |
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
18 """CubicWeb is a generic framework to quickly build applications which describes |
18 """CubicWeb is a generic framework to quickly build applications which describes |
19 relations between entitites. |
19 relations between entitites. |
20 """ |
20 """ |
21 from __future__ import with_statement |
|
22 |
|
23 __docformat__ = "restructuredtext en" |
21 __docformat__ = "restructuredtext en" |
24 |
22 |
25 # ignore the pygments UserWarnings |
23 # ignore the pygments UserWarnings |
26 import warnings |
24 import warnings |
27 warnings.filterwarnings('ignore', category=UserWarning, |
25 warnings.filterwarnings('ignore', category=UserWarning, |
197 """ |
195 """ |
198 def _decorator(func): |
196 def _decorator(func): |
199 CW_EVENT_MANAGER.bind(event, func, *args, **kwargs) |
197 CW_EVENT_MANAGER.bind(event, func, *args, **kwargs) |
200 return func |
198 return func |
201 return _decorator |
199 return _decorator |
|
200 |
|
201 |
|
202 from yams.schema import role_name as rname |
|
203 |
|
204 def validation_error(entity, errors, substitutions=None, i18nvalues=None): |
|
205 """easy way to retrieve a :class:`cubicweb.ValidationError` for an entity or eid. |
|
206 |
|
207 You may also have 2-tuple as error keys, :func:`yams.role_name` will be |
|
208 called automatically for them. |
|
209 |
|
210 Messages in errors **should not be translated yet**, though marked for |
|
211 internationalization. You may give an additional substition dictionary that |
|
212 will be used for interpolation after the translation. |
|
213 """ |
|
214 if substitutions is None: |
|
215 # set empty dict else translation won't be done for backward |
|
216 # compatibility reason (see ValidationError.translate method) |
|
217 substitutions = {} |
|
218 for key in list(errors): |
|
219 if isinstance(key, tuple): |
|
220 errors[rname(*key)] = errors.pop(key) |
|
221 return ValidationError(getattr(entity, 'eid', entity), errors, |
|
222 substitutions, i18nvalues) |