Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django >= 1.10 support #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions django_mobile/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from django_mobile import flavour_storage
from django_mobile import set_flavour, _init_flavour
from django_mobile.conf import settings
from django.utils.deprecation import MiddlewareMixin


class SetFlavourMiddleware(object):
class SetFlavourMiddleware(MiddlewareMixin):
def process_request(self, request):
_init_flavour(request)

Expand All @@ -18,7 +18,7 @@ def process_response(self, request, response):
return response


class MobileDetectionMiddleware(object):
class MobileDetectionMiddleware(MiddlewareMixin):
user_agents_test_match = (
"w3c ", "acs-", "alav", "alca", "amoi", "audi",
"avan", "benq", "bird", "blac", "blaz", "brew",
Expand All @@ -44,17 +44,35 @@ class MobileDetectionMiddleware(object):
))
http_accept_regex = re.compile("application/vnd\.wap\.xhtml\+xml", re.IGNORECASE)

def __init__(self):
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, get_response=None, *args, **kwargs):
self.get_response = get_response
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)
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_mobile = False

if 'HTTP_USER_AGENT' in request.META :
user_agent = request.META['HTTP_USER_AGENT']
# Ipad or Blackberry
if self.user_agents_tablets_search_regex.search(user_agent):
is_mobile = 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_mobile = True

# Test common mobile values.
if self.user_agents_test_search_regex.search(user_agent) and \
Expand Down