[CMF-checkins] SVN: CMF/trunk/C fixed some encoding issues with forms/formlib:

Yvo Schubbe y.2006_ at wcm-solutions.de
Thu Jul 13 11:21:07 EDT 2006


Log message for revision 69110:
  fixed some encoding issues with forms/formlib:
  - added getBrowserCharset function
  - changed portal_status_message encoding
  - made sure getMainGlobals does not override the Content-Type charset if already set

Changed:
  U   CMF/trunk/CHANGES.txt
  U   CMF/trunk/CMFDefault/browser/utils.py
  U   CMF/trunk/CMFDefault/skins/zpt_control/setRedirect.py
  U   CMF/trunk/CMFDefault/skins/zpt_generic/getMainGlobals.py
  U   CMF/trunk/CMFDefault/utils.py

-=-
Modified: CMF/trunk/CHANGES.txt
===================================================================
--- CMF/trunk/CHANGES.txt	2006-07-13 15:17:21 UTC (rev 69109)
+++ CMF/trunk/CHANGES.txt	2006-07-13 15:21:06 UTC (rev 69110)
@@ -2,6 +2,10 @@
 
   New Features
 
+    - CMFDefault utils: Added getBrowserCharset function.
+      Returns the charset preferred by the browser. Strings encoded with this
+      charset are decoded correctly by Five.browser.decode.processInputs.
+
     - Content: Added IFactory utilities for all content classes.
       They are now used by default instead of the old constructor methods.
 
@@ -15,6 +19,10 @@
 
   Others
 
+    - skins: Changed encoding of translated portal_status_messages.
+      Now getBrowserCharset is used to play nice with Five forms. Customized
+      setRedirect and getMainGlobals scripts have to be updated.
+
     - FSFile: Added registration for 'txt' extension.
 
     - Profiles: All profiles are now registered by ZCML.

Modified: CMF/trunk/CMFDefault/browser/utils.py
===================================================================
--- CMF/trunk/CMFDefault/browser/utils.py	2006-07-13 15:17:21 UTC (rev 69109)
+++ CMF/trunk/CMFDefault/browser/utils.py	2006-07-13 15:21:06 UTC (rev 69110)
@@ -25,6 +25,7 @@
 
 from Products.CMFCore.utils import getToolByName
 from Products.CMFDefault.permissions import View
+from Products.CMFDefault.utils import getBrowserCharset
 from Products.CMFDefault.utils import html_marshal
 from Products.CMFDefault.utils import Message as _
 from Products.CMFDefault.utils import translate
@@ -92,6 +93,10 @@
         ptool = self._getTool('portal_properties')
         return ptool.getProperty('default_charset', None)
 
+    @memoize
+    def _getBrowserCharset(self):
+        return getBrowserCharset(self.request)
+
     # interface
 
     @memoize
@@ -120,7 +125,7 @@
         message = self.request.other.get('portal_status_message', '')
         if message:
             if isinstance(message, unicode):
-                message = message.encode(self._getDefaultCharset())
+                message = message.encode(self._getBrowserCharset())
             kw['portal_status_message'] = message
         for k in keys.split(','):
             k = k.strip()

Modified: CMF/trunk/CMFDefault/skins/zpt_control/setRedirect.py
===================================================================
--- CMF/trunk/CMFDefault/skins/zpt_control/setRedirect.py	2006-07-13 15:17:21 UTC (rev 69109)
+++ CMF/trunk/CMFDefault/skins/zpt_control/setRedirect.py	2006-07-13 15:21:06 UTC (rev 69110)
@@ -2,10 +2,9 @@
 ##
 from ZTUtils import make_query
 from Products.CMFCore.utils import getToolByName
+from Products.CMFDefault.utils import getBrowserCharset
 
 utool = getToolByName(script, 'portal_url')
-ptool = getToolByName(script, 'portal_properties')
-default_charset = ptool.getProperty('default_charset', None)
 portal_url = utool()
 
 
@@ -17,7 +16,7 @@
 message = context.REQUEST.other.get('portal_status_message', '')
 if message:
     if isinstance(message, unicode):
-        message = unicode(message).encode(default_charset)
+        message = unicode(message).encode(getBrowserCharset(context.REQUEST))
     kw['portal_status_message'] = message
 for k, v in kw.items():
     if not v:

Modified: CMF/trunk/CMFDefault/skins/zpt_generic/getMainGlobals.py
===================================================================
--- CMF/trunk/CMFDefault/skins/zpt_generic/getMainGlobals.py	2006-07-13 15:17:21 UTC (rev 69109)
+++ CMF/trunk/CMFDefault/skins/zpt_generic/getMainGlobals.py	2006-07-13 15:21:06 UTC (rev 69110)
@@ -2,6 +2,7 @@
 ##
 from Products.CMFCore.utils import getToolByName
 from Products.CMFDefault.utils import decode
+from Products.CMFDefault.utils import getBrowserCharset
 
 atool = getToolByName(script, 'portal_actions')
 mtool = getToolByName(script, 'portal_membership')
@@ -10,14 +11,18 @@
 wtool = getToolByName(script, 'portal_workflow')
 portal_object = utool.getPortalObject()
 
-default_charset = ptool.getProperty('default_charset', None)
-if default_charset:
-    context.REQUEST.RESPONSE.setHeader('Content-Type',
-                                     'text/html;charset=%s' % default_charset)
+if not 'charset' in context.REQUEST.RESPONSE.getHeader('content-type'):
+    # Some newstyle views set a different charset - don't override it.
+    # Oldstyle views need the default_charset.
+    default_charset = ptool.getProperty('default_charset', None)
+    if default_charset:
+        context.REQUEST.RESPONSE.setHeader('content-type',
+                                    'text/html; charset=%s' % default_charset)
 
 message = context.REQUEST.get('portal_status_message')
 if message and isinstance(message, str):
-    message = message.decode(default_charset)
+    # portal_status_message uses always the browser charset.
+    message = message.decode(getBrowserCharset(context.REQUEST))
 
 globals = {'utool': utool,
            'mtool': mtool,

Modified: CMF/trunk/CMFDefault/utils.py
===================================================================
--- CMF/trunk/CMFDefault/utils.py	2006-07-13 15:17:21 UTC (rev 69109)
+++ CMF/trunk/CMFDefault/utils.py	2006-07-13 15:21:06 UTC (rev 69110)
@@ -26,6 +26,7 @@
 from Products.PageTemplates.GlobalTranslationService \
         import getGlobalTranslationService
 from ZTUtils.Zope import complex_marshal
+from zope.i18n.interfaces import IUserPreferredCharsets
 from zope.i18nmessageid import MessageFactory
 
 from Products.CMFCore.utils import getToolByName
@@ -457,5 +458,13 @@
             pass
     return GTS.translate('cmf_default', message, context=context)
 
+security.declarePublic('getBrowserCharset')
+def getBrowserCharset(request):
+    """ Get charset preferred by the browser.
+    """
+    envadapter = IUserPreferredCharsets(request)
+    charsets = envadapter.getPreferredCharsets() or ['utf-8']
+    return charsets[0]
+
 security.declarePublic('Message')
 Message = _ = MessageFactory('cmf_default')



More information about the CMF-checkins mailing list