From ed03e562e2bc11ccf00e4dbaa9ebab70ff13c3d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20P=C3=BCtz?= Date: Wed, 19 Feb 2020 13:27:06 +0100 Subject: [PATCH] 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. --- rest_auth/serializers.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/rest_auth/serializers.py b/rest_auth/serializers.py index b6452317..f9f8acc2 100644 --- a/rest_auth/serializers.py +++ b/rest_auth/serializers.py @@ -104,8 +104,15 @@ def validate(self, attrs): # If required, is the email verified? if 'rest_auth.registration' in settings.INSTALLED_APPS: from allauth.account import app_settings + from allauth.account.models import EmailAddress if app_settings.EMAIL_VERIFICATION == app_settings.EmailVerificationMethod.MANDATORY: - email_address = user.emailaddress_set.get(email=user.email) + try: + email_address = user.emailaddress_set.get(email=user.email) + # There is no corresponding email address object for the EmailAddress model, this could happen + # if the rest_auth app has bee installed after some users had already been created. + except EmailAddress.DoesNotExist: + email_address = EmailAddress(email=user.email, verified=True, primary=True) + email_address.save() if not email_address.verified: raise serializers.ValidationError(_('E-mail is not verified.'))