forked from gregmuellegger/django-mobile
-
Notifications
You must be signed in to change notification settings - Fork 4
/
middleware.py
43 lines (35 loc) · 1.92 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
import re
from django_mobile.middleware import MobileDetectionMiddleware
from django_mobile import set_flavour
from django.conf import settings
class MobileTabletDetectionMiddleware(MobileDetectionMiddleware):
# Example how default middleware could be expanded to provide possibility to detect
# tablet devices.
user_agents_android_search = u"(?:android)"
user_agents_mobile_search = u"(?:mobile)"
user_agents_tablets_search = u"(?:%s)" % u'|'.join(('ipad', 'tablet', ))
def __init__(self):
super(MobileTabletDetectionMiddleware, self).__init__()
self.user_agents_android_search_regex = re.compile(self.user_agents_android_search,
re.IGNORECASE)
self.user_agents_mobile_search_regex = re.compile(self.user_agents_mobile_search,
re.IGNORECASE)
self.user_agents_tablets_search_regex = re.compile(self.user_agents_tablets_search,
re.IGNORECASE)
def process_request(self, request):
is_tablet = False
user_agent = request.META.get('HTTP_USER_AGENT')
if user_agent:
# Ipad or Blackberry
if self.user_agents_tablets_search_regex.search(user_agent):
is_tablet = True
# Android-device. If User-Agent doesn't contain Mobile, then it's a tablet
elif (self.user_agents_android_search_regex.search(user_agent) and
not self.user_agents_mobile_search_regex.search(user_agent)):
is_tablet = True
else:
# otherwise, let the superclass make decision
super(MobileTabletDetectionMiddleware, self).process_request(request)
# set tablet flavour. It can be `mobile`, `tablet` or anything you want
if is_tablet:
set_flavour(settings.FLAVOURS[2], request)