Class
zope.app.file.browser.file.FileUpload

View that updates an existing File object with a new upload.
Fires an ObjectModifiedEvent.
>>> from zope.publisher.browser import TestRequest
>>> import StringIO
>>> sio = StringIO.StringIO("some data")
>>> sio.filename = 'abc.txt'

Before we instanciate the request, we need to make sure that the IUserPreferredLanguages adapter exists, so that the request's locale exists. This is necessary because the update_object method uses the locale formatter for the status message:

>>> from zope.app.testing import ztapi
>>> from zope.publisher.browser import BrowserLanguages
>>> from zope.publisher.interfaces.http import IHTTPRequest
>>> from zope.i18n.interfaces import IUserPreferredLanguages
>>> ztapi.provideAdapter(IHTTPRequest, IUserPreferredLanguages,
...                      BrowserLanguages)

We install an event logger so we can see the events generated:

>>> def eventLog(event):
...     print 'ModifiedEvent:', event.descriptions[0].attributes
>>> zope.event.subscribers.append(eventLog)

Let's make sure we can use the uploaded file name if one isn't specified by the user, and can use the content type when specified.

>>> request = TestRequest(form={'field.data': sio,
...                             'field.contentType': 'text/foobar',
...                             'UPDATE_SUBMIT': 'Update'})
>>> file = File()
>>> view = FileUpload(file, request)
>>> view.errors()
ModifiedEvent: ('contentType', 'data')
u'Updated on ${date_time}'
>>> file.contentType
'text/foobar'
>>> file.data
'some data'

Now let's guess the content type, but also use a provided file name for adding the new content object:

>>> request = TestRequest(form={'field.data': sio,
...                             'field.contentType': '',
...                             'add_input_name': 'splat.txt',
...                             'UPDATE_SUBMIT': 'Update'})
>>> file = File()
>>> view = FileUpload(file, request)
>>> view.errors()
ModifiedEvent: ('contentType', 'data')
u'Updated on ${date_time}'
>>> file.contentType
'text/plain'

The ObjectModifiedEvent lists only the contentType if the data are omitted:

>>> request = TestRequest(form={'field.data': None,
...                             'field.contentType': '',
...                             'add_input_name': 'splat.txt',
...                             'UPDATE_SUBMIT': 'Update'})
>>> file = File()
>>> view = FileUpload(file, request)
>>> view.errors()
ModifiedEvent: ('contentType',)
u'Updated on ${date_time}'

Cleanup:

>>> zope.event.subscribers.remove(eventLog)

Base classes

Implemented Interfaces

There are no implemented interfaces.

Attributes/Properties

There are no attributes in this class.

Methods

Known Subclasses

There are no known subclasses.