Merge branch 'master' of github.com:mozilla/olympia

This commit is contained in:
verbatim updates user 2014-03-24 09:55:13 +00:00
Родитель a00d9de387 280075ec0f
Коммит 10914bae03
3 изменённых файлов: 36 добавлений и 11 удалений

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

@ -8,7 +8,7 @@
</tr> </tr>
<tr class="artist"> <tr class="artist">
<th>{{ _('Artist') }}</th> <th>{{ _('Artist') }}</th>
<td>{{ users_list(addon.listed_authors) or <td>{{ users_list(addon.listed_authors, max_text_length=20) or
persona.display_username }}</td> persona.display_username }}</td>
</tr> </tr>
<tr> <tr>

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

@ -1,6 +1,5 @@
import random import random
from django.conf import settings
from django.utils.encoding import smart_unicode from django.utils.encoding import smart_unicode
import jinja2 import jinja2
@ -42,7 +41,7 @@ def user_link(user):
@register.function @register.function
def users_list(users, size=None): def users_list(users, size=None, max_text_length=None):
if not users: if not users:
return '' return ''
@ -51,7 +50,12 @@ def users_list(users, size=None):
users = users[:size] users = users[:size]
tail = [_('others', 'user_list_others')] tail = [_('others', 'user_list_others')]
return jinja2.Markup(', '.join(map(_user_link, users) + tail)) if max_text_length:
user_list = [_user_link(user, max_text_length) for user in users]
else:
user_list = map(_user_link, users)
return jinja2.Markup(', '.join(user_list + tail))
@register.inclusion_tag('users/helpers/addon_users_list.html') @register.inclusion_tag('users/helpers/addon_users_list.html')
@ -62,11 +66,17 @@ def addon_users_list(context, addon):
return ctx return ctx
def _user_link(user): def _user_link(user, max_text_length=None):
if isinstance(user, basestring): if isinstance(user, basestring):
return user return user
return u'<a href="%s">%s</a>' % (
user.get_url_path(), jinja2.escape(smart_unicode(user.name))) username = user.name
if max_text_length and len(user.name) > max_text_length:
username = user.name[:max_text_length].strip() + '...'
return u'<a href="%s" title="%s">%s</a>' % (
user.get_url_path(), user.name,
jinja2.escape(smart_unicode(username)))
@register.filter @register.filter

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

@ -40,7 +40,9 @@ def test_emaillink():
def test_user_link(): def test_user_link():
u = UserProfile(username='jconnor', display_name='John Connor', pk=1) u = UserProfile(username='jconnor', display_name='John Connor', pk=1)
eq_(user_link(u), '<a href="%s">John Connor</a>' % u.get_url_path()) eq_(user_link(u),
'<a href="%s" title="%s">John Connor</a>' % (u.get_url_path(),
u.name))
# handle None gracefully # handle None gracefully
eq_(user_link(None), '') eq_(user_link(None), '')
@ -50,7 +52,8 @@ def test_user_link_xss():
u = UserProfile(username='jconnor', u = UserProfile(username='jconnor',
display_name='<script>alert(1)</script>', pk=1) display_name='<script>alert(1)</script>', pk=1)
html = "&lt;script&gt;alert(1)&lt;/script&gt;" html = "&lt;script&gt;alert(1)&lt;/script&gt;"
eq_(user_link(u), '<a href="%s">%s</a>' % (u.get_url_path(), html)) eq_(user_link(u), '<a href="%s" title="%s">%s</a>' % (u.get_url_path(),
u.name, html))
def test_users_list(): def test_users_list():
@ -72,14 +75,26 @@ def test_short_users_list():
eq_(shortlist, ', '.join((user_link(u1), user_link(u2))) + ', others') eq_(shortlist, ', '.join((user_link(u1), user_link(u2))) + ', others')
def test_users_list_truncate_display_name():
u = UserProfile(username='oscar',
display_name='Some Very Long Display Name', pk=1)
truncated_list = users_list([u], None, 10)
eq_(truncated_list,
u'<a href="%s" title="%s">Some Very...</a>' % (u.get_url_path(),
u.name))
def test_user_link_unicode(): def test_user_link_unicode():
"""make sure helper won't choke on unicode input""" """make sure helper won't choke on unicode input"""
u = UserProfile(username=u'jmüller', display_name=u'Jürgen Müller', pk=1) u = UserProfile(username=u'jmüller', display_name=u'Jürgen Müller', pk=1)
eq_(user_link(u), u'<a href="%s">Jürgen Müller</a>' % u.get_url_path()) eq_(user_link(u),
u'<a href="%s" title="%s">Jürgen Müller</a>' % (u.get_url_path(),
u.name))
u = UserProfile(username='\xe5\xaf\x92\xe6\x98\x9f', pk=1) u = UserProfile(username='\xe5\xaf\x92\xe6\x98\x9f', pk=1)
eq_(user_link(u), eq_(user_link(u),
u'<a href="%s">%s</a>' % (u.get_url_path(), u.username)) u'<a href="%s" title="%s">%s</a>' % (u.get_url_path(), u.name,
u.username))
class TestAddonUsersList(TestPersonas, amo.tests.TestCase): class TestAddonUsersList(TestPersonas, amo.tests.TestCase):