Skip to content

Commit

Permalink
Corner case handling for non existing email adress
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
spuetz committed Feb 19, 2020
1 parent 624ad01 commit 6af9292
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion rest_auth/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'))

Expand Down

0 comments on commit 6af9292

Please sign in to comment.