Skip to content
Browse files

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.
  • Loading branch information...
1 parent 8bbe5e8 commit 266f2ceaf030a6201a6c055453e23978ff4e6d94 @tkaemming tkaemming committed Apr 1, 2013
View
16 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
View
2 mailviews/templates/mailviews/base.html
@@ -4,6 +4,8 @@
{% load mailviews %}
+ <meta charset="utf-8" />
+
<link rel="stylesheet" type="text/css" href="{% mailviews_static 'mailviews/css/bootstrap.css' %}" />
<link rel="stylesheet" type="text/css" href="{% mailviews_static 'mailviews/css/mailviews.css' %}" />
View
2 mailviews/templates/mailviews/previews/detail.html
@@ -95,7 +95,7 @@
<div class="tab-content">
<section id="body-html" class="tab-pane active">
- <iframe src="data:text/html;base64,{{ escaped_html }}" frameborder="0" allowtransparency="true"></iframe>
+ <iframe src="data:text/html;charset=utf-8;base64,{{ escaped_html }}" frameborder="0" allowtransparency="true"></iframe>
</section>
<section id="html-raw" class="tab-pane">
View
2 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',

0 comments on commit 266f2ce

Please sign in to comment.
Something went wrong with that request. Please try again.