Skip to content
New issue

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

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

Already on GitHub? # to your account

get supp text files to install correctly using setup.py #2

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
8 changes: 5 additions & 3 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ This mostly works using a list of search strings, though there are a couple of o

It also includes a pretty extensive list of user agents to test against.

If sessions are enabled subsequent requests obtain mobile attributes from the session without parsing the requestor's meta data.
If sessions are enabled subsequent requests obtain mobile attributes from the session without parsing the requestor's meta data, unless `MINIDETECTOR_USE_SESSIONS` is set to `False` in `settings.py`.

Optionally "mobile" requestors can be redirected to a mobile domain. If they come back to the non-mobile domain the presence of session data keeps them from be automatically redirected back to the mobile domain.

Adapted from `http://code.google.com/p/minidetector/`

Installation
============

Expand Down Expand Up @@ -92,9 +94,9 @@ You can do the reverse for iphone and android (i.e. having them treat your site
Redirect to mobile domain
------------------------

By default only the request is modified as described above. If `MOBILE_URL` is set to a non empty string and the request.mobile is True the requestor will be redirected to the `MOBILE_URL`.
By default only the request is modified as described above. If `MOBILE_URL` is set to a non empty string and the request.mobile is True the requestor will be redirected to the `MOBILE_URL`. The `MOBILE_URL` setting can contain python format string substitutions (using the .format() string method) --- the variables `path` (request path without query string, such as `/page'), `full_path` (with query string, such as `/page?id=5` and `host` (HTTP host, such as `example.com`) are all available, so if you want to preserve the request path, set `MOBILE_URL` to a path such as `http://mobile.example.com/{full_path}`

If sessions are enabled and a requestor returns to the non mobile site they will not be redirected again to the `MOBILE_URL` site.
If sessions are enabled and a requestor returns to the non mobile site they will not be redirected again to the `MOBILE_URL` site (unless `MINIDETECTOR_USE_SESSIONS` is set to `False`).

This also means that mobile clients that revisit the non-mobile site on a later visit while their session still exists on the server won't be redirected to the mobile version of the site. This behavior can be changed by setting `SESSION_EXPIRE_AT_BROWSER_CLOSE` to True or by setting `SESSION_COOKIE_AGE` to an appropriately short time.

Expand Down
11 changes: 8 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

setup(
name='minidetector',
version='1.2',
version='1.3',
description='Django middleware and view decorator to detect phones and small-screen devices',
long_description = open("readme.markdown").read(),
author='metamoof, Chris Drackett, Steve Schwarz',
url = "http://code.google.com/p/minidetector/",
author='metamoof, Chris Drackett, Steve Schwarz, Andrew MacKinlay',
url = "http://github.com/admackin/minidetector",
package_dir={'': 'src'},
packages = [
"minidetector",
"minidetector.tests",
],
package_data={
'minidetector': ['search_strings.txt'],
'minidetector.tests': ['*.txt'],
},
classifiers = [
"Development Status :: 3 - Alpha",
"Environment :: Web Environment",
Expand Down
26 changes: 23 additions & 3 deletions minidetector/__init__.py → src/minidetector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,41 @@ def process_request(request):
""" Adds a "mobile" attribute to the request which is True or False
depending on whether the request should be considered to come from a
small-screen device such as a phone or a PDA"""
if hasattr(request, 'session'):
if hasattr(request, 'session') and minidetector_settings.USE_SESSIONS:
# session enabled
if not request.session.get('mobile_checked', False):
# haven't checked if mobile yet - put in request and session
Middleware.configure_request(request)
Middleware.set_session_from_request(request)
request.session['mobile_checked'] = True
if request.mobile and minidetector_settings.MOBILE_URL:
return HttpResponseRedirect(minidetector_settings.MOBILE_URL)
if request.mobile:
mobile_redirect = Middleware.get_mobile_redirect(request)
if mobile_redirect:
return mobile_redirect
else:
# Make sure it doesn't try this again
Middleware.set_request_from_session(request)
else:
# sessions disabled - always do the work
Middleware.configure_request(request)
if request.mobile:
mobile_redirect = Middleware.get_mobile_redirect(request)
if mobile_redirect:
return mobile_redirect
return None

@staticmethod
def get_mobile_redirect(request):
if not minidetector_settings.MOBILE_URL:
return None
subs = {
'host': request.get_host(),
'path': request.path,
'full_path': request.get_full_path(),
}
mobile_url = minidetector_settings.MOBILE_URL.format(**subs)
return HttpResponseRedirect(mobile_url)


@staticmethod
def set_session_from_request(request):
Expand Down Expand Up @@ -87,6 +106,7 @@ def configure_request(request):
# algorithm. Certainly more so than regexes.
# Also, Caching didn't help much, with real-world caches.
s = request.META["HTTP_USER_AGENT"].lower()
print "checking user agent", s

if 'applewebkit' in s:
request.browser_is_webkit = True
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions minidetector/settings.py → src/minidetector/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
ANDROID_IS_MOBILE = getattr(settings, 'ANDROID_IS_MOBILE', True)
IPHONE_IS_MOBILE = getattr(settings, 'IPHONE_IS_MOBILE', True)
IPAD_IS_MOBILE = getattr(settings, 'IPAD_IS_MOBILE', False)
USE_SESSIONS = getattr(settings, 'MINIDETECTOR_USE_SESSIONS', True)
File renamed without changes.
File renamed without changes.