0

I am writing a signal function that will send a signup notification email to the user when s/he signs up and an update notification email when s/he updates the user profile. For that, I have used an if-else block to check the created parameter. If it is true, the user will get signup notification. Otherwise, the user will get a profile update notification. It works fine when the user object is updated. But when a new user signs up, both of the if and else blocks are executing and the new signed up user gets two email notification: one signup notification and one profile update notification. The full signal code is given below:

#myapp/signals.py

from django.core.mail import send_mail
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.template.loader import render_to_string
from django.utils.html import strip_tags

#getting custom user model
User = get_user_model()


@receiver(post_save, sender=User)
def send_user_notification(sender, instance, created, **kwargs):
    if created:
        subject = 'Signup Notification'
        html_message = render_to_string(
            'email/signup_notification.html',
            {
                'username': instance.username,
                'email': instance.email,
            }
        )
        message = strip_tags(html_message)
        send_mail(subject, message, from_email='info@silatechnologies.com', recipient_list=[instance.email,],
                  html_message=html_message)

    else:
        send_mail(subject='Your profile on Sila Crowdfunding updated!',
                  message='Dear ' + instance.username + ', your profile information is updated successfully.',
                  from_email='info@silatechnologies.com',
                  recipient_list=[instance.email, ]
                  )

I have also used if not created instead of else. But I am getting the same result. Can anyone provide any solution of that? Thanks in advance.

update:

My view function for registering user may be helpful for solving the issue. Here is the view function:

def signup(request):
    """
    Render signup form if request is GET else create user
    """
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            return redirect('home')
    else:
        form = SignUpForm()
    return render(request, 'registration/signup.html', {'form': form})
Muminur Rahman
  • 575
  • 1
  • 8
  • 23
  • i think in your logic when user is signs up, there are two action first is create and second update. Simple add print or log to your signal and you see it. – Brown Bear Apr 22 '19 at 08:40
  • Are you using any custom signup form which updates the `User` model? might be possible that when signing up, two `save` calls are being made. and that is why a new user is getting two emails – Aman Garg Apr 22 '19 at 08:41
  • @AmanGarg i have used a custom form. But I think, the problem is what Alex is said in the answer. – Muminur Rahman Apr 22 '19 at 08:51

1 Answers1

1

Assuming that you are using Django's built-in registration flow this will imply 2 actions on the User model:

  1. It will create the User object, thus firing the post_save signal with created=True and you will be sending the first email.
  2. After registration, Django logs in the user and updates it's last_login field, thus firing another post_save signal with created=False
Alex
  • 2,356
  • 15
  • 19
  • I think that is the problem why I am getting two emails. But I have a question. If the last_login table is updated in evvery login, why not the user getting update email when he logged in? I have checked, when the user logs in, he does not get any email. – Muminur Rahman Apr 22 '19 at 08:53
  • I got the problem. It is what you have said. I have removed the `login(request, user)' line and I am getting only sign up notification. – Muminur Rahman Apr 22 '19 at 08:58