Anonymous avatar Anonymous committed 7a7813c

Handle profile creation via callback

Comments (0)

Files changed (2)

registration/models.py

                 return user
         return False
     
-    def create_inactive_user(self, username, password, email, send_email=True):
+    def create_inactive_user(self, username, password, email, send_email=True, profile_callback=None):
         """
         Creates a new User and a new RegistrationProfile for that
         User, generates an activation key, and mails it.
         
         Pass ``send_email=False`` to disable sending the email.
+
+        To enable creation of a custom user profile along with the
+        User (e.g., the model specified in the ``AUTH_PROFILE_MODULE``
+        setting), define a function which knows how to create and save
+        an instance of that model with appropriate default values, and
+        pass it as the keyword argument ``profile_callback``. This
+        function should accept one argument:
+        
+            user
+                The User object to which the profile will be related.
         
         """
         # Create the user.
         activation_key = sha.new(salt+new_user.username).hexdigest()
         
         # And finally create the profile.
-        new_profile = self.create(user=new_user,
-                                  activation_key=activation_key)
+        registration_profile = self.create(user=new_user,
+                                           activation_key=activation_key)
         
-        # Experimental: create an instance of the model specified
-        # in AUTH_PROFILE_MODULE, if any.
-        #
-        # First draft implementation here relies on an additional
-        # setting -- DEFAULT_AUTH_PROFILE_VALUES -- which must be
-        # a dictionary matching field names and default values for
-        # all non-nullable fields on the custom profile model
-        # (except the foreign key to User, which will be filled in
-        # automatically with the User who was just created).
-        #
-        # Because both of those settings are needed, we only try
-        # this when both are present.
-        if hasattr(settings, 'AUTH_PROFILE_MODULE') and hasattr(settings, 'DEFAULT_AUTH_PROFILE_VALUES'):
-            auth_profile_mod = models.get_model(*settings.AUTH_PROFILE_MODULE.split('.'))
-            new_auth_profile = auth_profile_mod._default_manager.create(user=new_user, **settings.DEFAULT_AUTH_PROFILE_VALUES)
+        # Create site-specific profile, if specified.
+        if profile_callback is not None:
+            profile_callback(user=new_user)
         
         if send_email:
             from django.core.mail import send_mail

registration/views.py

                                 'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS },
                               context_instance=RequestContext(request))
 
-def register(request, success_url='/accounts/register/complete/'):
+def register(request, success_url='/accounts/register/complete/', profile_callback=None):
     """
     Allows a new user to register an account.
-
+    
     On successful registration, an email will be sent to the new user
     with an activation link to click to make the account active. This
     view will then redirect to ``success_url``, which defaults to
     view to display a short message telling the user to check their
     email for the account activation link.
     
+    To enable creation of a site-specific user profile object for the
+    new user, pass a function which will create the profile object as
+    the keyword argument ``profile_callback``. See
+    ``RegistrationManager.create_inactive_user`` for details on what
+    this function should do.
+    
     Context::
         form
             The registration form
         if form.is_valid():
             new_user = RegistrationProfile.objects.create_inactive_user(username=form.cleaned_data['username'],
                                                                         password=form.cleaned_data['password1'],
-                                                                        email=form.cleaned_data['email'])
+                                                                        email=form.cleaned_data['email'],
+                                                                        profile_callback=profile_callback)
             return HttpResponseRedirect(success_url)
     else:
         form = RegistrationForm()
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.