Changeset - 9ecd1d4836cc
[Not reviewed]
default
0 2 0
Mads Kiilerich - 11 years ago 2015-03-11 00:24:25
madski@unity3d.com
helpers: introduce user_or_none helper helper

Slight cleanup refactoring - will be useful later.
2 files changed with 25 insertions and 30 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/helpers.py
Show inline comments
 
@@ -494,29 +494,34 @@ def is_hg(repository):
 
    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 @@ def person(author, show_attr="username")
 
    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"):
kallithea/model/user.py
Show inline comments
 
@@ -347,11 +347,11 @@ class UserModel(BaseModel):
 
            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:
0 comments (0 inline, 0 general)