Back to index

plone3  3.1.7
exception.py
Go to the documentation of this file.
00001 __docformat__ = 'restructuredtext'
00002 
00003 from cgi import escape
00004 
00005 from zope.interface import implements
00006 from zope.i18n import translate
00007 
00008 from zope.app.form.interfaces import IWidgetInputError
00009 from zope.app.form.browser.interfaces import IWidgetInputErrorView
00010 
00011 class WidgetInputErrorView(object):
00012     """A widget error view that hardcodes no html"""
00013     implements(IWidgetInputErrorView)
00014 
00015     __used_for__ = IWidgetInputError
00016 
00017     def __init__(self, context, request):
00018         self.context, self.request = context, request
00019 
00020     def snippet(self):
00021         """Convert a widget input error to an html snippet
00022 
00023         >>> from zope.app.form.interfaces import WidgetInputError
00024         >>> class TooSmallError(object):
00025         ...     def doc(self):
00026         ...         return "Foo input < 1"
00027         >>> err = WidgetInputError("foo", "Foo", TooSmallError())
00028         >>> view = WidgetInputErrorView(err, None)
00029         >>> view.snippet()
00030         u'Foo input &lt; 1'
00031 
00032         The only method that IWidgetInputError promises to implement is
00033         `doc()`. Therefore, other implementations of the interface should also
00034         work.
00035 
00036         >>> from zope.app.form.interfaces import ConversionError
00037         >>> err = ConversionError('Could not convert to float.')
00038         >>> view = WidgetInputErrorView(err, None)
00039         >>> view.snippet()
00040         u'Could not convert to float.'
00041         """
00042         message = self.context.doc()
00043         translated = translate(message, context=self.request, default=message)
00044         return u'%s' % escape(translated)