[Zope3-checkins]
SVN: Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/
Added mimefield module which is a field for uploaded file data.
Janko Hauser
jhauser at zscout.de
Sun Jan 16 13:12:14 EST 2005
Log message for revision 28849:
Added mimefield module which is a field for uploaded file data.
Also added test script.
Changed:
A Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/mimefield.py
A Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/tests/test_mimefield.py
-=-
Added: Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/mimefield.py
===================================================================
--- Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/mimefield.py 2005-01-16 18:10:35 UTC (rev 28848)
+++ Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/mimefield.py 2005-01-16 18:12:14 UTC (rev 28849)
@@ -0,0 +1,180 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""File content component
+
+$Id: $
+"""
+__docformat__ = 'restructuredtext'
+
+from persistent import Persistent
+from transaction import get_transaction
+from zope.interface import implements
+
+from zope.schema.interfaces import IBytesLine
+from zope.schema._bootstrapfields import Field
+from zope.schema._bootstrapfields import TextLine
+from zope.schema._field import BytesLine
+from zope.app.file.file import File
+
+from zope.i18nmessageid import MessageIDFactory
+_ = MessageIDFactory("zope")
+
+from interfaces import IFile
+
+#
+# The basic schema interface
+#
+class IMime(IBytesLine):
+ u"""Fields which hold data characterized by a mime type.
+
+ The data is stored memory effecient.
+ """
+
+ contentType = TextLine(title=_(u"Mime type"),
+ description=_(u"The mime type of the stored data"),
+ required=False,
+ default=u"application/octet-stream"
+ )
+
+ def getSize():
+ u"""Return the size of the stored data in bytes."""
+
+class IFileData(IMime):
+ u"""Fields which hold uploaded data, mainly file type data"""
+
+ filename = TextLine(title=_(u"Filename"),
+ description=_(u"The Filename of the uploaded file"),
+ required=False)
+
+
+ # The field implementation
+class FileData(BytesLine, File):
+ """A field implementation for uploaded files.
+
+ Let's test the constructor:
+
+ >>> file = FileData()
+ >>> file.contentType
+ ''
+ >>> file.data
+ ''
+
+ >>> file = FileData('Foobar')
+ >>> file.contentType
+ ''
+ >>> file.data
+ 'Foobar'
+
+ >>> file = FileData('Foobar', 'text/plain')
+ >>> file.contentType
+ 'text/plain'
+ >>> file.data
+ 'Foobar'
+
+ >>> file = FileData(data='Foobar', contentType='text/plain')
+ >>> file.contentType
+ 'text/plain'
+ >>> file.data
+ 'Foobar'
+
+
+ Let's test the mutators:
+
+ >>> file = FileData()
+ >>> file.contentType = 'text/plain'
+ >>> file.contentType
+ 'text/plain'
+
+ >>> file.data = 'Foobar'
+ >>> file.data
+ 'Foobar'
+
+ >>> file.data = None
+ Traceback (most recent call last):
+ ...
+ TypeError: Cannot set None data on a file.
+
+
+ Let's test large data input:
+
+ >>> file = FileData()
+
+ Insert as string:
+
+ >>> file.data = 'Foobar'*60000
+ >>> file.getSize()
+ 360000
+ >>> file.data == 'Foobar'*60000
+ True
+
+ Insert data as FileChunk:
+ >>> from zope.app.file.file import FileChunk
+ >>> fc = FileChunk('Foobar'*4000)
+ >>> file.data = fc
+ >>> file.getSize()
+ 24000
+ >>> file.data == 'Foobar'*4000
+ True
+
+ Insert data from file object:
+
+ >>> import cStringIO
+ >>> sio = cStringIO.StringIO()
+ >>> sio.write('Foobar'*100000)
+ >>> sio.seek(0)
+ >>> file.data = sio
+ >>> file.getSize()
+ 600000
+ >>> file.data == 'Foobar'*100000
+ True
+
+ Test handling of filename
+
+ >>> file.filename == ''
+ True
+
+ Last, but not least, verify the interface:
+
+ >>> from zope.interface.verify import verifyClass
+ >>> IFile.implementedBy(File)
+ True
+ >>> verifyClass(IFile, File)
+ True
+ """
+
+ implements(IFileData, IFile)
+
+ def __init__(self, data='', contentType=''):
+ self.data = data
+ # instead of mimeType we use contentType as it is mandated by IFile
+ self.contentType = contentType
+ self.filename = self._extractFilename(data)
+
+ def _setdata(self, data):
+ File._setdata(data)
+ self.filename = self._extractFilename(data)
+
+ def _extractFilename(self, data):
+ # if it is a fileupload object
+ if hasattr(data,'filename'):
+ fid = data.filename
+ # prepare from ospath filenames from explorer.
+ fid=fid[max(fid.rfind('/'),
+ fid.rfind('\\'),
+ fid.rfind(':')
+ )+1:]
+ return fid
+ else:
+ return ''
+
Added: Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/tests/test_mimefield.py
===================================================================
--- Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/tests/test_mimefield.py 2005-01-16 18:10:35 UTC (rev 28848)
+++ Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/tests/test_mimefield.py 2005-01-16 18:12:14 UTC (rev 28849)
@@ -0,0 +1,27 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Test file field implementation.
+
+$Id: $
+"""
+import unittest
+from zope.testing.doctestunit import DocTestSuite
+
+def test_suite():
+ return unittest.TestSuite((
+ DocTestSuite('zope.app.file.mimefield'),
+ ))
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
More information about the Zope3-Checkins
mailing list