mirror of https://github.com/zulip/zulip.git
Refactor json_invite_users into do_invite_users
(imported from commit 053274a1fcfbb93fba27d136b246b65b3491f549)
This commit is contained in:
parent
ffa8541818
commit
bd837936ce
|
@ -9,13 +9,6 @@ from humbug import settings
|
||||||
from zephyr.models import Realm, get_user_profile_by_email, UserProfile
|
from zephyr.models import Realm, get_user_profile_by_email, UserProfile
|
||||||
from zephyr.lib.actions import do_change_password
|
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):
|
def is_inactive(value):
|
||||||
try:
|
try:
|
||||||
if get_user_profile_by_email(value).is_active:
|
if get_user_profile_by_email(value).is_active:
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.core import validators
|
||||||
from django.contrib.sessions.models import Session
|
from django.contrib.sessions.models import Session
|
||||||
from zephyr.lib.context_managers import lockfile
|
from zephyr.lib.context_managers import lockfile
|
||||||
from zephyr.models import Realm, Stream, UserProfile, UserActivity, \
|
from zephyr.models import Realm, Stream, UserProfile, UserActivity, \
|
||||||
|
@ -1304,3 +1305,80 @@ def handle_missedmessage_emails(user_profile_id, missed_email_events):
|
||||||
return
|
return
|
||||||
|
|
||||||
do_send_missedmessage_email(user_profile, messages)
|
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
|
||||||
|
|
|
@ -31,9 +31,10 @@ from zephyr.lib.actions import do_remove_subscription, bulk_remove_subscriptions
|
||||||
recipient_for_emails, extract_recipients, do_events_register, \
|
recipient_for_emails, extract_recipients, do_events_register, \
|
||||||
get_status_dict, do_change_enable_offline_email_notifications, \
|
get_status_dict, do_change_enable_offline_email_notifications, \
|
||||||
do_update_onboarding_steps, do_update_message, internal_prep_message, \
|
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, \
|
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.views.decorators.csrf import csrf_exempt, csrf_protect
|
||||||
from django_openid_auth.views import default_render_failure, login_complete
|
from django_openid_auth.views import default_render_failure, login_complete
|
||||||
from openid.consumer.consumer import SUCCESS as openid_SUCCESS
|
from openid.consumer.consumer import SUCCESS as openid_SUCCESS
|
||||||
|
@ -247,7 +248,7 @@ def accounts_register(request):
|
||||||
is_inactive(email)
|
is_inactive(email)
|
||||||
else:
|
else:
|
||||||
# Other users should not already exist at all.
|
# Other users should not already exist at all.
|
||||||
is_unique(email)
|
user_email_is_unique(email)
|
||||||
except ValidationError:
|
except ValidationError:
|
||||||
return HttpResponseRedirect(reverse('django.contrib.auth.views.login') + '?email=' + urllib.quote_plus(email))
|
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)
|
return json_error("Stream does not exist: %s. No invites were sent." % stream_name)
|
||||||
streams.append(stream)
|
streams.append(stream)
|
||||||
|
|
||||||
new_prereg_users = []
|
ret_error, error_data = do_invite_users(user_profile, invitee_emails, streams)
|
||||||
errors = []
|
|
||||||
skipped = []
|
|
||||||
for email in invitee_emails:
|
|
||||||
if email == '':
|
|
||||||
continue
|
|
||||||
|
|
||||||
if not validators.email_re.match(email):
|
if ret_error is not None:
|
||||||
errors.append((email, "Invalid address."))
|
return json_error(data=error_data, msg=ret_error)
|
||||||
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!")
|
|
||||||
else:
|
else:
|
||||||
return json_success()
|
return json_success()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue