[Zope3-checkins] SVN: Zope3/trunk/ - switched fieldstorage to a custom class that creates temporary

Christian Theune ct at gocept.com
Thu Mar 8 16:52:25 EST 2007


Log message for revision 73077:
   - switched fieldstorage to a custom class that creates temporary
     files which allow access to their filename
  

Changed:
  U   Zope3/trunk/doc/CHANGES.txt
  U   Zope3/trunk/src/zope/publisher/browser.py
  U   Zope3/trunk/src/zope/publisher/tests/test_browserrequest.py

-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2007-03-08 21:41:39 UTC (rev 73076)
+++ Zope3/trunk/doc/CHANGES.txt	2007-03-08 21:52:25 UTC (rev 73077)
@@ -10,6 +10,9 @@
 
     New features
 
+      - Implemented a custom FieldStorage that uses named temporary files to
+        allow the Blob integration to use `consumeFile` on uploaded files.
+
       - added widget for zope.schema.FrozenSet like for zope.schema.Set.
 
       - icon zcml directive supports now 'width' and 'height'

Modified: Zope3/trunk/src/zope/publisher/browser.py
===================================================================
--- Zope3/trunk/src/zope/publisher/browser.py	2007-03-08 21:41:39 UTC (rev 73076)
+++ Zope3/trunk/src/zope/publisher/browser.py	2007-03-08 21:52:25 UTC (rev 73077)
@@ -25,6 +25,7 @@
 import re
 from types import ListType, TupleType, StringType
 from cgi import FieldStorage
+import tempfile
 
 import zope.component
 from zope.interface import implements, directlyProvides
@@ -263,7 +264,8 @@
         if 'QUERY_STRING' not in self._environ:
             self._environ['QUERY_STRING'] = ''
 
-        fs = FieldStorage(fp=fp, environ=self._environ, keep_blank_values=1)
+        fs = ZopeFieldStorage(fp=fp, environ=self._environ,
+                              keep_blank_values=1)
 
         fslist = getattr(fs, 'list', None)
         if fslist is not None:
@@ -571,6 +573,12 @@
         return default
 
 
+class ZopeFieldStorage(FieldStorage):
+
+    def make_file(self, binary=None):
+        return tempfile.NamedTemporaryFile('w+b')
+
+
 class FileUpload(object):
     '''File upload objects
 
@@ -591,7 +599,8 @@
         else:
             methods = ['close', 'fileno', 'flush', 'isatty',
                 'read', 'readline', 'readlines', 'seek',
-                'tell', 'truncate', 'write', 'writelines']
+                'tell', 'truncate', 'write', 'writelines',
+                'name']
 
         d = self.__dict__
         for m in methods:

Modified: Zope3/trunk/src/zope/publisher/tests/test_browserrequest.py
===================================================================
--- Zope3/trunk/src/zope/publisher/tests/test_browserrequest.py	2007-03-08 21:41:39 UTC (rev 73076)
+++ Zope3/trunk/src/zope/publisher/tests/test_browserrequest.py	2007-03-08 21:52:25 UTC (rev 73077)
@@ -35,7 +35,14 @@
 
 from zope.publisher.publish import publish as publish_
 
+LARGE_FILE_BODY = """-----------------------------1
+Content-Disposition: form-data; name="upload"; filename="test"
+Content-Type: text/plain
 
+Here comes some text! %s
+-----------------------------1--
+""" % ('test' * 1000)
+
 def publish(request):
     publish_(request, handle_errors=0)
 
@@ -176,7 +183,19 @@
         request  = self._createRequest(extra,body=body)
         request.processInputs()
 
+    def testFileUploadPost(self):
+        """Produce a Fieldstorage with a file handle that exposes
+        its filename."""
 
+        extra = {'REQUEST_METHOD':'POST',
+                 'PATH_INFO': u'/',
+                 'CONTENT_TYPE': 'multipart/form-data;\
+                 boundary=---------------------------1'}
+        
+        request  = self._createRequest(extra, body=LARGE_FILE_BODY)
+        request.processInputs()
+        self.assert_(request.form['upload'].name)
+
     def testDefault2(self):
         extra = {'PATH_INFO': '/folder/item2/view'}
         request = self._createRequest(extra)



More information about the Zope3-Checkins mailing list