diff options
author | Dan McGee <dan@archlinux.org> | 2010-07-28 15:38:33 -0500 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2010-07-28 15:38:33 -0500 |
commit | f9aa30e7bd4c65dce18e4dfe066099a02bdbe732 (patch) | |
tree | 437aa39ababe71a693f096fd06b88e922350ab88 /devel/management/commands | |
parent | 573357b79988837f6f2a8bc8d06887485b875c3c (diff) |
Clean up find_user() code a bitrelease_2010-07-28
With suggestions from Jason Chu, make the code a bit less repetitive with
regards to exception handling and fallthrough to the next method of finding
the user.
Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'devel/management/commands')
-rw-r--r-- | devel/management/commands/reporead.py | 47 |
1 files changed, 24 insertions, 23 deletions
diff --git a/devel/management/commands/reporead.py b/devel/management/commands/reporead.py index cd964ddb..777ebb93 100644 --- a/devel/management/commands/reporead.py +++ b/devel/management/commands/reporead.py @@ -142,33 +142,34 @@ def find_user(userstring): if userstring in find_user.cache: return find_user.cache[userstring] matches = re.match(r'^([^<]+)? ?<([^>]*)>', userstring) + if not matches: + return None + user = None - if matches and not user: - email = matches.group(2) + name = matches.group(1) + email = matches.group(2) + + def user_email(): + return User.objects.get(email=email) + def profile_email(): + return User.objects.get(userprofile_user__public_email=email) + 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. + name_q = Q() + for token in name.split(): + name_q &= (Q(first_name__icontains=token) | + Q(last_name__icontains=token)) + user = User.objects.get(name_q) + + for matcher in (user_email, profile_email, user_name): try: - user = User.objects.get(email=email) - except (User.DoesNotExist, User.MultipleObjectsReturned): - pass - if matches and not user: - email = matches.group(2) - try: - user = UserProfile.objects.get(public_email=email).user - except (UserProfile.DoesNotExist, UserProfile.MultipleObjectsReturned): - pass - if matches and not user: - name = matches.group(1) - try: - # yes, a bit odd but this is the easiest way to handle multiple - # bits in the first and last names 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. - name_q = Q() - for token in name.split(): - name_q &= (Q(first_name__icontains=token) | - Q(last_name__icontains=token)) - user = User.objects.get(name_q) + user = matcher() + break except (User.DoesNotExist, User.MultipleObjectsReturned): pass + find_user.cache[userstring] = user return user |