Fixup! refactor email_exists, and handle many more cases

This commit is contained in:
Troy Sankey
2018-05-16 16:41:10 -04:00
parent 232b359258
commit a7ecfe1cd3
12 changed files with 166 additions and 39 deletions

View File

@@ -19,8 +19,7 @@ from django.core.validators import RegexValidator, slug_re
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
from openedx.core.djangoapps.user_api import accounts as accounts_settings
from openedx.core.djangoapps.user_api.accounts.utils import email_exists
from student.models import CourseEnrollmentAllowed
from student.models import CourseEnrollmentAllowed, email_exists_or_retired
from util.password_policy_validators import password_max_length, password_min_length, validate_password
@@ -295,7 +294,7 @@ class AccountCreationForm(forms.Form):
# reject the registration.
if not CourseEnrollmentAllowed.objects.filter(email=email).exists():
raise ValidationError(_("Unauthorized email address."))
if email_exists(email):
if email_exists_or_retired(email):
raise ValidationError(
_(
"It looks like {email} belongs to an existing account. Try again with a different email address."

View File

@@ -48,7 +48,8 @@ from student.models import (
Registration,
UserAttribute,
UserProfile,
unique_id_for_user
unique_id_for_user,
email_exists_or_retired
)
@@ -652,7 +653,7 @@ def do_create_account(form, custom_form=None):
USERNAME_EXISTS_MSG_FMT.format(username=proposed_username),
field="username"
)
elif User.objects.filter(email=user.email):
elif email_exists_or_retired(user.email):
raise AccountValidationError(
_("An account with the Email '{email}' already exists.").format(email=user.email),
field="email"

View File

@@ -228,6 +228,13 @@ def is_email_retired(email):
return User.objects.filter(email__in=list(locally_hashed_emails)).exists()
def email_exists_or_retired(email):
"""
Check an email against the User model for existence.
"""
return User.objects.filter(email=email).exists() or is_email_retired(email)
def get_retired_username_by_username(username):
"""
If a UserRetirementStatus object with an original_username matching the given username exists,
@@ -2246,18 +2253,30 @@ class CourseAccessRole(models.Model):
#### Helper methods for use from python manage.py shell and other classes.
def strip_if_string(value):
if isinstance(value, six.string_types):
return value.strip()
return value
def get_user_by_username_or_email(username_or_email):
"""
Return a User object, looking up by email if username_or_email contains a
'@', otherwise by username.
Raises:
User.DoesNotExist is lookup fails.
User.DoesNotExist if no user object can be found, the user was
retired, or the user is in the process of being retired.
"""
username_or_email = strip_if_string(username_or_email)
if '@' in username_or_email:
return User.objects.get(email=username_or_email)
user = User.objects.get(email=username_or_email)
else:
return User.objects.get(username=username_or_email)
user = User.objects.get(username=username_or_email)
UserRetirementRequest = apps.get_model('user_api', 'UserRetirementRequest')
if UserRetirementRequest.has_user_requested_retirement(user):
raise User.DoesNotExist
return user
def get_user(email):

View File

@@ -95,7 +95,7 @@ from student.models import (
UserSignupSource,
UserStanding,
create_comments_service_user,
username_or_email_exists_or_retired,
email_exists_or_retired,
)
from student.signals import REFUND_ORDER
from student.tasks import send_activation_email
@@ -1252,7 +1252,7 @@ def validate_new_email(user, new_email):
if new_email == user.email:
raise ValueError(_('Old email is the same as the new email.'))
if username_or_email_exists_or_retired(new_email):
if email_exists_or_retired(new_email):
raise ValueError(_('An account with this e-mail already exists.'))