Commit a717c7be authored by Andreas Jung's avatar Andreas Jung

deal with the case that 'context' must not have a REQUEST object

(test comes later)
parent 33343447
...@@ -62,25 +62,42 @@ class PreferredCharsetResolver: ...@@ -62,25 +62,42 @@ class PreferredCharsetResolver:
def resolve(self, context, text, expression): def resolve(self, context, text, expression):
request = context.REQUEST request = getattr(context, 'REQUEST', None)
charsets = getattr(request, '__zpt_available_charsets', None) # Deal with the fact that a REQUEST is not always available.
if charsets is None: # In this case fall back to the encoding of the ZMI and the
charsets = IUserPreferredCharsets(request).getPreferredCharsets() # Python default encoding.
if request is None:
# add management_page_charset as one fallback charsets = [default_encoding]
management_charset = getattr(context, 'management_page_charset', None) management_charset = getattr(context, 'management_page_charset', None)
if management_charset: 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 # add Python's default encoding as last fallback
charsets.append(default_encoding) charsets.append(default_encoding)
# include the charsets based on the HTTP_ACCEPT_CHARSET
# header
charsets = IUserPreferredCharsets(request).getPreferredCharsets() +\
charsets
# cache list of charsets # cache list of charsets
request.__zpt_available_charsets = charsets request.__zpt_available_charsets = charsets
for enc in charsets: for enc in charsets:
if enc == '*': continue if enc == '*':
continue
try: try:
return unicode(text, enc) return unicode(text, enc)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment