Skip to content

Commit

Permalink
fix(account): Send email confirmation vs no USER_MODEL_EMAIL_FIELD
Browse files Browse the repository at this point in the history
  • Loading branch information
pennersr committed Nov 5, 2023
1 parent a86cdaf commit 6790542
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
3 changes: 2 additions & 1 deletion allauth/account/tests/test_signup.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def test_get_initial_with_valid_email():
assert view.context_data["view"].get_initial()["email"] == "[email protected]"


def test_signup_user_model_no_email(settings, client, password_factory, db):
def test_signup_user_model_no_email(settings, client, password_factory, db, mailoutbox):
settings.ACCOUNT_USERNAME_REQUIRED = False
settings.ACCOUNT_EMAIL_REQUIRED = True
settings.ACCOUNT_EMAIL_VERIFICATION = app_settings.EmailVerificationMethod.MANDATORY
Expand All @@ -405,3 +405,4 @@ def test_signup_user_model_no_email(settings, client, password_factory, db):
email = EmailAddress.objects.get(email=email)
assert email.primary
assert not email.verified
assert len(mailoutbox) == 1
21 changes: 18 additions & 3 deletions allauth/account/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,31 @@ def send_email_confirmation(request, user, signup=False, email=None):
sent (consider a user retrying a few times), which is why there is
a cooldown period before sending a new mail. This cooldown period
can be configured in ACCOUNT_EMAIL_CONFIRMATION_COOLDOWN setting.
TODO: This code is doing way too much. Looking up EmailAddress, creating
if not present, etc. To be refactored.
"""
from .models import EmailAddress

adapter = get_adapter()

email_address = None
if not email:
email = user_email(user)
if not email:
email_address = (
EmailAddress.objects.filter(user=user).order_by("verified", "pk").first()
)
if email_address:
email = email_address.email

if email:
try:
email_address = EmailAddress.objects.get_for_user(user, email)
if email_address is None:
try:
email_address = EmailAddress.objects.get_for_user(user, email)
except EmailAddress.DoesNotExist:
pass
if email_address is not None:
if not email_address.verified:
send_email = adapter.should_send_confirmation_mail(
request, email_address
Expand All @@ -384,7 +399,7 @@ def send_email_confirmation(request, user, signup=False, email=None):
email_address.send_confirmation(request, signup=signup)
else:
send_email = False
except EmailAddress.DoesNotExist:
else:
send_email = True
email_address = EmailAddress.objects.add_email(
request, user, email, signup=signup, confirm=True
Expand Down

0 comments on commit 6790542

Please sign in to comment.