From f9aa30e7bd4c65dce18e4dfe066099a02bdbe732 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Wed, 28 Jul 2010 15:38:33 -0500 Subject: Clean up find_user() code a bit 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 --- devel/management/commands/reporead.py | 47 ++++++++++++++++++----------------- 1 file 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 -- cgit v1.2.3-54-g00ecf