2

What was done

I have a custom User model called MyUser as it is described in an full example for an custom user model on the docs and a so called UserProfile that is connected to MyUser by an One-to-one relationship like it's described in the Django documentation about how to extend an existing user model.

In the documentation for Customizing authentication is an example for how to connect a post_save signal with the existing User model.

By following this, I put an testing receiver in a signals.py that is loaded by my apps.py file, like it's described in this SO answer.

Configuration

signal.py

def post_save_receiver(sender, instance, created, **kwargs):
    send_mail('Testing', "Just testing...", 'hell@earth.tld', ['admin@heaven.tld'], fail_silently=False)

post_save.connect(post_save_receiver, sender=settings.AUTH_USER_MODEL)

models.py

class MyUserManager(BaseUserManager):
    def create_user(self, email, password=None):
    """
        Creates and saves a User with the given email and password.
        """
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            email=self.normalize_email(email),
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password):
        """
        Creates and saves a superuser with the given email and password.
        """
        user = self.create_user(email,
            password=password,
        )
        user.is_admin = True
        user.save(using=self._db)
        return user




class MyUser(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = MyUserManager()

    USERNAME_FIELD = 'email'

    def get_full_name(self):
        # The user is identified by their email address
        return self.email

    def get_short_name(self):
        # The user is identified by their email address
        return self.email

    def __str__(self):              # __unicode__ on Python 2
        return self.email

    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always
        return True

    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True

    @property
    def is_staff(self):
        "Is the user a member of staff?"
        # Simplest possible answer: All admins are staff
        return self.is_admin

views.py

def UserLogin(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect("http://www.heaven.tld/account")
    else:
        if request.method == 'POST':
            form = UserLoginForm(request.POST)
            if form.is_valid():
                username = form.cleaned_data['username']
                password = form.cleaned_data['password']
                user = authenticate(username=username, password=password)
                if user is not None and user.is_active:
                    login(request, user)
                    return HttpResponseRedirect("http://www.heaven.tld/account")
                else:
                    error_msg = 'Please try again!'
                    return render(request, "development",
                        {'form':form,
                        'error_msg':error_msg,
                         })
            else:
                error_msg = "Please try again!"
                return render(request, "development",
                    {'form':form,
                    'error_msg':error_msg,
                    })
        else:
            form = UserLoginForm()
            return render(request, "development", {'form': form})

The Problem

Now if I log in I always get this mail, but I want it only if a user was created.

Community
  • 1
  • 1
rwx
  • 696
  • 8
  • 25

3 Answers3

7

Logging in causes the last_login timestamp to be updated, which triggers a save.

As others have stated, you just need to use the created parameter, which your signal receives but you currently ignore.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
3

try:

def post_save_receiver(sender, instance, created, **kwargs):
    if created:
        send_mail('Testing', "Just testing...", 'hell@earth.tld',     ['admin@heaven.tld'], fail_silently=False)


post_save.connect(post_save_receiver, sender=settings.AUTH_USER_MODEL)
lukeaus
  • 11,465
  • 7
  • 50
  • 60
2

I can't understand what will be saved when you login. You can use ipdb to trace it. By the way, if you set the created condition before send_mail function, the problem should be resolved.

if created:
    send_mail(...)
Mehdi Pourfar
  • 488
  • 4
  • 13