[Zope-Checkins] SVN: Zope/trunk/lib/python/Products/PageTemplates/unicodeconflictresolver.py deal with the case that 'context' must not have a REQUEST object

Andreas Jung andreas at andreas-jung.com
Sat Aug 2 07:54:52 EDT 2008


Log message for revision 89200:
  deal with the case that 'context' must not have a REQUEST object
  (test comes later)
  

Changed:
  U   Zope/trunk/lib/python/Products/PageTemplates/unicodeconflictresolver.py

-=-
Modified: Zope/trunk/lib/python/Products/PageTemplates/unicodeconflictresolver.py
===================================================================
--- Zope/trunk/lib/python/Products/PageTemplates/unicodeconflictresolver.py	2008-08-02 11:50:45 UTC (rev 89199)
+++ Zope/trunk/lib/python/Products/PageTemplates/unicodeconflictresolver.py	2008-08-02 11:54:52 UTC (rev 89200)
@@ -62,25 +62,42 @@
 
     def resolve(self, context, text, expression):
 
-        request = context.REQUEST
+        request = getattr(context, 'REQUEST', None)
 
-        charsets = getattr(request, '__zpt_available_charsets', None)
-        if charsets is None:
-            charsets = IUserPreferredCharsets(request).getPreferredCharsets()
-
-            # add management_page_charset as one fallback
+        # Deal with the fact that a REQUEST is not always available.
+        # In this case fall back to the encoding of the ZMI and the
+        # Python default encoding.
+        if request is None:
+            charsets = [default_encoding]
             management_charset = getattr(context, 'management_page_charset', None)
             if management_charset:
-                charsets.append(management_charset)
+                charsets.insert(0, management_charset)
+        else:
+            # charsets might by cached within the request            
+            charsets = getattr(request, '__zpt_available_charsets', None)
 
+        # No uncached charsets found: investigate the HTTP_ACCEPT_CHARSET
+        # header. This code is only called if 'context' has a request 
+        # object. The condition is true because otherwise 'charsets' contains
+        # at least the default encoding of Python.
+        if charsets is None:
+
+            charsets = list()
+
             # add Python's default encoding as last fallback
             charsets.append(default_encoding)               
 
+            # include the charsets based on the HTTP_ACCEPT_CHARSET
+            # header
+            charsets = IUserPreferredCharsets(request).getPreferredCharsets() +\
+                       charsets                    
+
             # cache list of charsets
             request.__zpt_available_charsets = charsets
 
         for enc in charsets:
-            if enc == '*': continue
+            if enc == '*': 
+                continue
 
             try:
                 return unicode(text, enc)



More information about the Zope-Checkins mailing list