dready avatar dready committed 488a6c1

send_activation_email: allow templates to be customized

and create_inactive_user accepts **kwargs that passes through to send_activation_email

Comments (0)

Files changed (1)

registration/models.py

         return False
     
     def create_inactive_user(self, username, email, password,
-                             site, send_email=True):
+                             site, send_email=True, **kwargs):
         """
         Create a new, inactive ``User``, generate a
         ``RegistrationProfile`` and email its activation key to the
 
         By default, an activation email will be sent to the new
         user. To disable this, pass ``send_email=False``.
-        
+
+        ``kwargs`` are passed to ``RegistrationProfile.send_activation_email``.
         """
         new_user = User.objects.create_user(username, email, password)
         new_user.is_active = False
         registration_profile = self.create_profile(new_user)
 
         if send_email:
-            registration_profile.send_activation_email(site)
+            registration_profile.send_activation_email(site, **kwargs)
 
         return new_user
     create_inactive_user = transaction.commit_on_success(create_inactive_user)
                (self.user.date_joined + expiration_date <= datetime.datetime.now())
     activation_key_expired.boolean = True
 
-    def send_activation_email(self, site):
+    def send_activation_email(self, site,
+                              extra_context=None,
+                              subject_template='registration/activation_email_subject.txt',
+                              body_template='registration/activation_email.txt'):
         """
         Send an activation email to the user associated with this
         ``RegistrationProfile``.
-        
+
+        ``extra_context`` - additional context variables to be made available when rendering the templates.
+
         The activation email will make use of two templates:
 
-        ``registration/activation_email_subject.txt``
+        ``subject_template`` (default: ``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``
+        ``body_template`` (default: ``registration/activation_email.txt``)
             This template will be used for the body of the email.
 
         These templates will each receive the following context
         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)
+        ctx_dict.update(extra_context or {})
+        
+        subject = render_to_string(subject_template, ctx_dict)
         # Email subject *must not* contain newlines
         subject = ''.join(subject.splitlines())
         
-        message = render_to_string('registration/activation_email.txt',
-                                   ctx_dict)
+        message = render_to_string(body_template, ctx_dict)
         
         self.user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
     
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.