Commit f3ee1917 authored by Jérome Perrin's avatar Jérome Perrin

Don't create subobject when provided FileUpload is empty

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@29068 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 2ea730fc
...@@ -26,7 +26,8 @@ ...@@ -26,7 +26,8 @@
# #
############################################################################## ##############################################################################
from Acquisition import aq_base
from ZPublisher.HTTPRequest import FileUpload
from Base import func_code, type_definition, list_types, ATTRIBUTE_PREFIX, Getter as BaseGetter, Setter as BaseSetter from Base import func_code, type_definition, list_types, ATTRIBUTE_PREFIX, Getter as BaseGetter, Setter as BaseSetter
from Products.ERP5Type.PsycoWrapper import psyco from Products.ERP5Type.PsycoWrapper import psyco
from zLOG import LOG from zLOG import LOG
...@@ -161,6 +162,18 @@ class Setter(BaseSetter): ...@@ -161,6 +162,18 @@ class Setter(BaseSetter):
assertAttributePortalType(instance, self._storage_id, self._portal_type) assertAttributePortalType(instance, self._storage_id, self._portal_type)
o = instance._getOb(self._storage_id, None) o = instance._getOb(self._storage_id, None)
if o is None: if o is None:
if self._acquired_property == 'file':
if isinstance(value, FileUpload) or \
getattr(aq_base(value), 'tell', None) is not None:
# When editing through the web interface, we are always provided a
# FileUpload, and when no file has been specified, the file is empty.
# In the case of empty file, we should not create the sub document.
value.seek(0, 2)
is_empty_file = not value.tell()
value.seek(0)
if is_empty_file:
return ()
o = instance.newContent(id=self._storage_id, o = instance.newContent(id=self._storage_id,
portal_type=self._portal_type[0]) portal_type=self._portal_type[0])
o._setProperty(self._acquired_property, value, *args, **kw) o._setProperty(self._acquired_property, value, *args, **kw)
......
...@@ -75,20 +75,28 @@ class Setter(Method): ...@@ -75,20 +75,28 @@ class Setter(Method):
elif self._property_type == 'content': elif self._property_type == 'content':
# A file object should be provided # A file object should be provided
file_upload = args[0] file_upload = args[0]
if isinstance(file_upload, FileUpload): if isinstance(file_upload, FileUpload) or \
if file_upload: getattr(aq_base(value), 'tell', None) is not None:
try: # When editing through the web interface, we are always provided a
o = getattr(instance, self._storage_id) # FileUpload, and when no file has been specified, the file is empty.
except AttributeError: # In the case of empty file, we should not create the sub document.
# But, i don't think this code is actually used ...
file_upload.seek(0, 2)
is_empty_file = not file_upload.tell()
file_upload.seek(0)
if not is_empty_file:
content_document = instance._getOb(self._storage_id, None)
if content_document is None:
# We create a default type # We create a default type
o = instance.PUT_factory(self._storage_id, content_document = instance.PUT_factory(self._storage_id,
file_upload.headers.get('content-type', None), file_upload) file_upload.headers.get('content-type', None), file_upload)
instance._setObject(self._storage_id, o) instance._setObject(self._storage_id, content_document)
o = getattr(instance, self._storage_id) content_document = getattr(instance, self._storage_id)
o.manage_upload(file = file_upload) content_document.manage_upload(file = file_upload)
modified_object_list = [o] modified_object_list = [content_document]
else: else:
LOG('ERP5Type WARNING', 0, '%s is not an instance of FileUpload' LOG('ERP5Type WARNING', 0, '%s is not a file like object'
% str(file_upload)) % str(file_upload))
else: else:
setattr(instance, self._storage_id, self._cast(args[0])) setattr(instance, self._storage_id, self._cast(args[0]))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment