|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
from __future__ import unicode_literals |
|
3
|
|
|
""" |
|
4
|
|
|
Backend |
|
5
|
|
|
|
|
6
|
|
|
This is a modification of django-registration_ ``backends/__init__.py`` |
|
7
|
|
|
The original code is written by James Bennett |
|
8
|
|
|
|
|
9
|
|
|
.. _django-registration: https://bitbucket.org/ubernostrum/django-registration |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
Original License:: |
|
13
|
|
|
|
|
14
|
|
|
Copyright (c) 2007-2011, James Bennett |
|
15
|
|
|
All rights reserved. |
|
16
|
|
|
|
|
17
|
|
|
Redistribution and use in source and binary forms, with or without |
|
18
|
|
|
modification, are permitted provided that the following conditions are |
|
19
|
|
|
met: |
|
20
|
|
|
|
|
21
|
|
|
* Redistributions of source code must retain the above copyright |
|
22
|
|
|
notice, this list of conditions and the following disclaimer. |
|
23
|
|
|
* Redistributions in binary form must reproduce the above |
|
24
|
|
|
copyright notice, this list of conditions and the following |
|
25
|
|
|
disclaimer in the documentation and/or other materials provided |
|
26
|
|
|
with the distribution. |
|
27
|
|
|
* Neither the name of the author nor the names of other |
|
28
|
|
|
contributors may be used to endorse or promote products derived |
|
29
|
|
|
from this software without specific prior written permission. |
|
30
|
|
|
|
|
31
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
32
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
33
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
34
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
35
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
36
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
37
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
38
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
39
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
40
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
41
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
42
|
|
|
""" |
|
43
|
|
|
__author__ = 'Alisue <[email protected]>' |
|
44
|
|
|
__all__ = ('get_backend', 'RegistrationBackendBase') |
|
45
|
|
|
from django.core.exceptions import ImproperlyConfigured |
|
46
|
|
|
|
|
47
|
|
|
from registration.conf import settings |
|
48
|
|
|
from registration.compat import import_module |
|
49
|
|
|
from registration.backends.base import RegistrationBackendBase |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
View Code Duplication |
def get_backend_class(path=None): |
|
|
|
|
|
|
53
|
|
|
""" |
|
54
|
|
|
Return an class of a registration backend, given the dotted |
|
55
|
|
|
Python import path (as a string) to the backend class. |
|
56
|
|
|
|
|
57
|
|
|
If the backend cannot be located (e.g., because no such module |
|
58
|
|
|
exists, or because the module does not contain a class of the |
|
59
|
|
|
appropriate name), ``django.core.exceptions.ImproperlyConfigured`` |
|
60
|
|
|
is raised. |
|
61
|
|
|
|
|
62
|
|
|
""" |
|
63
|
|
|
path = path or settings.REGISTRATION_BACKEND_CLASS |
|
64
|
|
|
i = path.rfind('.') |
|
65
|
|
|
module, attr = path[:i], path[i+1:] |
|
66
|
|
|
try: |
|
67
|
|
|
mod = import_module(module) |
|
68
|
|
|
except ImportError as e: |
|
69
|
|
|
raise ImproperlyConfigured( |
|
70
|
|
|
'Error loading registration backend %s: "%s"' % (module, e)) |
|
71
|
|
|
try: |
|
72
|
|
|
cls = getattr(mod, attr) |
|
73
|
|
|
except AttributeError: |
|
74
|
|
|
raise ImproperlyConfigured(( |
|
75
|
|
|
'Module "%s" does not define a registration backend named "%s"' |
|
76
|
|
|
) % (module, attr)) |
|
77
|
|
|
if cls and not issubclass(cls, RegistrationBackendBase): |
|
78
|
|
|
raise ImproperlyConfigured(( |
|
79
|
|
|
'Registration backend class "%s" must be a subclass of ' |
|
80
|
|
|
'``registration.backends.RegistrationBackendBase``') % path) |
|
81
|
|
|
return cls |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
def get_backend(path=None): |
|
85
|
|
|
""" |
|
86
|
|
|
Return an instance of a registration backend, given the dotted |
|
87
|
|
|
Python import path (as a string) to the backend class. |
|
88
|
|
|
|
|
89
|
|
|
If the backend cannot be located (e.g., because no such module |
|
90
|
|
|
exists, or because the module does not contain a class of the |
|
91
|
|
|
appropriate name), ``django.core.exceptions.ImproperlyConfigured`` |
|
92
|
|
|
is raised. |
|
93
|
|
|
|
|
94
|
|
|
""" |
|
95
|
|
|
return get_backend_class(path)() |
|
96
|
|
|
|