1. Nicolas Kuttler
  2. django-registration


ubernostrum  committed 7a7813c

Handle profile creation via callback

  • Participants
  • Parent commits 195dbb4
  • Branches default

Comments (0)

Files changed (2)

File registration/models.py

View file
  • Ignore whitespace
                 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

File registration/views.py

View file
  • Ignore whitespace
                                 'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS },
-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.
             The registration form
         if form.is_valid():
             new_user = RegistrationProfile.objects.create_inactive_user(username=form.cleaned_data['username'],
-                                                                        email=form.cleaned_data['email'])
+                                                                        email=form.cleaned_data['email'],
+                                                                        profile_callback=profile_callback)
             return HttpResponseRedirect(success_url)
         form = RegistrationForm()