Statistics
| Branch: | Tag: | Revision:

root / social / apps / tornado_app / utils.py @ a0a04c0a

History | View | Annotate | Download (1.5 kB)

1
import warnings
2

    
3
from functools import wraps
4

    
5
from social.utils import setting_name
6
from social.strategies.utils import get_strategy
7
from social.backends.utils import get_backend
8

    
9

    
10
DEFAULTS = {
11
    'STORAGE': 'social.apps.tornado_app.models.TornadoStorage',
12
    'STRATEGY': 'social.strategies.tornado_strategy.TornadoStrategy'
13
}
14

    
15

    
16
def get_helper(request_handler, name):
17
    return request_handler.settings.get(setting_name(name),
18
                                        DEFAULTS.get(name, None))
19

    
20

    
21
def load_strategy(request_handler):
22
    strategy = get_helper(request_handler, 'STRATEGY')
23
    storage = get_helper(request_handler, 'STORAGE')
24
    return get_strategy(strategy, storage, request_handler)
25

    
26

    
27
def load_backend(request_handler, strategy, name, redirect_uri):
28
    backends = get_helper(request_handler, 'AUTHENTICATION_BACKENDS')
29
    Backend = get_backend(backends, name)
30
    return Backend(strategy, redirect_uri)
31

    
32

    
33
def psa(redirect_uri=None):
34
    def decorator(func):
35
        @wraps(func)
36
        def wrapper(self, backend, *args, **kwargs):
37
            uri = redirect_uri
38
            if uri and not uri.startswith('/'):
39
                uri = self.reverse_url(uri, backend)
40
            self.strategy = load_strategy(self)
41
            self.backend = load_backend(self, self.strategy, backend, uri)
42
            return func(self, backend, *args, **kwargs)
43
        return wrapper
44
    return decorator
45

    
46

    
47
def strategy(*args, **kwargs):
48
    warnings.warn('@strategy decorator is deprecated, use @psa instead')
49
    return psa(*args, **kwargs)