Commits

James Bennett  committed 18d5d5a

get_backend() should take an optional path, which makes other things much easier.

  • Participants
  • Parent commits 89e11bb

Comments (0)

Files changed (2)

File registration/__init__.py

 # back to Django's implementation.
 from django.utils.importlib import import_module
 
-def get_backend():
+def get_backend(path=None):
     """
-    Return an instance of the registration backend for use on this
-    site, as determined by the ``REGISTRATION_BACKEND`` setting. Raise
-    ``django.core.exceptions.ImproperlyConfigured`` if the specified
-    backend cannot be located, or if no backend is specified.
+    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 simialrly be a full dotted Python import path.
+
+    If the backend cannot be located (e.g., because no such module
+    exists, or because the module does not contain a class of the
+    appropriate name), ``django.core.exceptions.ImproperlyConfigured``
+    is raised.
     
     """
-    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?)')
-    i = settings.REGISTRATION_BACKEND.rfind('.')
-    module, attr = settings.REGISTRATION_BACKEND[:i], settings.REGISTRATION_BACKEND[i+1:]
+    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:
         mod = import_module(module)
     except ImportError, e:

File registration/tests/backends.py

         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``