From 5faf18754e4e33db8f91994d4340b3fcaa786cc1 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Wed, 14 Nov 2012 17:32:34 -0500 Subject: [PATCH] Decrease code duplication in create_mirrored_message_users. (imported from commit d299124aa1eaa9afac62278c6b98208a14d2cdb1) --- zephyr/views.py | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/zephyr/views.py b/zephyr/views.py index 4591b6814f..aec63fe6ef 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -476,30 +476,23 @@ def extract_recipients(request): def create_mirrored_message_users(request, user_profile): if "sender" not in request.POST: return (False, None) + sender_email = request.POST["sender"].strip().lower() - - # First, check that the sender is in our realm: - if not same_realm_email(user_profile, sender_email): - return (False, None) - - pm_recipients = [] + referenced_users = set([sender_email]) if request.POST['type'] == 'private': - if "to" not in request.POST: - return (False, None) - pm_recipients = extract_recipients(request) + for email in extract_recipients(request): + referenced_users.add(email) - # Then, check that all private message recipients are in our realm: - for recipient in pm_recipients: - if not same_realm_email(user_profile, recipient): + # Check that all referenced users are in our realm: + for email in referenced_users: + if not same_realm_email(user_profile, email): return (False, None) - # Create a user for the sender, if needed - sender = create_mit_user_if_needed(user_profile.realm, sender_email) - - # Create users for private message recipients, if needed. - for email in pm_recipients: + # Create users for the referenced users, if needed. + for email in referenced_users: create_mit_user_if_needed(user_profile.realm, email) + sender = UserProfile.objects.get(user__email=sender_email) return (True, sender) # We do not @require_login for send_message_backend, since it is used