From 733d359e4a6e3378b7590225db8b01ef9413a01f Mon Sep 17 00:00:00 2001 From: Justin Hamade Date: Wed, 24 Jul 2013 14:57:52 -0700 Subject: [PATCH 1/2] Add regex of url list to check to ignore --- mobler/middleware.py | 32 ++++++++++++++++++-------------- mobler/settings.py | 1 + 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/mobler/middleware.py b/mobler/middleware.py index c5a4cf4..a2da84b 100755 --- a/mobler/middleware.py +++ b/mobler/middleware.py @@ -4,6 +4,7 @@ import time import urlparse import urllib +import re COOKIE_AGE = mobler_settings.MOBLER_COOKIE_AGE COOKIE_NAME = mobler_settings.MOBLER_COOKIE_NAME @@ -24,19 +25,22 @@ def process_request(self, request): is_mobile = browser.detect_mobile(user_agent) do_override = (request.COOKIES.get(COOKIE_NAME) == '1') - ## Check for the override GET param. If we have it, set the cookie and refresh. - if is_mobile and request.GET.get(COOKIE_NAME): - response = redirect(build_full_path(request)) - response.set_cookie(COOKIE_NAME, '1', time.time()+COOKIE_AGE) - return response + all_regex = "(" + ")|(".join(mobler_settings.MOBLER_EXCLUDE_URLS) + ")" - ## If we have a mobile UA but no browser override, redirect - ## to the mobile site. - if is_mobile and not do_override: - mobile_url = mobler_settings.MOBILE_DOMAIN - if mobler_settings.MOBLER_PRESERVE_URL: - mobile_url = urlparse.urljoin(mobile_url, build_full_path(request)) - return redirect(mobile_url) + if re.match(all_regex, request.path) is None: + ## Check for the override GET param. If we have it, set the cookie and refresh. + if is_mobile and request.GET.get(COOKIE_NAME): + response = redirect(build_full_path(request)) + response.set_cookie(COOKIE_NAME, '1', time.time()+COOKIE_AGE) + return response - ## We might be mobile, so set a flag on the request for convenience. - request.is_mobile = is_mobile + ## If we have a mobile UA but no browser override, redirect + ## to the mobile site. + if is_mobile and not do_override: + mobile_url = mobler_settings.MOBILE_DOMAIN + if mobler_settings.MOBLER_PRESERVE_URL: + mobile_url = urlparse.urljoin(mobile_url, build_full_path(request)) + return redirect(mobile_url) + + ## We might be mobile, so set a flag on the request for convenience. + request.is_mobile = is_mobile diff --git a/mobler/settings.py b/mobler/settings.py index 546bd04..7f01bd7 100755 --- a/mobler/settings.py +++ b/mobler/settings.py @@ -17,3 +17,4 @@ MOBLER_COOKIE_AGE = getattr(settings, 'MOBLER_COOKIE_AGE', 60*60*24*31) MOBLER_PRESERVE_URL = getattr(settings, 'MOBLER_PRESERVE_URL', True) MOBLER_UA_STRINGS = getattr(settings, 'MOBLER_UA_STRINGS', MOBLER_DEFAULT_UA_STRINGS) +MOBLER_EXCLUDE_URLS = getattr(settings, 'MOBLER_EXCLUDE_URLS', []) From ea409a05003fb5d3e1f1053f9f7a36434605b24f Mon Sep 17 00:00:00 2001 From: Justin Hamade Date: Mon, 29 Jul 2013 14:48:07 -0700 Subject: [PATCH 2/2] Made appending a url append as a relative url and remove the leading / --- mobler/middleware.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mobler/middleware.py b/mobler/middleware.py index a2da84b..8ee067e 100755 --- a/mobler/middleware.py +++ b/mobler/middleware.py @@ -39,7 +39,7 @@ def process_request(self, request): if is_mobile and not do_override: mobile_url = mobler_settings.MOBILE_DOMAIN if mobler_settings.MOBLER_PRESERVE_URL: - mobile_url = urlparse.urljoin(mobile_url, build_full_path(request)) + mobile_url = urlparse.urljoin(mobile_url, build_full_path(request)[1:]) return redirect(mobile_url) ## We might be mobile, so set a flag on the request for convenience.