changeset 4900:9ecd1d4836cc

helpers: introduce user_or_none helper helper Slight cleanup refactoring - will be useful later.
author Mads Kiilerich <madski@unity3d.com>
date Wed, 11 Mar 2015 00:24:25 +0100
parents 9ee018948dcd
children 409eaadc0054
files kallithea/lib/helpers.py kallithea/model/user.py
diffstat 2 files changed, 25 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/helpers.py	Wed Mar 11 00:24:25 2015 +0100
+++ b/kallithea/lib/helpers.py	Wed Mar 11 00:24:25 2015 +0100
@@ -494,29 +494,34 @@
     return _type == 'hg'
 
 
+def user_or_none(author):
+    email = author_email(author)
+    if email is not None:
+        user = User.get_by_email(email, case_insensitive=True, cache=True)
+        if user is not None:
+            return user
+
+    user = User.get_by_username(author_name(author), case_insensitive=True, cache=True)
+    if user is not None:
+        return user
+
+    return None
+
 def email_or_none(author):
     if not author:
         return None
+    user = user_or_none(author)
+    if user is not None:
+        return user.email # always use main email address - not necessarily the one used to find user
+
     # extract email from the commit string
-    _email = email(author)
-    if _email:
-        # check it against Kallithea database, and use the MAIN email for this
-        # user
-        user = User.get_by_email(_email, case_insensitive=True, cache=True)
-        if user is not None:
-            return user.email
-        return _email
-
-    # See if it contains a username we can get an email from
-    user = User.get_by_username(author_name(author), case_insensitive=True,
-                                cache=True)
-    if user is not None:
-        return user.email
+    email = author_email(author)
+    if email:
+        return email
 
     # No valid email, not a valid user in the system, none!
     return None
 
-
 def person(author, show_attr="username"):
     """Find the user identified by 'author', return one of the users attributes,
     default to the username attribute, None if there is no user"""
@@ -527,22 +532,12 @@
     if isinstance(author, User):
         return person_getter(author)
 
-    # Valid email in the attribute passed, see if they're in the system
-    _email = email(author)
-    if _email:
-        user = User.get_by_email(_email, case_insensitive=True, cache=True)
-        if user is not None:
-            return person_getter(user)
-
-    # Maybe it's a username?
-    _author = author_name(author)
-    user = User.get_by_username(_author, case_insensitive=True,
-                                cache=True)
+    user = user_or_none(author)
     if user is not None:
         return person_getter(user)
 
     # Still nothing?  Just pass back the author name if any, else the email
-    return _author or _email
+    return author_name(author) or email(author)
 
 
 def person_by_id(id_, show_attr="username"):
--- a/kallithea/model/user.py	Wed Mar 11 00:24:25 2015 +0100
+++ b/kallithea/model/user.py	Wed Mar 11 00:24:25 2015 +0100
@@ -347,11 +347,11 @@
             raise Exception('You need to pass user_id, api_key or username')
 
         dbuser = None
-        if user_id:
+        if user_id is not None:
             dbuser = self.get(user_id)
-        elif api_key:
+        elif api_key is not None:
             dbuser = self.get_by_api_key(api_key)
-        elif username:
+        elif username is not None:
             dbuser = self.get_by_username(username)
 
         if dbuser is not None and dbuser.active: