Skip to content

Commit

Permalink
Fix error when not selecting a file for upload in Files and Images (#497
Browse files Browse the repository at this point in the history
)

* Fix error when not selecting a file for upload in Files and Images

* - under Python 2 that ValueError is not thrown. Sigh.
  • Loading branch information
dataflake committed Feb 13, 2019
1 parent fb0377c commit 4301984
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -17,6 +17,9 @@ Fixes
- Fix handling of DTML in Ace editor
(`#489 <https://github.com/zopefoundation/Zope/issues/489>`_)

- Fix error when not selecting a file for upload in Files and Images
(`#492 <https://github.com/zopefoundation/Zope/issues/492>`_)


4.0b9 (2019-02-09)
------------------
Expand Down
18 changes: 10 additions & 8 deletions src/OFS/Image.py
Expand Up @@ -553,17 +553,19 @@ def manage_upload(self, file='', REQUEST=None):
if self.wl_isLocked():
raise ResourceLockedError("File is locked.")

data, size = self._read_data(file)
content_type = self._get_content_type(file, data, self.__name__,
'application/octet-stream')
self.update_data(data, content_type, size)

notify(ObjectModifiedEvent(self))
if file:
data, size = self._read_data(file)
content_type = self._get_content_type(file, data, self.__name__,
'application/octet-stream')
self.update_data(data, content_type, size)
notify(ObjectModifiedEvent(self))
msg = 'Saved changes.'
else:
msg = 'Please select a file to upload.'

if REQUEST:
message = "Saved changes."
return self.manage_main(
self, REQUEST, manage_tabs_message=message)
self, REQUEST, manage_tabs_message=msg)

def _get_content_type(self, file, body, id, content_type=None):
headers = getattr(file, 'headers', None)
Expand Down
4 changes: 4 additions & 0 deletions src/OFS/tests/testFileAndImage.py
Expand Up @@ -228,6 +228,10 @@ def testManageUpload(self):
self.assertEqual(1, len(self.eventCatcher.modified))
self.assertTrue(self.eventCatcher.modified[0].object is self.file)

def testManageUploadWithoutFileData(self):
self.file.manage_upload()
self.assertEqual(0, len(self.eventCatcher.modified))

def testIfModSince(self):
now = time.time()
e = {'SERVER_NAME': 'foo',
Expand Down

0 comments on commit 4301984

Please sign in to comment.