Non ascii slugs are now displayed right on Step 7 of submission (bug 621568)

This commit is contained in:
Kumar McMillan 2010-12-30 16:05:56 -06:00
Родитель 8741056401
Коммит 47a73a73b9
4 изменённых файлов: 54 добавлений и 1 удалений

Просмотреть файл

@ -1,5 +1,7 @@
from collections import defaultdict
import urllib
import chardet
import jinja2
from jingo import register
from tower import ugettext as _, ungettext as ngettext
@ -113,3 +115,18 @@ def summarize_validation(validation):
warnings = ngettext('{0} warning', '{0} warnings',
validation.warnings).format(validation.warnings)
return "%s, %s" % (errors, warnings)
@register.filter
def display_url(url):
"""Display a URL like the browser URL bar would.
Note: returns a Unicode object, not a valid URL.
"""
if isinstance(url, unicode):
# Byte sequences will be url encoded so convert
# to bytes here just to stop auto decoding.
url = url.encode('utf8')
bytes = urllib.unquote(url)
c = chardet.detect(bytes)
return bytes.decode(c['encoding'], 'replace')

Просмотреть файл

@ -26,7 +26,7 @@
</p>
<p>
<a id="submitted-addon-url" href="{{ addon.get_url_path() }}">
{{ addon.get_url_path()|absolutify }}</a>
{{ addon.get_url_path()|absolutify|display_url }}</a>
</p>
<div class="done-next-steps">
<p><strong>{{ _('Next steps:') }}</strong></p>

Просмотреть файл

@ -1,4 +1,6 @@
# -*- coding: utf8 -*-
import unittest
import urllib
from django.utils import translation
@ -117,3 +119,25 @@ def test_summarize_validation():
eq_(render('{{ summarize_validation(validation) }}',
{'validation': v}),
u'2 errors, 2 warnings')
class TestDisplayUrl(unittest.TestCase):
def setUp(self):
self.raw_url = u'http://host/%s' % 'フォクすけといっしょ'.decode('utf8')
def test_utf8(self):
url = urllib.quote(self.raw_url.encode('utf8'))
eq_(render('{{ url|display_url }}', {'url':url}),
self.raw_url)
def test_unicode(self):
url = urllib.quote(self.raw_url.encode('utf8'))
url = unicode(url, 'utf8')
eq_(render('{{ url|display_url }}', {'url':url}),
self.raw_url)
def test_euc_jp(self):
url = urllib.quote(self.raw_url.encode('euc_jp'))
eq_(render('{{ url|display_url }}', {'url':url}),
self.raw_url)

Просмотреть файл

@ -1,3 +1,4 @@
# -*- coding: utf8 -*-
import json
import os
import re
@ -2840,6 +2841,17 @@ class TestSubmitStep7(TestSubmitBase):
follow=True)
self.assertRedirects(r, reverse('devhub.versions', args=['a3615']))
def test_display_non_ascii_url(self):
addon = Addon.objects.get(pk=3615)
addon.update(slug='フォクすけといっしょ')
r = self.client.get(reverse('devhub.submit.7', args=['フォクすけといっしょ']))
eq_(r.status_code, 200)
# The meta charset will always be utf-8.
doc = pq(r.content.decode('utf-8'))
eq_(doc('#submitted-addon-url').text(),
u'%s/en-US/firefox/addon/%s/' % (settings.SITE_URL,
'フォクすけといっしょ'.decode('utf8')))
class TestResumeStep(TestSubmitBase):