diff --git a/seahub/base/templatetags/seahub_tags.py b/seahub/base/templatetags/seahub_tags.py index 825629e1e4..0a76de8174 100644 --- a/seahub/base/templatetags/seahub_tags.py +++ b/seahub/base/templatetags/seahub_tags.py @@ -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') diff --git a/tests/seahub/base/templatetags/test_seahub_tags.py b/tests/seahub/base/templatetags/test_seahub_tags.py new file mode 100644 index 0000000000..f78389b647 --- /dev/null +++ b/tests/seahub/base/templatetags/test_seahub_tags.py @@ -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'