File edit form mixin.
Lets the user edit a text file directly via a browser form.
Converts between Unicode strings used in browser forms and 8-bit strings stored internally.
>>> from zope.publisher.browser import BrowserView, TestRequest >>> class FileEditView(FileEdit, BrowserView): pass >>> view = FileEditView(File(), TestRequest()) >>> view.getData() {'data': u'', 'contentType': ''}We install an event logger so we can see the events generated.
>>> def eventLog(event): ... print event >>> zope.event.subscribers.append(eventLog)>>> view.setData({'contentType': 'text/plain; charset=ISO-8859-13', ... 'data': u'text \u0105'}) # doctest:+ELLIPSIS <zope.app.event.objectevent.ObjectModifiedEvent object at ...> u'Updated on ${date_time}'>>> view.context.contentType 'text/plain; charset=ISO-8859-13' >>> view.context.data 'text \xe0'>>> view.getData()['data'] u'text \u0105'Cleanup eventlog.
>>> zope.event.subscribers.remove(eventLog)
You will get an error if you try to specify a charset that cannot encode all the characters
>>> view.setData({'contentType': 'text/xml; charset=ISO-8859-1', ... 'data': u'text \u0105'}) Traceback (most recent call last): ... CharsetTooWeak: ISO-8859-1
You will get a different error if you try to specify an invalid charset
>>> view.setData({'contentType': 'text/xml; charset=UNKNOWN', ... 'data': u'text \u0105'}) Traceback (most recent call last): ... UnknownCharset: UNKNOWN
The update method catches those errors and replaces them with error messages
>>> from zope.i18n import translate >>> class FakeFormView(BrowserView): ... def update(self): ... raise CharsetTooWeak('ASCII') >>> class FileEditView(FileEdit, FakeFormView): pass >>> view = FileEditView(File(), TestRequest()) >>> translate(view.update()) u'The character set you specified (ASCII) cannot encode all characters in text.' >>> translate(view.update_status) u'The character set you specified (ASCII) cannot encode all characters in text.'>>> class FakeFormView(BrowserView): ... def update(self): ... raise UnknownCharset('UNKNOWN') >>> class FileEditView(FileEdit, FakeFormView): pass >>> view = FileEditView(File(), TestRequest()) >>> translate(view.update()) u'The character set you specified (UNKNOWN) is not supported.' >>> translate(view.update_status) u'The character set you specified (UNKNOWN) is not supported.'
Speaking about errors, if you trick the system and upload a file with incorrect charset designation, you will get a UserError when you visit the view:
>>> view.context.contentType = 'text/plain; charset=UNKNOWN' >>> view.context.data = '\xff' >>> view.getData() Traceback (most recent call last): ... UserError: The character set specified in the content type ($charset) is not supported.>>> view.context.contentType = 'text/plain; charset=UTF-8' >>> view.context.data = '\xff' >>> view.getData() Traceback (most recent call last): ... UserError: The character set specified in the content type ($charset) does not match file content.
There are no implemented interfaces.
error
(type: NoneType
)
None
getData()
setData(data)
update()
There are no known subclasses.