[Zope3-checkins] SVN: Zope3/branches/jhauser-filefieldwidget/src/zope/ Commit the actual state

Roger Ineichen roger at projekt01.ch
Fri Jan 21 07:52:58 EST 2005


Log message for revision 28904:
  Commit the actual state
  o Added BBB for upload.html page for IFile
  o Changed edit.html to BBB_edit.html and upload.html to BBB_ulopad.html
    let them out of scope right now.
  o New style forms are not working and tested
  o Added initial widgets
  o TODO: Start adding MimeWidget

Changed:
  U   Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/browser/configure.zcml
  U   Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/browser/ftests.py
  U   Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/interfaces.py
  U   Zope3/branches/jhauser-filefieldwidget/src/zope/app/form/browser/__init__.py
  U   Zope3/branches/jhauser-filefieldwidget/src/zope/app/form/browser/configure.zcml
  U   Zope3/branches/jhauser-filefieldwidget/src/zope/app/form/browser/filewidgets.py
  U   Zope3/branches/jhauser-filefieldwidget/src/zope/schema/_field.py

-=-
Modified: Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/browser/configure.zcml
===================================================================
--- Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/browser/configure.zcml	2005-01-21 02:23:45 UTC (rev 28903)
+++ Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/browser/configure.zcml	2005-01-21 12:52:57 UTC (rev 28904)
@@ -10,9 +10,10 @@
       class="zope.app.publication.traversers.FileContentTraverser"
       permission="zope.Public"
       />
-  
+
+<!-- Old style text-based files -->
   <browser:editform
-      name="edit.html"
+      name="BBB_edit.html"
       schema="zope.app.file.interfaces.IFile"
       label="Change a file"
       permission="zope.ManageContent" 
@@ -24,6 +25,7 @@
 
   </browser:editform>
 
+
   <browser:menuItem
       menu="zmi_views" title="Edit"
       for="zope.app.file.interfaces.IFile"
@@ -31,10 +33,25 @@
       filter="python:context.contentType.startswith('text/')"
       permission="zope.ManageContent" />
 
-  <browser:page
+
+
+<!-- upload for new style files -->
+
+  <browser:editform
       name="upload.html"
       menu="zmi_views" title="Upload"
       for="zope.app.file.interfaces.IFile"
+      schema="zope.app.file.interfaces.IFile"
+      fields="contents"
+      permission="zope.ManageContent"
+      />
+
+<!-- BBB: upload for old style files -->
+
+  <browser:page
+      name="BBB_upload.html"
+      menu="zmi_views" title="BBB Upload"
+      for="zope.app.file.interfaces.IFile"
       template="file_upload.pt"
       class=".file.FileUpload"
       permission="zope.ManageContent"

Modified: Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/browser/ftests.py
===================================================================
--- Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/browser/ftests.py	2005-01-21 02:23:45 UTC (rev 28903)
+++ Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/browser/ftests.py	2005-01-21 12:52:57 UTC (rev 28904)
@@ -65,7 +65,7 @@
     def testEditForm(self):
         self.addFile()
         response = self.publish(
-            '/file/@@edit.html',
+            '/file/@@BBB_edit.html',
             basic='mgr:mgrpw')
         self.assertEqual(response.getStatus(), 200)
         body = response.getBody()
@@ -78,7 +78,7 @@
     def testEdit(self):
         self.addFile()
         response = self.publish(
-            '/file/@@edit.html',
+            '/file/@@BBB_edit.html',
             form={'field.data': u'<h1>A File</h1>',
                   'field.contentType': u'text/plain',
                   'UPDATE_SUBMIT': u'Edit'},
@@ -97,7 +97,7 @@
     def testUploadForm(self):
         self.addFile()
         response = self.publish(
-            '/file/@@upload.html',
+            '/file/@@BBB_upload.html',
             basic='mgr:mgrpw')
         self.assertEqual(response.getStatus(), 200)
         body = response.getBody()
@@ -110,7 +110,7 @@
     def testUpload(self):
         self.addFile()
         response = self.publish(
-            '/file/@@upload.html',
+            '/file/@@BBB_upload.html',
             form={'field.data': StringIO('<h1>A file</h1>'),
                   'field.contentType': u'text/plain',
                   'UPDATE_SUBMIT': u'Change'},

Modified: Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/interfaces.py
===================================================================
--- Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/interfaces.py	2005-01-21 02:23:45 UTC (rev 28903)
+++ Zope3/branches/jhauser-filefieldwidget/src/zope/app/file/interfaces.py	2005-01-21 12:52:57 UTC (rev 28904)
@@ -89,7 +89,7 @@
 
 class IFile(Interface):
 
-    content = Mime(
+    contents = Mime(
         title = _(u'The file data'),
         description = _(u'The mime information and file data, which can be '
                          'read as a file.'),

Modified: Zope3/branches/jhauser-filefieldwidget/src/zope/app/form/browser/__init__.py
===================================================================
--- Zope3/branches/jhauser-filefieldwidget/src/zope/app/form/browser/__init__.py	2005-01-21 02:23:45 UTC (rev 28903)
+++ Zope3/branches/jhauser-filefieldwidget/src/zope/app/form/browser/__init__.py	2005-01-21 12:52:57 UTC (rev 28904)
@@ -31,7 +31,11 @@
 
 # Widgets for file-based fields
 from zope.app.form.browser.filewidgets import FileWidget
-from zope.app.form.browser.filewidgets import MimeDisplayWidget, MimeWidget
+from zope.app.form.browser.filewidgets import MimeWidget
+from zope.app.form.browser.filewidgets import MimeDataWidget
+from zope.app.form.browser.filewidgets import MimeDataEncodingWidget
+from zope.app.form.browser.filewidgets import MimeTypeWidget
+from zope.app.form.browser.filewidgets import MimeDisplayWidget
 
 # Widgets for boolean fields
 from zope.app.form.browser.boolwidgets import CheckBoxWidget

Modified: Zope3/branches/jhauser-filefieldwidget/src/zope/app/form/browser/configure.zcml
===================================================================
--- Zope3/branches/jhauser-filefieldwidget/src/zope/app/form/browser/configure.zcml	2005-01-21 02:23:45 UTC (rev 28903)
+++ Zope3/branches/jhauser-filefieldwidget/src/zope/app/form/browser/configure.zcml	2005-01-21 12:52:57 UTC (rev 28904)
@@ -154,6 +154,30 @@
 
   <view
       type="zope.publisher.interfaces.browser.IBrowserRequest"
+      for="zope.schema.interfaces.IMimeData"
+      provides="zope.app.form.interfaces.IInputWidget"
+      factory=".MimeDataWidget"
+      permission="zope.Public"
+      />
+
+  <view
+      type="zope.publisher.interfaces.browser.IBrowserRequest"
+      for="zope.schema.interfaces.IMimeDataEncoding"
+      provides="zope.app.form.interfaces.IInputWidget"
+      factory=".MimeDataEncodingWidget"
+      permission="zope.Public"
+      />
+
+  <view
+      type="zope.publisher.interfaces.browser.IBrowserRequest"
+      for="zope.schema.interfaces.IMimeType"
+      provides="zope.app.form.interfaces.IInputWidget"
+      factory=".MimeTypeWidget"
+      permission="zope.Public"
+      />
+
+  <view
+      type="zope.publisher.interfaces.browser.IBrowserRequest"
       for="zope.schema.interfaces.IASCII"
       provides="zope.app.form.interfaces.IInputWidget"
       factory=".BytesAreaWidget"

Modified: Zope3/branches/jhauser-filefieldwidget/src/zope/app/form/browser/filewidgets.py
===================================================================
--- Zope3/branches/jhauser-filefieldwidget/src/zope/app/form/browser/filewidgets.py	2005-01-21 02:23:45 UTC (rev 28903)
+++ Zope3/branches/jhauser-filefieldwidget/src/zope/app/form/browser/filewidgets.py	2005-01-21 12:52:57 UTC (rev 28904)
@@ -22,6 +22,7 @@
 
 from zope.app.form.interfaces import IInputWidget, ConversionError
 from zope.app.form.browser.widget import SimpleInputWidget, renderElement
+from zope.app.form.browser.textwidgets import BytesWidget
 from zope.app.form.browser.widget import DisplayWidget
 from zope.app.form.browser.textwidgets import escape
 
@@ -46,10 +47,77 @@
     The session is initiated from the MimeWidget and contains the fileupload.
     The method _toFieldValue() reads the fileupload (input) from the session.
     """
-    pass
+    type = 'file'
 
+    default = ''
+    displayWidth = 20
+    displayMaxWidth = ""
+    extra = ''
+    style = ''
+    convert_missing_value = True
 
-class MimeTypeWidget(SimpleInputWidget):
+    def __call__(self):
+        # XXX set the width to 40 to be sure to recognize this widget
+        displayMaxWidth = self.displayMaxWidth or 0
+        if displayMaxWidth > 0:
+            return renderElement(self.tag,
+                                 type=self.type,
+                                 name=self.name,
+                                 id=self.name,
+                                 cssClass=self.cssClass,
+                                 size=40,
+                                 maxlength=40,
+                                 extra=self.extra)
+        else:
+            return renderElement(self.tag,
+                                 type=self.type,
+                                 name=self.name,
+                                 id=self.name,
+                                 cssClass=self.cssClass,
+                                 size=40,
+                                 extra=self.extra)
+
+    def _toFieldValue(self, input):
+        if input == '':
+            return self.context.missing_value
+        try:
+            seek = input.seek
+            read = input.read
+        except AttributeError, e:
+            raise ConversionError('Form input is not a file object', e)
+        else:
+            # if the FileUpload instance has no filename set, there is
+            # no upload.
+            if getattr(input, 'filename', ''):
+                return input
+            else:
+                return self.context.missing_value
+
+    def applyChanges(self, content):
+        field = self.context
+        value = self.getInputValue()
+        # need to test for value, as an empty field is not an error, but
+        # the current file should not be replaced.
+        if value and (field.query(content, self) != value):
+            field.set(content, value)
+            return True
+        else:
+            return False
+
+
+# TODO: add better description.
+
+# till now we use a simply BytesWidget, later we can make use of a vocabulary
+# for list the encoding via a Choice field 
+class MimeDataEncodingWidget(BytesWidget):
+    """MimeDataEncodingWidget set the encoding on text-based data.
+    
+    If we have a file with a mime-type 'text/...' we can set the encoding.
+    """
+
+# till now we use a simply BytesWidget, later we can make use of a vocabulary
+# for list the encoding via a Choice field 
+class MimeTypeWidget(BytesWidget):
     """MimeTypeWidget is used for to guess the mime-type.
     
     The session is initiated from the MimeWidget and contains the filename.
@@ -59,15 +127,6 @@
     pass
 
 
-# TODO: better description.
-class MimeDataEncodingWidget(SimpleInputWidget):
-    """MimeDataEncodingWidget set the encoding on text-based data.
-    
-    If we have a file with a mime-type 'text/...' we can set the encoding.
-    """
-    pass
-
-
 class MimeDisplayWidget(DisplayWidget):
     """Mime data display widget."""
     # There need to be probably some widget options to determine how
@@ -134,8 +193,8 @@
             else:
                 return self.context.missing_value
 
-
-class MimeWidget(SimpleInputWidget):
+# TODO: remove it
+class XXXMimeWidget(SimpleInputWidget):
     u"""Mime file upload widget"""
 
     type = 'file'

Modified: Zope3/branches/jhauser-filefieldwidget/src/zope/schema/_field.py
===================================================================
--- Zope3/branches/jhauser-filefieldwidget/src/zope/schema/_field.py	2005-01-21 02:23:45 UTC (rev 28903)
+++ Zope3/branches/jhauser-filefieldwidget/src/zope/schema/_field.py	2005-01-21 12:52:57 UTC (rev 28904)
@@ -176,20 +176,61 @@
             return ''
 
 
+# TODO: add encodng vocabulary for selecting possible mime-types
 class MimeDataEncoding(Field):
     __doc__ = IMimeDataEncoding.__doc__
-    implements(IMimeDataEncoding)
+    implements(IMimeDataEncoding, IFromUnicode)
 
     _type = str
 
+    def fromUnicode(self, u):
+        """
+        >>> b = Bytes(constraint=lambda v: 'x' in v)
 
+        >>> b.fromUnicode(u" foo x.y.z bat")
+        ' foo x.y.z bat'
+        >>> b.fromUnicode(u" foo y.z bat")
+        Traceback (most recent call last):
+        ...
+        ConstraintNotSatisfied:  foo y.z bat
+
+        """
+        v = str(u)
+        self.validate(v)
+        return v
+
+    def constraint(self, value):
+        return '\n' not in value
+
+
+# TODO: perhaps add mime-type vocabulary for possible mime-types.
+# If so, we need also to list the mime-types from the python lib mimetypes
 class MimeType(Field):
     __doc__ = IMimeType.__doc__
-    implements(IMimeType)
+    implements(IMimeType, IFromUnicode)
 
     _type = str
 
+    def fromUnicode(self, u):
+        """
+        >>> b = Bytes(constraint=lambda v: 'x' in v)
 
+        >>> b.fromUnicode(u" foo x.y.z bat")
+        ' foo x.y.z bat'
+        >>> b.fromUnicode(u" foo y.z bat")
+        Traceback (most recent call last):
+        ...
+        ConstraintNotSatisfied:  foo y.z bat
+
+        """
+        v = str(u)
+        self.validate(v)
+        return v
+
+    def constraint(self, value):
+        return '\n' not in value
+
+
 class ASCII(Bytes):
     __doc__ = IASCII.__doc__
     implements(IASCII)



More information about the Zope3-Checkins mailing list