Permalink
Browse files

Adding in display name generator helper.

  • Loading branch information...
1 parent 534240f commit cd7b8f2c3c29a6b1a62d293cf0cd2f5cd209336c @etianen committed May 23, 2012
Showing with 22 additions and 0 deletions.
  1. +6 −0 src/usertools/helpers.py
  2. +16 −0 src/usertools/tests.py
@@ -0,0 +1,6 @@
+"""Helpers used by django-usertools."""
+
+
+def get_display_name(user, fallback=u"Anonymous"):
+ """Returns a display name for the user."""
+ return u" ".join(p for p in (user.first_name, user.last_name,) if p) or fallback
@@ -11,6 +11,22 @@
from django.utils.http import int_to_base36
from django.conf import settings
+from usertools.helpers import get_display_name
+
+
+class HelpersTest(TestCase):
+
+ def testDisplayName(self):
+ user = User(first_name="Foo", last_name="Bar")
+ self.assertEqual(get_display_name(user), "Foo Bar")
+ user = User(first_name="Foo")
+ self.assertEqual(get_display_name(user), "Foo")
+ user = User(last_name="Bar")
+ self.assertEqual(get_display_name(user), "Bar")
+ user = User()
+ self.assertEqual(get_display_name(user), "Anonymous")
+ self.assertEqual(get_display_name(user, fallback="Baz"), "Baz")
+
admin.autodiscover()

0 comments on commit cd7b8f2

Please sign in to comment.