Skip to content

Commit

Permalink
avoid adding an EmailAddress for a user when the same e-mail is being…
Browse files Browse the repository at this point in the history
… already used by another user.

This fixes scott2b#10
  • Loading branch information
clemente committed Dec 17, 2014
1 parent 0cfb768 commit 71b69d2
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions multimail/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def email_address_handler(sender, **kwargs):
# loading fixtures etc.
return
try:
addr = None
if MM.SEND_EMAIL_ON_USER_SAVE_SIGNAL:
if user.email:
addr = EmailAddress.objects.filter(user=user,
Expand All @@ -169,12 +170,18 @@ def email_address_handler(sender, **kwargs):
user.is_active and not addr.verified_at:
addr.verified_at = now()
except EmailAddress.DoesNotExist:
addr = EmailAddress()
addr.user = user
addr.email = user.email
addr.save(verify=False)
addr._set_primary_flags() # do this for every save in case things
# get out of sync
# Add new e-mail address for this user except if the e-mail
# belongs to another user
if EmailAddress.objects.filter(email=user.email).count()==0:
addr = EmailAddress()
addr.user = user
addr.email = user.email
# Save existing or new address
if addr:
addr.save(verify=False)
if addr:
addr._set_primary_flags() # do this for every save in case
# things get out of sync
except Exception:
msg = """An attempt to create EmailAddress object for user %s, email
%s has failed. This may indicate that an EmailAddress object for that email
Expand Down

1 comment on commit 71b69d2

@clemente
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This made my tests run, and it avoids IntegrityError. Please check whether it makes sense.
I think that big if could be simplified (right now there are too many branches), but I don't understand all of them.

Please sign in to comment.