-
Notifications
You must be signed in to change notification settings - Fork 5
/
middleware.py
54 lines (41 loc) · 1.47 KB
/
middleware.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
try:
from threading import local
except ImportError:
from django.utils._threading_local import local
from importlib import import_module
from django.conf import settings
_thread_locals = local()
def get_current_request():
return getattr(_thread_locals, 'request', None)
def get_current_layer():
return getattr(_thread_locals, 'layer', {})
layer_confs = {}
def store_conf_from_module(mod):
conf = mod.get_config()
for k, v in conf.iteritems():
if k not in layer_confs:
layer_confs[k] = v
layer_confs[k].update(v)
def load_conf(app, module):
try:
mod = import_module(app + "." + module)
if hasattr(mod, 'get_config') and callable(mod.get_config):
store_conf_from_module(mod)
except (ImportError, AttributeError):
pass # don't care
for app in settings.INSTALLED_APPS:
## "layers" turned out to be a poor module name since it's the
## same as this package!
load_conf(app, "layers")
load_conf(app, "select_layer")
def get_active_layer(request, layer_funcs=None):
""" return the first layer func result that is not false """
from layers.loader import app_layers_funcs
for f in (layer_funcs or app_layers_funcs):
prefix = f(request)
if prefix:
return prefix
class LayerLoaderMiddleware(object):
def process_request(self, request):
_thread_locals.request = request
_thread_locals.layer = layer_confs.get(get_active_layer(request))