Skip to content

Commit

Permalink
0.5.2: Improve UTF-8 handling in previewer.
Browse files Browse the repository at this point in the history
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
tkaemming committed Apr 1, 2013
1 parent 8bbe5e8 commit 266f2ce
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
16 changes: 14 additions & 2 deletions mailviews/previews.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {}
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions mailviews/templates/mailviews/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -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' %}" />

Expand Down
2 changes: 1 addition & 1 deletion mailviews/templates/mailviews/previews/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ <h3>HTML</h3>
<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">
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='[email protected]',
Expand Down

0 comments on commit 266f2ce

Please sign in to comment.