James Bennett avatar James Bennett committed 0a96b25

Restore the registration/activation signals.

Comments (0)

Files changed (2)

registration/backends/default/__init__.py

 from django.contrib.sites.models import RequestSite
 from django.contrib.sites.models import Site
 
+from registration import signals
 from registration.forms import RegistrationForm
 from registration.models import RegistrationProfile
 
         information about these templates and the contexts provided to
         them.
 
+        After the ``User`` and ``RegistrationProfile`` are created and
+        the activation email is sent, the signal
+        ``registration.signals.user_registered`` will be sent, with
+        the new ``User`` as the keyword argument ``user`` and the
+        class of this backend as the sender.
+
         """
         username, email, password = kwargs['username'], kwargs['email'], kwargs['password1']
         if Site._meta.installed:
             site = Site.objects.get_current()
         else:
             site = RequestSite(request)
-        return RegistrationProfile.objects.create_inactive_user(username, email, password, site)
+        new_user = RegistrationProfile.objects.create_inactive_user(username, email,
+                                                                    password, site)
+        signals.user_registered.send(sender=self.__class__, user=new_user)
+        return new_user
 
     def activate(self, request, activation_key):
         """
         Given an an activation key, look up and activate the user
         account corresponding to that key (if possible).
+
+        After successful activation, the signal
+        ``registration.signals.user_activated`` will be sent, with the
+        newly activated ``User`` as the keyword argument ``user`` and
+        the class of this backend as the sender.
         
         """
-        return RegistrationProfile.objects.activate_user(activation_key)
+        activated = RegistrationProfile.objects.activate_user(activation_key)
+        if activated:
+            signals.user_activated.send(sender=self.__class__, user=activated)
+        return activated
 
     def registration_allowed(self, request):
         """

registration/tests/backends.py

 from django.test import TestCase
 
 from registration import forms
+from registration import signals
 from registration.models import RegistrationProfile
 
 
         """
         self.assertEqual(self.backend.post_registration_redirect({}, User()),
                          'registration_complete')
+
+    def test_registration_signal(self):
+        """
+        Test that registering a user sends the ``user_registered``
+        signal.
+        
+        """
+        def receiver(sender, **kwargs):
+            self.failUnless('user' in kwargs)
+            self.assertEqual(kwargs['user'].username, 'bob')
+            received_signals.append(kwargs.get('signal'))
+
+        received_signals = []
+        signals.user_registered.connect(receiver, sender=self.backend.__class__)
+
+        self.backend.register({},
+                              username='bob',
+                              email='bob@example.com',
+                              password1='secret')
+
+        self.assertEqual(len(received_signals), 1)
+        self.assertEqual(received_signals, [signals.user_registered])
+
+    def test_activation_signal_success(self):
+        """
+        Test that successfully activating a user sends the
+        ``user_activated`` signal.
+        
+        """
+        def receiver(sender, **kwargs):
+            self.failUnless('user' in kwargs)
+            self.assertEqual(kwargs['user'].username, 'bob')
+            received_signals.append(kwargs.get('signal'))
+
+        received_signals = []
+        signals.user_activated.connect(receiver, sender=self.backend.__class__)
+
+        new_user = self.backend.register({},
+                                         username='bob',
+                                         email='bob@example.com',
+                                         password1='secret')
+        profile = RegistrationProfile.objects.get(user=new_user)
+        self.backend.activate({}, profile.activation_key)
+
+        self.assertEqual(len(received_signals), 1)
+        self.assertEqual(received_signals, [signals.user_activated])
+
+    def test_activation_signal_failure(self):
+        """
+        Test that an unsuccessful activation attempt does not send the
+        ``user_activated`` signal.
+        
+        """
+        def receiver(sender, **kwargs):
+            received_signals.append(kwargs.get('signal'))
+
+        received_signals = []
+        signals.user_activated.connect(receiver, sender=self.backend.__class__)
+
+        new_user = self.backend.register({},
+                                         username='bob',
+                                         email='bob@example.com',
+                                         password1='secret')
+        new_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1)
+        new_user.save()
+        profile = RegistrationProfile.objects.get(user=new_user)
+        self.backend.activate({}, profile.activation_key)
+
+        self.assertEqual(len(received_signals), 0)
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.