diff --git a/apps/addons/templates/addons/persona_detail_table.html b/apps/addons/templates/addons/persona_detail_table.html index da1f0048bd..f542b689da 100644 --- a/apps/addons/templates/addons/persona_detail_table.html +++ b/apps/addons/templates/addons/persona_detail_table.html @@ -8,7 +8,7 @@ {{ _('Artist') }} - {{ users_list(addon.listed_authors) or + {{ users_list(addon.listed_authors, max_text_length=20) or persona.display_username }} diff --git a/apps/users/helpers.py b/apps/users/helpers.py index 5a23b7aea0..c98f3dd931 100644 --- a/apps/users/helpers.py +++ b/apps/users/helpers.py @@ -1,6 +1,5 @@ import random -from django.conf import settings from django.utils.encoding import smart_unicode import jinja2 @@ -42,7 +41,7 @@ def user_link(user): @register.function -def users_list(users, size=None): +def users_list(users, size=None, max_text_length=None): if not users: return '' @@ -51,7 +50,12 @@ def users_list(users, size=None): users = users[:size] 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') @@ -62,11 +66,17 @@ def addon_users_list(context, addon): return ctx -def _user_link(user): +def _user_link(user, max_text_length=None): if isinstance(user, basestring): return user - return u'%s' % ( - 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'%s' % ( + user.get_url_path(), user.name, + jinja2.escape(smart_unicode(username))) @register.filter diff --git a/apps/users/tests/test_helpers.py b/apps/users/tests/test_helpers.py index fb0ad18c86..4339a7471f 100644 --- a/apps/users/tests/test_helpers.py +++ b/apps/users/tests/test_helpers.py @@ -40,7 +40,9 @@ def test_emaillink(): def test_user_link(): u = UserProfile(username='jconnor', display_name='John Connor', pk=1) - eq_(user_link(u), 'John Connor' % u.get_url_path()) + eq_(user_link(u), + 'John Connor' % (u.get_url_path(), + u.name)) # handle None gracefully eq_(user_link(None), '') @@ -50,7 +52,8 @@ def test_user_link_xss(): u = UserProfile(username='jconnor', display_name='', pk=1) html = "<script>alert(1)</script>" - eq_(user_link(u), '%s' % (u.get_url_path(), html)) + eq_(user_link(u), '%s' % (u.get_url_path(), + u.name, html)) def test_users_list(): @@ -72,14 +75,26 @@ def test_short_users_list(): 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'Some Very...' % (u.get_url_path(), + u.name)) + + def test_user_link_unicode(): """make sure helper won't choke on unicode input""" u = UserProfile(username=u'jmüller', display_name=u'Jürgen Müller', pk=1) - eq_(user_link(u), u'Jürgen Müller' % u.get_url_path()) + eq_(user_link(u), + u'Jürgen Müller' % (u.get_url_path(), + u.name)) u = UserProfile(username='\xe5\xaf\x92\xe6\x98\x9f', pk=1) eq_(user_link(u), - u'%s' % (u.get_url_path(), u.username)) + u'%s' % (u.get_url_path(), u.name, + u.username)) class TestAddonUsersList(TestPersonas, amo.tests.TestCase):