[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/file/browser/ Images now use the filename of the data field if no name in the add form is specified like in the File implementation

Bernd Dorn bernd.dorn at fhv.at
Sat Mar 11 03:33:33 EST 2006


Log message for revision 65906:
  Images now use the filename of the data field if no name in the add form is specified like in the File implementation

Changed:
  U   Zope3/trunk/src/zope/app/file/browser/configure.zcml
  U   Zope3/trunk/src/zope/app/file/browser/ftests.py
  U   Zope3/trunk/src/zope/app/file/browser/image.py

-=-
Modified: Zope3/trunk/src/zope/app/file/browser/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/file/browser/configure.zcml	2006-03-10 20:00:26 UTC (rev 65905)
+++ Zope3/trunk/src/zope/app/file/browser/configure.zcml	2006-03-11 08:33:32 UTC (rev 65906)
@@ -100,6 +100,7 @@
       schema="zope.app.file.interfaces.IImage"
       label="Add an Image"
       content_factory="zope.app.file.image.Image"
+      class=".image.ImageAdd"
       name="zope.app.file.Image"
       permission="zope.ManageContent"
       />

Modified: Zope3/trunk/src/zope/app/file/browser/ftests.py
===================================================================
--- Zope3/trunk/src/zope/app/file/browser/ftests.py	2006-03-10 20:00:26 UTC (rev 65905)
+++ Zope3/trunk/src/zope/app/file/browser/ftests.py	2006-03-11 08:33:32 UTC (rev 65906)
@@ -65,6 +65,24 @@
         file = root['file']
         self.assertEqual(file.data, 'A file')
 
+    def testAddWithoutName(self):
+        data = StringIO('File Contents')
+        data.filename="test.txt"
+        response = self.publish(
+            '/+/zope.app.file.File=',
+            form={'type_name': u'zope.app.file.File',
+                  'field.data': data,
+                  'field.data.used': '',
+                  'UPDATE_SUBMIT': u'Add'},
+            basic='mgr:mgrpw')
+        self.assertEqual(response.getStatus(), 302)
+        self.assertEqual(response.getHeader('Location'),
+                         'http://localhost/@@contents.html')
+        root = self.getRootFolder()
+        self.assert_('test.txt' in root)
+        file = root['test.txt']
+        self.assertEqual(file.data, 'File Contents')
+
     def testEditForm(self):
         self.addFile()
         response = self.publish(
@@ -193,6 +211,26 @@
         image = root['image']
         self.assertEqual(image.data, self.content)
 
+    def testAddWithoutName(self):
+        data = StringIO(self.content)
+        data.filename="test.gif"
+        response = self.publish(
+            '/+/zope.app.file.Image=',
+            form={'type_name': u'zope.app.image.Image',
+                  'field.data': data,
+                  'field.data.used': '',
+                  'UPDATE_SUBMIT': u'Add'},
+            basic='mgr:mgrpw')
+        self.assertEqual(response.getStatus(), 302)
+        self.assertEqual(response.getHeader('Location'),
+                         'http://localhost/@@contents.html')
+        root = self.getRootFolder()
+        self.assert_('test.gif' in root)
+        image = root['test.gif']
+        self.assertEqual(image.data, self.content)
+
+
+
     def testUploadForm(self):
         self.addImage()
         response = self.publish(

Modified: Zope3/trunk/src/zope/app/file/browser/image.py
===================================================================
--- Zope3/trunk/src/zope/app/file/browser/image.py	2006-03-10 20:00:26 UTC (rev 65905)
+++ Zope3/trunk/src/zope/app/file/browser/image.py	2006-03-11 08:33:32 UTC (rev 65906)
@@ -18,6 +18,8 @@
 __docformat__ = 'restructuredtext'
 
 from zope.app.size.interfaces import ISized
+from zope.app.form.browser.submit import Update
+from zope.app.form.utility import getWidgetsData
 
 class ImageData(object):
 
@@ -89,3 +91,20 @@
     def size(self):
         sized = ISized(self.context)
         return sized.sizeForDisplay()
+
+class ImageAdd(object):
+    
+    def update(self):
+        
+        if self.update_status is not None:
+            # We've been called before. Just return the previous result.
+            return self.update_status
+
+        # set the name of the image if not defined
+        if not self.request.form.get('add_input_name'):
+            filename = getattr(self.request.form.get("field.data"),
+                               "filename", None)
+            if filename is not None:
+                self.request.form["add_input_name"] = filename
+
+        return super(ImageAdd,self).update()



More information about the Zope3-Checkins mailing list