-
Notifications
You must be signed in to change notification settings - Fork 169
/
middleware.py
80 lines (67 loc) · 3.38 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import re
from django_mobile import flavour_storage
from django_mobile import set_flavour, _init_flavour
from django_mobile.conf import settings
class SetFlavourMiddleware(object):
def process_request(self, request):
_init_flavour(request)
if settings.FLAVOURS_GET_PARAMETER in request.GET:
flavour = request.GET[settings.FLAVOURS_GET_PARAMETER]
if flavour in settings.FLAVOURS:
set_flavour(flavour, request, permanent=True)
def process_response(self, request, response):
flavour_storage.save(request, response)
return response
class MobileDetectionMiddleware(object):
user_agents_test_match = (
"w3c ", "acs-", "alav", "alca", "amoi", "audi",
"avan", "benq", "bird", "blac", "blaz", "brew",
"cell", "cldc", "cmd-", "dang", "doco", "eric",
"hipt", "inno", "ipaq", "java", "jigs", "kddi",
"keji", "leno", "lg-c", "lg-d", "lg-g", "lge-",
"maui", "maxo", "midp", "mits", "mmef", "mobi",
"mot-", "moto", "mwbp", "nec-", "newt", "noki",
"xda", "palm", "pana", "pant", "phil", "play",
"port", "prox", "qwap", "sage", "sams", "sany",
"sch-", "sec-", "send", "seri", "sgh-", "shar",
"sie-", "siem", "smal", "smar", "sony", "sph-",
"symb", "t-mo", "teli", "tim-", "tosh", "tsm-",
"upg1", "upsi", "vk-v", "voda", "wap-", "wapa",
"wapi", "wapp", "wapr", "webc", "winw", "xda-",)
user_agents_test_search = u"(?:%s)" % u'|'.join((
'up.browser', 'up.link', 'mmp', 'symbian', 'smartphone', 'midp',
'wap', 'phone', 'windows ce', 'pda', 'mobile', 'mini', 'palm',
'netfront', 'opera mobi',
))
user_agents_exception_search = u"(?:%s)" % u'|'.join((
'ipad',
))
http_accept_regex = re.compile("application/vnd\.wap\.xhtml\+xml", re.IGNORECASE)
def __init__(self):
user_agents_test_match = r'^(?:%s)' % '|'.join(self.user_agents_test_match)
self.user_agents_test_match_regex = re.compile(user_agents_test_match, re.IGNORECASE)
self.user_agents_test_search_regex = re.compile(self.user_agents_test_search, re.IGNORECASE)
self.user_agents_exception_search_regex = re.compile(self.user_agents_exception_search, re.IGNORECASE)
def process_request(self, request):
is_mobile = False
if 'HTTP_USER_AGENT' in request.META :
user_agent = request.META['HTTP_USER_AGENT']
# Test common mobile values.
if self.user_agents_test_search_regex.search(user_agent) and \
not self.user_agents_exception_search_regex.search(user_agent):
is_mobile = True
else:
# Nokia like test for WAP browsers.
# http://www.developershome.com/wap/xhtmlmp/xhtml_mp_tutorial.asp?page=mimeTypesFileExtension
if 'HTTP_ACCEPT' in request.META :
http_accept = request.META['HTTP_ACCEPT']
if self.http_accept_regex.search(http_accept):
is_mobile = True
if not is_mobile:
# Now we test the user_agent from a big list.
if self.user_agents_test_match_regex.match(user_agent):
is_mobile = True
if is_mobile:
set_flavour(settings.DEFAULT_MOBILE_FLAVOUR, request)
else:
set_flavour(settings.FLAVOURS[0], request)