From 266f2ceaf030a6201a6c055453e23978ff4e6d94 Mon Sep 17 00:00:00 2001 From: Ted Kaemming Date: Mon, 1 Apr 2013 11:37:23 -0700 Subject: [PATCH] 0.5.2: Improve UTF-8 handling in previewer. This improves the handling of non-ASCII encoded headers and message bodies within the message previewer. Thanks to @yunmanger1 and Sebastian Vetter (@elbaschid) for the initial patches. Closes #5, #6. --- mailviews/previews.py | 16 ++++++++++++++-- mailviews/templates/mailviews/base.html | 2 ++ .../templates/mailviews/previews/detail.html | 2 +- setup.py | 2 +- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/mailviews/previews.py b/mailviews/previews.py index 873e0a4..b9ccf7c 100644 --- a/mailviews/previews.py +++ b/mailviews/previews.py @@ -2,6 +2,7 @@ import os from base64 import b64encode from collections import namedtuple +from email.header import decode_header from django.conf.urls.defaults import include, patterns, url from django.core.urlresolvers import reverse @@ -23,6 +24,17 @@ ModulePreviews = namedtuple('ModulePreviews', ('module', 'previews')) +def maybe_decode_header(header): + """ + Decodes an encoded 7-bit ASCII header value into it's actual value. + """ + value, encoding = decode_header(header)[0] + if encoding: + return value.decode(encoding) + else: + return value + + class PreviewSite(object): def __init__(self): self.__previews = {} @@ -162,7 +174,7 @@ def detail_view(self, request): message = message_view.render_to_message() raw = message.message() - headers = SortedDict((header, raw[header]) for header in self.headers) + headers = SortedDict((header, maybe_decode_header(raw[header])) for header in self.headers) context.update({ 'message': message, @@ -178,7 +190,7 @@ def detail_view(self, request): if alternative[1] == 'text/html') context.update({ 'html': html, - 'escaped_html': b64encode(html), + 'escaped_html': b64encode(html.encode('utf-8')), }) except StopIteration: pass diff --git a/mailviews/templates/mailviews/base.html b/mailviews/templates/mailviews/base.html index d5d5225..7b749eb 100644 --- a/mailviews/templates/mailviews/base.html +++ b/mailviews/templates/mailviews/base.html @@ -4,6 +4,8 @@ {% load mailviews %} + + diff --git a/mailviews/templates/mailviews/previews/detail.html b/mailviews/templates/mailviews/previews/detail.html index 643c6d6..fc18f0b 100644 --- a/mailviews/templates/mailviews/previews/detail.html +++ b/mailviews/templates/mailviews/previews/detail.html @@ -95,7 +95,7 @@

HTML

- +
diff --git a/setup.py b/setup.py index 0ebbb68..c61cf58 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ install_requires = ['django'] setup(name='django-mailviews', - version='0.5.1', + version='0.5.2', url='http://github.com/disqus/django-mailviews/', author='ted kaemming', author_email='ted@disqus.com',