-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware.py
41 lines (29 loc) · 1004 Bytes
/
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
# -*- coding:utf-8 -*-
'''
@author: wTayyeb https://github.com/wtayyeb
@license: MIT
'''
from django.contrib.sites.models import Site, SITE_CACHE
from .threadlocals import set_thread_variable
class ThemingMiddleware(object):
''' Middleware that puts the request object in thread local storage.
add this middleware to MIDDLEWARE_CLASSES to make theming work.
MIDDLEWARE_CLASSES = (
...
'theming.middleware.ThemingMiddleware',
)
'''
def process_request(self, request):
try:
host = request.get_host()
if host not in SITE_CACHE:
site = Site.objects.get(domain__iexact=host)
SITE_CACHE[host] = site
site = SITE_CACHE[host]
except (Site.DoesNotExist, KeyError):
site = None
try:
sitetheme = site.sitetheme
except (AttributeError):
sitetheme = None
set_thread_variable('sitetheme', sitetheme)