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

Configureable paths to ignore #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
32 changes: 18 additions & 14 deletions mobler/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)[1:])
return redirect(mobile_url)

## We might be mobile, so set a flag on the request for convenience.
request.is_mobile = is_mobile
1 change: 1 addition & 0 deletions mobler/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', [])