[Zope-Checkins] SVN: Zope/branches/ajung-zpt-integration/lib/python/Products/ZPT/ - smarter upload

Andreas Jung andreas at andreas-jung.com
Fri Dec 9 12:06:16 EST 2005


Log message for revision 40666:
  - smarter upload
  - enforcing UTF-8 in the ZMI
  - enforcing unicode as internal  representation
  - added some assertions to check for unicode
  

Changed:
  U   Zope/branches/ajung-zpt-integration/lib/python/Products/ZPT/ZPT.py
  U   Zope/branches/ajung-zpt-integration/lib/python/Products/ZPT/pt/ptEdit.zpt

-=-
Modified: Zope/branches/ajung-zpt-integration/lib/python/Products/ZPT/ZPT.py
===================================================================
--- Zope/branches/ajung-zpt-integration/lib/python/Products/ZPT/ZPT.py	2005-12-09 16:56:40 UTC (rev 40665)
+++ Zope/branches/ajung-zpt-integration/lib/python/Products/ZPT/ZPT.py	2005-12-09 17:06:16 UTC (rev 40666)
@@ -20,6 +20,7 @@
 import os, AccessControl, Acquisition 
 from Globals import ImageFile, package_home, InitializeClass
 from OFS.SimpleItem import SimpleItem
+from OFS.content_types import guess_content_type
 from DateTime.DateTime import DateTime
 from Shared.DC.Scripts.Script import Script 
 from Shared.DC.Scripts.Signature import FuncCode
@@ -63,6 +64,7 @@
     __implements__ = (WriteLockInterface,)
 
     meta_type = 'ZPT'
+    management_page_charset = 'utf-8'
 
     func_defaults = None
     func_code = FuncCode((), 0)
@@ -90,12 +92,12 @@
     security.declareObjectProtected(view)
     security.declareProtected(view, '__call__')
 
-    def __init__(self, id, text=None, content_type=None):
+    def __init__(self, id, text=None, content_type=None, encoding='utf-8'):
         self.id = str(id)
         self.ZBindings_edit(self._default_bindings)
         if text is None:
             text = open(self._default_content_fn).read()
-        self.pt_edit(text, content_type)
+        self.pt_edit(text, content_type, encoding)
 
     def _setPropValue(self, id, value):
         PropertyManager._setPropValue(self, id, value)
@@ -104,7 +106,11 @@
 
 
     security.declareProtected(change_page_templates, 'pt_edit')
-    def pt_edit(self, text, content_type):
+    def pt_edit(self, text, content_type, encoding='utf-8'):
+        if not isinstance(text, unicode):
+            text = unicode(text, encoding, 'strict')
+        assert isinstance(text, unicode)
+        self.ZCacheable_invalidate()
         PageTemplate.pt_edit(self, text, content_type)
 
     security.declareProtected(change_page_templates, 'pt_editAction')
@@ -143,16 +149,22 @@
         if self.wl_isLocked():
             raise ResourceLockedError("File is locked via WebDAV")
 
+        filename = None
         if not isinstance(file, str):
             if not file: raise ValueError('File not specified')
+            filename = file.filename
             file = file.read()
-        if charset:
-            try:
-                unicode(file, 'us-ascii')
-                file = str(file)
-            except UnicodeDecodeError:
-                file = unicode(file, charset)
-        self.write(file)
+
+        ct, dummy = guess_content_type(filename, file)   
+        if not ct in ('text/html', 'text/xml'):
+            raise ValueError('Unsupported mimetype: %s' % ct)
+
+        if not isinstance(file, unicode):
+            if not charset:
+                raise ValueError('No encoding specified for non-unicode content')
+            file = unicode(file, charset)
+
+        self.pt_edit(file, ct)
         message = 'Saved changes.'
         return self.pt_editForm(manage_tabs_message=message)
 
@@ -205,10 +217,9 @@
              }
         return c
 
-    security.declareProtected(change_page_templates, 'write')
-    def write(self, text):
-        self.ZCacheable_invalidate()
-        PageTemplate.write(self, text)
+#    security.declareProtected(change_page_templates, 'write')
+#    def write(self, text):
+#        PageTemplate.write(self, text)
 
     security.declareProtected(view_management_screens, 'manage_main', 'read',
       'ZScriptHTML_tryForm')
@@ -248,6 +259,7 @@
         try:
             # XXX: check the parameters for pt_render()! (aj)
             result = self.pt_render(self.pt_getContext())
+            assert isinstance(result, unicode)
 
 #            result = self.pt_render(extra_context=bound_names)
             if keyset is not None:
@@ -266,7 +278,8 @@
         """ Handle HTTP PUT requests """
         self.dav__init(REQUEST, RESPONSE)
         self.dav__simpleifhandler(REQUEST, RESPONSE, refresh=1)
-        self.write(REQUEST.get('BODY', ''))
+        ## XXX:this should be unicode or we must pass an encoding
+        self.pt_edit(REQUEST.get('BODY', ''))
         RESPONSE.setStatus(204)
         return RESPONSE
 
@@ -281,7 +294,7 @@
         self.REQUEST.RESPONSE.setHeader('Content-Type', self.content_type)
         return self.read()
 
-    security.declareProtected(view_manage_screens, 'html')
+    security.declareProtected(view_management_screens, 'html')
     def html(self):
         return self.content_type == 'text/html'
         

Modified: Zope/branches/ajung-zpt-integration/lib/python/Products/ZPT/pt/ptEdit.zpt
===================================================================
--- Zope/branches/ajung-zpt-integration/lib/python/Products/ZPT/pt/ptEdit.zpt	2005-12-09 16:56:40 UTC (rev 40665)
+++ Zope/branches/ajung-zpt-integration/lib/python/Products/ZPT/pt/ptEdit.zpt	2005-12-09 17:06:16 UTC (rev 40666)
@@ -123,15 +123,14 @@
   <input type="file" name="file" size="25" value="">
   </td>
 </tr>
-<tr tal:condition="context/management_page_charset|nothing">
+<tr>
   <td align="left" valign="top">
     <div class="form-label">
       Encoding &nbsp;
     </div>
   </td>
   <td align="left" valign="top">
-    <input name="charset" value=""
-      tal:attributes="value context/management_page_charset|default" />
+    <input name="charset" value="utf-8"/>
   </td>
 </tr>
 <tr>



More information about the Zope-Checkins mailing list