Skip to content

Commit

Permalink
refactor(account): email_address_exists -> EmailAddress.objects.is_ve…
Browse files Browse the repository at this point in the history
…rified
  • Loading branch information
pennersr committed Jun 16, 2023
1 parent 40d05c3 commit 9fccdd0
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 21 deletions.
5 changes: 1 addition & 4 deletions ChangeLog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ Backwards incompatible changes
automatically transition, but if you have a lot of accounts, you may need to
take special care using ``CREATE INDEX CONCURRENTLY``.

- The method ``allauth.utils.email_address_exists()`` now only returns ``True``
when an ``EmailAddress`` record exists that is verified. Previously, it would
return ``True`` when the email has any match (regardless of the verified
state) with an ``EmailAddress`` or user (``User.email``).
- The method ``allauth.utils.email_address_exists()`` has been removed.


Note worthy changes
Expand Down
5 changes: 3 additions & 2 deletions allauth/account/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from allauth.account.app_settings import EmailVerificationMethod
from allauth.utils import (
build_absolute_uri,
email_address_exists,
generate_unique_username,
get_user_model,
import_attribute,
Expand Down Expand Up @@ -306,7 +305,9 @@ def clean_password(self, password, user=None):
return password

def validate_unique_email(self, email):
if email_address_exists(email):
from .models import EmailAddress

if EmailAddress.objects.is_verified(email):
raise forms.ValidationError(self.error_messages["email_taken"])
return email

Expand Down
3 changes: 3 additions & 0 deletions allauth/account/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def get_for_user(self, user, email):
return address
raise self.model.DoesNotExist()

def is_verified(self, email):
return self.filter(email__iexact=email, verified=True).exists()


class EmailConfirmationManager(models.Manager):
def all_expired(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class Migration(migrations.Migration):
dependencies = [
("account", "0003_alter_emailaddress_unique_together_and_more"),
("account", "0003_alter_emailaddress_create_unique_verified_email"),
]

operations = [
Expand Down
5 changes: 3 additions & 2 deletions allauth/account/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from allauth.account.adapter import get_adapter
from allauth.exceptions import ImmediateHttpResponse
from allauth.utils import (
email_address_exists,
get_request_param,
get_user_model,
import_callable,
Expand Down Expand Up @@ -202,6 +201,8 @@ def cleanup_email_addresses(request, addresses):
Order is important: e.g. if multiple primary e-mail addresses
exist, the first one encountered will be kept as primary.
"""
from .models import EmailAddress

adapter = get_adapter(request)
# Let's group by `email`
e2a = OrderedDict() # maps email to EmailAddress
Expand All @@ -217,7 +218,7 @@ def cleanup_email_addresses(request, addresses):
if (
app_settings.UNIQUE_EMAIL
and address.verified
and email_address_exists(email)
and EmailAddress.objects.is_verified(email)
):
continue
a = e2a.get(email.lower())
Expand Down
3 changes: 1 addition & 2 deletions allauth/socialaccount/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from ..account.utils import user_email, user_field, user_username
from ..utils import (
deserialize_instance,
email_address_exists,
import_attribute,
serialize_instance,
valid_email_or_none,
Expand Down Expand Up @@ -144,7 +143,7 @@ def is_auto_signup_allowed(self, request, sociallogin):
# Let's check if auto_signup is really possible...
if email:
if account_settings.UNIQUE_EMAIL:
if email_address_exists(email):
if EmailAddress.objects.is_verified(email):
# Oops, another user already has this address.
# We cannot simply connect this social account
# to the existing user. Reason is that the
Expand Down
10 changes: 0 additions & 10 deletions allauth/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,6 @@ def valid_email_or_none(email):
return ret


def email_address_exists(email, exclude_user=None):
from .account.models import EmailAddress

emailaddresses = EmailAddress.objects
if exclude_user:
emailaddresses = emailaddresses.exclude(user=exclude_user)
ret = emailaddresses.filter(email__iexact=email, verified=True).exists()
return ret


def import_attribute(path):
assert isinstance(path, str)
pkg, attr = path.rsplit(".", 1)
Expand Down

0 comments on commit 9fccdd0

Please sign in to comment.