summaryrefslogtreecommitdiff
path: root/devel/utils.py
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2011-06-23 19:35:47 -0500
committerDan McGee <dan@archlinux.org>2011-06-23 19:35:47 -0500
commit26c54d017185b1c409dbd6ed4c09fb14986df0b3 (patch)
tree8666aaa4a99542555f2e8a2d4c6abb9de9a15946 /devel/utils.py
parentda20949c8cc185e91dbaae1b8369fcffa3447081 (diff)
find_user: add tests and fix no email address case
If a packager string was passed in without an email address, we would blow up on the matcher and not try to find a user. Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'devel/utils.py')
-rw-r--r--devel/utils.py26
1 files changed, 18 insertions, 8 deletions
diff --git a/devel/utils.py b/devel/utils.py
index 9d7dfb2d..acdda959 100644
--- a/devel/utils.py
+++ b/devel/utils.py
@@ -51,24 +51,32 @@ def find_user(userstring):
We start by searching for a matching email address; we then move onto
matching by first/last name. If we cannot find a user, then return None.
'''
+ if not userstring:
+ return None
if userstring in find_user.cache:
return find_user.cache[userstring]
matches = re.match(r'^([^<]+)? ?<([^>]*)>', userstring)
if not matches:
- return None
-
- user = None
- name = matches.group(1)
- email = matches.group(2)
+ name = userstring
+ email = None
+ else:
+ name = matches.group(1)
+ email = matches.group(2)
def user_email():
- return User.objects.get(email=email)
+ if email:
+ return User.objects.get(email=email)
+ return None
def profile_email():
- return User.objects.get(userprofile__public_email=email)
+ if email:
+ return User.objects.get(userprofile__public_email=email)
+ return None
def user_name():
# yes, a bit odd but this is the easiest way since we can't always be
# sure how to split the name. Ensure every 'token' appears in at least
# one of the two name fields.
+ if not name:
+ return None
name_q = Q()
for token in name.split():
# ignore quoted parts; e.g. nicknames in strings
@@ -78,10 +86,12 @@ def find_user(userstring):
Q(last_name__icontains=token))
return User.objects.get(name_q)
+ user = None
for matcher in (user_email, profile_email, user_name):
try:
user = matcher()
- break
+ if user != None:
+ break
except (User.DoesNotExist, User.MultipleObjectsReturned):
pass