Skip to content

Commit a68fb24

Browse files
committed
Corner case handling for non existing email address
Special handling if there is no corresponding email address object for the EmailAddress model, this could happen if the rest_auth app has been installed after some users had already been created.
1 parent 624ad01 commit a68fb24

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

rest_auth/serializers.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,15 @@ def validate(self, attrs):
104104
# If required, is the email verified?
105105
if 'rest_auth.registration' in settings.INSTALLED_APPS:
106106
from allauth.account import app_settings
107+
from allauth.account.models import EmailAddress
107108
if app_settings.EMAIL_VERIFICATION == app_settings.EmailVerificationMethod.MANDATORY:
108-
email_address = user.emailaddress_set.get(email=user.email)
109+
try:
110+
email_address = user.emailaddress_set.get(email=user.email)
111+
# There is no corresponding email address object for the EmailAddress model, this could happen
112+
# if the rest_auth app has bee installed after some users had already been created.
113+
except EmailAddress.DoesNotExist:
114+
email_address = EmailAddress(user=user, email=user.email, verified=True, primary=True)
115+
email_address.save()
109116
if not email_address.verified:
110117
raise serializers.ValidationError(_('E-mail is not verified.'))
111118

0 commit comments

Comments
 (0)