1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-02 07:47:32 +00:00

Merge pull request #1029 from haiwen/5.0-email2nickname

[seahub tags] Handle empty nickname in email2nickname
This commit is contained in:
Daniel Pan 2016-02-23 16:10:26 +08:00
commit eb0f749b82
2 changed files with 45 additions and 9 deletions

View File

@ -356,20 +356,24 @@ def translate_seahub_time_str(val):
@register.filter(name='email2nickname')
def email2nickname(value):
"""
Return nickname or short email.
Return nickname if it exists and it's not an empty string,
otherwise return short email.
"""
if not value:
return ''
key = normalize_cache_key(value, NICKNAME_CACHE_PREFIX)
nickname = cache.get(key)
if not nickname:
profile = get_first_object_or_none(Profile.objects.filter(user=value))
if profile is not None and profile.nickname:
nickname = profile.nickname
else:
nickname = value.split('@')[0]
cache.set(key, nickname, NICKNAME_CACHE_TIMEOUT)
cached_nickname = cache.get(key)
if cached_nickname and cached_nickname.strip():
return cached_nickname.strip()
profile = get_first_object_or_none(Profile.objects.filter(user=value))
if profile is not None and profile.nickname and profile.nickname.strip():
nickname = profile.nickname.strip()
else:
nickname = value.split('@')[0]
cache.set(key, nickname, NICKNAME_CACHE_TIMEOUT)
return nickname
@register.filter(name='email2id')

View File

@ -0,0 +1,32 @@
from seahub.test_utils import BaseTestCase
from seahub.base.templatetags.seahub_tags import email2nickname
from seahub.profile.models import Profile
class Email2nicknameTest(BaseTestCase):
def test_profile_is_none(self):
assert len(Profile.objects.all()) == 0
assert email2nickname(self.user.username) == self.user.username.split('@')[0]
def test_nickname_is_empty_string(self):
Profile.objects.add_or_update(self.user.username, '')
assert len(Profile.objects.all()) == 1
assert Profile.objects.all()[0].nickname == ''
assert email2nickname(self.user.username) == self.user.username.split('@')[0]
def test_nickname_is_space(self):
Profile.objects.add_or_update(self.user.username, ' ')
assert len(Profile.objects.all()) == 1
assert Profile.objects.all()[0].nickname == ' '
assert email2nickname(self.user.username) == self.user.username.split('@')[0]
def test_nickname_contains_space(self):
Profile.objects.add_or_update(self.user.username, ' foo bar ')
assert len(Profile.objects.all()) == 1
assert Profile.objects.all()[0].nickname == ' foo bar '
assert email2nickname(self.user.username) == 'foo bar'