0

I want to receive a signal when user is activated (i.e. when auth_user.is_active becomes 1). I only want to receive this signal once, the very first time that the user is activated.

I have used the answer given to this question, and it works for me:

@receiver(pre_save, sender=User, dispatch_uid='get_active_user_once')
def new_user_activation_handler(sender, instance, **kwargs):
    if instance.is_active and User.objects.filter(pk=instance.pk, is_active=False).exists():
        logger.info('user is activated')

However this seems to be a customized signal, I believe django has a built-in user_activated signal. I have tried using the built-in signal but it does not fire:

signals.py:

from django_registration.signals import user_activated

@receiver(user_activated, sender=User, dispatch_uid='django_registration.signals.user_activated')
def new_user_activation_handler(sender, instance, **kwargs):
    logger.info('user is activated')

Also this is what I have in apps.py:

class MyClassConfig(AppConfig):
    name = 'myclass'

    def ready(self):
        logger.info('ready...')
        import myclass.signals              # wire up signals ? 

Not sure why this signal is not being fired?

In order to get the above code running, I had to install django-registration package.

All the examples that I have seen have:

from registration.signals import user_activated

But in my case I have to use the a diferent namespace:

from django_registration.signals import user_activated

Not sure why...

Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137
  • 1
    "_I believe django has a built-in user_activated signal._" it does **not**. That signal is part of a 3rd party package. That signal is only fired if you use that packages views to activate the user. Why do you even need a 3rd party package just for a signal? You could easily spin that up yourself. Create a signal, create a function that you decide will be the only way you activate the user, when this function runs just send the signal. In fact if you don't want others to subscribe to this signal you might as well run whatever logic you want in the function itself... – Abdul Aziz Barkat Aug 11 '22 at 04:56

1 Answers1

0

You have wrong sender. Please see: Replace:

@receiver(user_activated, sender=User, dispatch_uid='django_registration.signals.user_activated')
def new_user_activation_handler(sender, instance, **kwargs):
    logger.info('user is activated')

With:

from django_registration.backends.activation.views import ActivationView
@receiver(user_activated, sender=ActivationView, dispatch_uid='django_registration.signals.user_activated')
def new_user_activation_handler(sender, instance, **kwargs):
    logger.info('user is activated')