Commits

James Bennett committed a5e4589

Move activation email onto an instance method.

Comments (0)

Files changed (2)

registration/models.py

         registration_profile = self.create_profile(new_user)
 
         if send_email:
-            self.send_activation_email(registration_profile, site)
+            registration_profile.send_activation_email(site)
 
         return new_user
     create_inactive_user = transaction.commit_on_success(create_inactive_user)
 
-    def send_activation_email(self, profile, site):
-        """
-        Send an activation email to the user associated with the given
-        ``RegistrationProfile``.
-        
-        The activation email will make use of two templates:
-
-        ``registration/activation_email_subject.txt``
-            This template will be used for the subject line of the
-            email. Because it is used as the subject line of an email,
-            this template's output **must** be only a single line of
-            text; output longer than one line will be forcibly joined
-            into only a single line.
-
-        ``registration/activation_email.txt``
-            This template will be used for the body of the email.
-
-        These templates will each receive the following context
-        variables:
-
-        ``activation_key``
-            The activation key for the new account.
-
-        ``expiration_days``
-            The number of days remaining during which the account may
-            be activated.
-
-        ``site``
-            An object representing the site on which the user
-            registered; depending on whether ``django.contrib.sites``
-            is installed, this may be an instance of either
-            ``django.contrib.sites.models.Site`` (if the sites
-            application is installed) or
-            ``django.contrib.sites.models.RequestSite`` (if
-            not). Consult the documentation for the Django sites
-            framework for details regarding these objects' interfaces.
-
-        """
-        ctx_dict = { 'activation_key': profile.activation_key,
-                     'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
-                     'site': site }
-        subject = render_to_string('registration/activation_email_subject.txt',
-                                   ctx_dict)
-        # Email subject *must not* contain newlines
-        subject = ''.join(subject.splitlines())
-        
-        message = render_to_string('registration/activation_email.txt',
-                                   ctx_dict)
-        
-        profile.user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
-    
     def create_profile(self, user):
         """
         Create a ``RegistrationProfile`` for a given
         return self.activation_key == self.ACTIVATED or \
                (self.user.date_joined + expiration_date <= datetime.datetime.now())
     activation_key_expired.boolean = True
+
+    def send_activation_email(self, site):
+        """
+        Send an activation email to the user associated with this
+        ``RegistrationProfile``.
+        
+        The activation email will make use of two templates:
+
+        ``registration/activation_email_subject.txt``
+            This template will be used for the subject line of the
+            email. Because it is used as the subject line of an email,
+            this template's output **must** be only a single line of
+            text; output longer than one line will be forcibly joined
+            into only a single line.
+
+        ``registration/activation_email.txt``
+            This template will be used for the body of the email.
+
+        These templates will each receive the following context
+        variables:
+
+        ``activation_key``
+            The activation key for the new account.
+
+        ``expiration_days``
+            The number of days remaining during which the account may
+            be activated.
+
+        ``site``
+            An object representing the site on which the user
+            registered; depending on whether ``django.contrib.sites``
+            is installed, this may be an instance of either
+            ``django.contrib.sites.models.Site`` (if the sites
+            application is installed) or
+            ``django.contrib.sites.models.RequestSite`` (if
+            not). Consult the documentation for the Django sites
+            framework for details regarding these objects' interfaces.
+
+        """
+        ctx_dict = { 'activation_key': self.activation_key,
+                     'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
+                     'site': site }
+        subject = render_to_string('registration/activation_email_subject.txt',
+                                   ctx_dict)
+        # Email subject *must not* contain newlines
+        subject = ''.join(subject.splitlines())
+        
+        message = render_to_string('registration/activation_email.txt',
+                                   ctx_dict)
+        
+        self.user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
+    

registration/tests/models.py

 
     def test_activation_email(self):
         """
-        ``RegistrationProfile.objects.send_activation_email`` sends an
+        ``RegistrationProfile.send_activation_email`` sends an
         email.
         
         """
         new_user = User.objects.create_user(**self.user_info)
         profile = RegistrationProfile.objects.create_profile(new_user)
-        RegistrationProfile.objects.send_activation_email(profile,
-                                                          Site.objects.get_current())
+        profile.send_activation_email(Site.objects.get_current())
         self.assertEqual(len(mail.outbox), 1)
         self.assertEqual(mail.outbox[0].to, [self.user_info['email']])