Commits

James Bennett committed bcea338

Backend is now a required argument, and implicit fallback to REGISTRATION_BACKEND is now gone.

Comments (0)

Files changed (5)

registration/backends/__init__.py

 # back to Django's implementation.
 from django.utils.importlib import import_module
 
-def get_backend(path=None):
+def get_backend(path):
     """
-    Return an instance of a registration backend.
-
-    If specified, the backend will be imported from ``path``, which
-    should be the full dotted Python import path to the backend
-    class. If ``path`` is not specified, the backend will be imported
-    based on the value of the setting ``REGISTRATION_BACKEND``, which
-    should similarly be a full dotted Python import path.
+    Return an instance of a registration backend, given the dotted
+    Python import path (as a string) to the backend class.
 
     If the backend cannot be located (e.g., because no such module
     exists, or because the module does not contain a class of the
     is raised.
     
     """
-    if path is None:
-        if not hasattr(settings, 'REGISTRATION_BACKEND') or not settings.REGISTRATION_BACKEND:
-            raise ImproperlyConfigured('Error loading registration backend: no backend specified (have you provided a value for the REGISTRATION_BACKEND setting?)')
-        path = settings.REGISTRATION_BACKEND
     i = path.rfind('.')
     module, attr = path[:i], path[i+1:]
     try:

registration/backends/default/urls.py

                        # confusing 404.
                        url(r'^activate/(?P<activation_key>\w+)/$',
                            activate,
+                           { 'backend': 'registration.backends.default.DefaultBackend' },
                            name='registration_activate'),
                        url(r'^login/$',
                            auth_views.login,
                            name='auth_password_reset_done'),
                        url(r'^register/$',
                            register,
+                           { 'backend': 'registration.backends.default.DefaultBackend' },
                            name='registration_register'),
                        url(r'^register/complete/$',
                            direct_to_template,

registration/tests/backends.py

     properly.
 
     """
-    def setUp(self):
-        """
-        Stash away the original value of
-        ``settings.REGISTRATION_BACKEND`` so it can be restored later.
-        
-        """
-        self.old_backend = getattr(settings, 'REGISTRATION_BACKEND', None)
-
-    def tearDown(self):
-        """
-        Restore the value of ``settings.REGISTRATION_BACKEND``.
-        
-        """
-        settings.REGISTRATION_BACKEND = self.old_backend
-    
     def test_get_backend(self):
         """
-        Set ``REGISTRATION_BACKEND`` temporarily, then verify that
-        ``get_backend()`` returns the correct value.
+        Verify that ``get_backend()`` returns the correct value when
+        passed a valid backend.
 
         """
-        settings.REGISTRATION_BACKEND = 'registration.backends.default.DefaultBackend'
-        self.failUnless(isinstance(get_backend(), DefaultBackend))
-
-    def test_get_backend_with_path(self):
-        """
-        Specifying the backend using a dotted path should load
-        correctly.
-        
-        """
-        # First, clear the setting so it can't accidentally be picked
-        # up from that.
-        settings.REGISTRATION_BACKEND = None
         self.failUnless(isinstance(get_backend('registration.backends.default.DefaultBackend'),
                                    DefaultBackend))
 
-    def test_backend_error_none(self):
-        """
-        Test that an invalid value for the ``REGISTRATION_BACKEND``
-        setting raises the correct exception.
-
-        """
-        settings.REGISTRATION_BACKEND = None
-        self.assertRaises(ImproperlyConfigured, get_backend)
-
     def test_backend_error_invalid(self):
         """
         Test that a nonexistent/unimportable backend raises the
         correct exception.
 
         """
-        settings.REGISTRATION_BACKEND = 'registration.backends.doesnotexist.NonExistentBackend'
-        self.assertRaises(ImproperlyConfigured, get_backend)
+        self.assertRaises(ImproperlyConfigured, get_backend,
+                          'registration.backends.doesnotexist.NonExistentBackend')
 
     def test_backend_attribute_error(self):
         """
         class of the specified name raises the correct exception.
         
         """
-        settings.REGISTRATION_BACKEND = 'registration.backends.default.NonexistentBackend'
-        self.assertRaises(ImproperlyConfigured, get_backend)
+        self.assertRaises(ImproperlyConfigured, get_backend,
+                          'registration.backends.default.NonexistentBackend')
 
 
 class DefaultRegistrationBackendTests(TestCase):

registration/tests/urls.py

                        # name.
                        url(r'^activate-with-template-name/(?P<activation_key>\w+)/$',
                            activate,
-                           {'template_name': 'registration/test_template_name.html'},
+                           {'template_name': 'registration/test_template_name.html',
+                            'backend': 'registration.backends.default.DefaultBackend'},
                            name='registration_test_activate_template_name'),
                        # Test the 'activate' view with
                        # extra_context_argument.
                        url(r'^activate-extra-context/(?P<activation_key>\w+)/$',
                            activate,
-                           {'extra_context': {'foo': 'bar', 'callable': lambda: 'called'}},
+                           {'extra_context': {'foo': 'bar', 'callable': lambda: 'called'},
+                            'backend': 'registration.backends.default.DefaultBackend'},
                            name='registration_test_activate_extra_context'),
                        (r'', include('registration.backends.default.urls')),
                        # Test the 'register' view with custom template
                        # name.
                        url(r'^register-with-template-name/$',
                            register,
-                           {'template_name': 'registration/test_template_name.html'},
+                           {'template_name': 'registration/test_template_name.html',
+                            'backend': 'registration.backends.default.DefaultBackend'},
                            name='registration_test_register_template_name'),
                        # Test the'register' view with extra_context
                        # argument.
                        url(r'^register-extra-context/$',
                            register,
-                           {'extra_context': {'foo': 'bar', 'callable': lambda: 'called'}},
+                           {'extra_context': {'foo': 'bar', 'callable': lambda: 'called'},
+                            'backend': 'registration.backends.default.DefaultBackend'},
                            name='registration_test_register_extra_context'),
                        # Test the 'register' view with custom URL for
                        # closed registration.
                        url(r'^register-with-disallowed-url/$',
                            register,
-                           {'disallowed_url': 'registration_test_custom_disallowed'},
+                           {'disallowed_url': 'registration_test_custom_disallowed',
+                            'backend': 'registration.backends.default.DefaultBackend'},
                            name='registration_test_register_disallowed_url'),
                        # Set up a pattern which will correspond to the
                        # custom 'disallowed_url' above.
                        # on successful registration.
                        url(r'^register-with-success_url/$',
                            register,
-                           {'success_url': 'registration_test_custom_success_url'},
+                           {'success_url': 'registration_test_custom_success_url',
+                            'backend': 'registration.backends.default.DefaultBackend'},
                            name='registration_test_register_success_url'
                            ),
                        # Pattern for custom redirect set above.

registration/views.py

 from registration.backends import get_backend
 
 
-def activate(request,
+def activate(request, backend,
              template_name='registration/activate.html',
-             extra_context=None, backend=None,
-             **kwargs):
+             extra_context=None, **kwargs):
     """
     Activate a user's account.
 
     The actual activation of the account will be delegated to the
-    backend specified by the ``REGISTRATION_BACKEND`` setting
-    (although this can be overridden; see argument list below); the
-    backend's ``activate()`` method will be called, passing any
+    backend specified by the ``backend`` keyword argument (see below);
+    the backend's ``activate()`` method will be called, passing any
     keyword arguments captured from the URL, and will be assumed to
     return a ``User`` if activation was successful, or a value which
     evaluates to ``False`` in boolean context if not.
     **Optional arguments**
 
     ``backend``
-        An instance of a registration backend. If specified, will be
-        used in place of the backend specified by the
-        ``REGISTRATION_BACKEND`` setting.
+        The dotted Python import path to the backend class to use.
 
     ``extra_context``
         A dictionary of variables to add to the template context. Any
                               context_instance=context)
 
 
-def register(request, success_url=None, form_class=None,
+def register(request, backend, success_url=None, form_class=None,
              disallowed_url='registration_disallowed',
              template_name='registration/registration_form.html',
-             extra_context=None, backend=None):
+             extra_context=None):
     """
     Allow a new user to register an account.
 
     The actual registration of the account will be delegated to the
-    backend specified by the ``REGISTRATION_BACKEND`` setting
-    (although this can be overridden; see argument list below); it
-    will be used as follows:
+    backend specified by the ``backend`` keywoard argument (see
+    below); it will be used as follows:
 
     1. The backend's ``registration_allowed()`` method will be called,
        passing the ``HttpRequest``, to determine whether registration
     **Optional arguments**
 
     ``backend``
-        An instance of a registration backend. If specified, will be
-        used in place of the backend specified by the
-        ``REGISTRATION_BACKEND`` setting.
+        The dotted Python import path to the backend class to use.
 
     ``disallowed_url``
         URL to redirect to if registration is not permitted for the