From bd837936cec835651229010422cea9210d718027 Mon Sep 17 00:00:00 2001 From: Leo Franchi Date: Mon, 8 Jul 2013 11:57:04 -0400 Subject: [PATCH] Refactor json_invite_users into do_invite_users (imported from commit 053274a1fcfbb93fba27d136b246b65b3491f549) --- zephyr/forms.py | 7 ---- zephyr/lib/actions.py | 78 +++++++++++++++++++++++++++++++++++++++++++ zephyr/views.py | 71 ++++----------------------------------- 3 files changed, 85 insertions(+), 71 deletions(-) diff --git a/zephyr/forms.py b/zephyr/forms.py index 7f860b66fc..00bdc58e14 100644 --- a/zephyr/forms.py +++ b/zephyr/forms.py @@ -9,13 +9,6 @@ from humbug import settings from zephyr.models import Realm, get_user_profile_by_email, UserProfile from zephyr.lib.actions import do_change_password -def is_unique(value): - try: - get_user_profile_by_email(value) - raise ValidationError(u'%s is already registered' % value) - except UserProfile.DoesNotExist: - pass - def is_inactive(value): try: if get_user_profile_by_email(value).is_active: diff --git a/zephyr/lib/actions.py b/zephyr/lib/actions.py index 5f7b4f5e0c..9d2d09b653 100644 --- a/zephyr/lib/actions.py +++ b/zephyr/lib/actions.py @@ -1,6 +1,7 @@ from __future__ import absolute_import from django.conf import settings +from django.core import validators from django.contrib.sessions.models import Session from zephyr.lib.context_managers import lockfile from zephyr.models import Realm, Stream, UserProfile, UserActivity, \ @@ -1304,3 +1305,80 @@ def handle_missedmessage_emails(user_profile_id, missed_email_events): return do_send_missedmessage_email(user_profile, messages) + + +def user_email_is_unique(value): + try: + get_user_profile_by_email(value) + raise ValidationError(u'%s is already registered' % value) + except UserProfile.DoesNotExist: + pass + +def do_invite_users(user_profile, invitee_emails, streams): + new_prereg_users = [] + errors = [] + skipped = [] + + ret_error = None + ret_error_data = {} + + for email in invitee_emails: + if email == '': + continue + + if not validators.email_re.match(email): + errors.append((email, "Invalid address.")) + continue + + if user_profile.realm.restricted_to_domain and \ + email.split('@', 1)[-1].lower() != user_profile.realm.domain.lower(): + errors.append((email, "Outside your domain.")) + continue + + # Redundant check in case earlier validation preventing MIT users from + # inviting people fails. + if settings.ALLOW_REGISTER == False: + if "@mit.edu" in email: + errors.append((email, "Invitations are not enabled for MIT at this time.")) + continue + + try: + user_email_is_unique(email) + except ValidationError: + skipped.append((email, "Already has an account.")) + continue + + # The logged in user is the referrer. + prereg_user = PreregistrationUser(email=email, referred_by=user_profile) + + # We save twice because you cannot associate a ManyToMany field + # on an unsaved object. + prereg_user.save() + prereg_user.streams = streams + prereg_user.save() + + new_prereg_users.append(prereg_user) + + if errors: + ret_error = "Some emails did not validate, so we didn't send any invitations." + ret_error_data = {'errors': errors} + + if skipped and len(skipped) == len(invitee_emails): + # All e-mails were skipped, so we didn't actually invite anyone. + ret_error = "We weren't able to invite anyone." + ret_error_data = {'errors': skipped} + return ret_error, ret_error_data + + # If we encounter an exception at any point before now, there are no unwanted side-effects, + # since it is totally fine to have duplicate PreregistrationUsers + for user in new_prereg_users: + event = {"email": user.email, "referrer_email": user_profile.email} + queue_json_publish("invites", event, + lambda event: do_send_confirmation_email(user, user_profile)) + + if skipped: + ret_error = "Some of those addresses are already using Humbug, \ +so we didn't send them an invitation. We did send invitations to everyone else!" + ret_error_data = {'errors': skipped} + + return ret_error, ret_error_data diff --git a/zephyr/views.py b/zephyr/views.py index e17b2bf78b..05d307666d 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -31,9 +31,10 @@ from zephyr.lib.actions import do_remove_subscription, bulk_remove_subscriptions recipient_for_emails, extract_recipients, do_events_register, \ get_status_dict, do_change_enable_offline_email_notifications, \ do_update_onboarding_steps, do_update_message, internal_prep_message, \ - do_send_messages, do_add_subscription, get_default_subs, do_deactivate + do_send_messages, do_add_subscription, get_default_subs, do_deactivate, \ + user_email_is_unique, do_invite_users from zephyr.forms import RegistrationForm, HomepageForm, ToSForm, CreateBotForm, \ - is_unique, is_inactive, isnt_mit + is_inactive, isnt_mit from django.views.decorators.csrf import csrf_exempt, csrf_protect from django_openid_auth.views import default_render_failure, login_complete from openid.consumer.consumer import SUCCESS as openid_SUCCESS @@ -247,7 +248,7 @@ def accounts_register(request): is_inactive(email) else: # Other users should not already exist at all. - is_unique(email) + user_email_is_unique(email) except ValidationError: return HttpResponseRedirect(reverse('django.contrib.auth.views.login') + '?email=' + urllib.quote_plus(email)) @@ -385,68 +386,10 @@ def json_invite_users(request, user_profile, invitee_emails=REQ): return json_error("Stream does not exist: %s. No invites were sent." % stream_name) streams.append(stream) - new_prereg_users = [] - errors = [] - skipped = [] - for email in invitee_emails: - if email == '': - continue + ret_error, error_data = do_invite_users(user_profile, invitee_emails, streams) - if not validators.email_re.match(email): - errors.append((email, "Invalid address.")) - continue - - if user_profile.realm.restricted_to_domain and \ - email.split('@', 1)[-1].lower() != user_profile.realm.domain.lower(): - errors.append((email, "Outside your domain.")) - continue - - # Redundant check in case earlier validation preventing MIT users from - # inviting people fails. - if settings.ALLOW_REGISTER == False: - try: - isnt_mit(email) - except ValidationError: - errors.append((email, "Invitations are not enabled for MIT at this time.")) - continue - - try: - is_unique(email) - except ValidationError: - skipped.append((email, "Already has an account.")) - continue - - # The logged in user is the referrer. - prereg_user = PreregistrationUser(email=email, referred_by=user_profile) - - # We save twice because you cannot associate a ManyToMany field - # on an unsaved object. - prereg_user.save() - prereg_user.streams = streams - prereg_user.save() - - new_prereg_users.append(prereg_user) - - if errors: - return json_error(data={'errors': errors}, - msg="Some emails did not validate, so we didn't send any invitations.") - - if skipped and len(skipped) == len(invitee_emails): - # All e-mails were skipped, so we didn't actually invite anyone. - return json_error(data={'errors': skipped}, - msg="We weren't able to invite anyone.") - - # If we encounter an exception at any point before now, there are no unwanted side-effects, - # since it is totally fine to have duplicate PreregistrationUsers - for user in new_prereg_users: - event = {"email": user.email, "referrer_email": user_profile.email} - queue_json_publish("invites", event, - lambda event: do_send_confirmation_email(user, user_profile)) - - if skipped: - return json_error(data={'errors': skipped}, - msg="Some of those addresses are already using Humbug, \ -so we didn't send them an invitation. We did send invitations to everyone else!") + if ret_error is not None: + return json_error(data=error_data, msg=ret_error) else: return json_success()