Coder Social home page Coder Social logo

saschwarz / minidetector Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ericholscher/minidetector

15.0 3.0 13.0 215 KB

Clone from Google Code: Django middleware and view decorator to detect phones and small-screen devices

Home Page: http://code.google.com/p/minidetector/

Python 100.00%

minidetector's Introduction

Info

This application is a simple middleware and associated decorator that will add a ".mobile" attribute to your request objects, which, if True, means the requester is coming to you from a mobile phone (cellphone), PDA, or other device that should be considered small screened, or have an underpowered browser, such as games consoles.

This mostly works using a list of search strings, though there are a couple of other tricks, like detecting the presence of Opera Mini. The strings are in an easily-parseable text file, and thus can be used for other similar projects.

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.

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.

Installation

Using minidetector is very simple. Simply place the minidetector package into your project's path, and then add:

minidetector.Middleware

to your MIDDLEWARE_CLASSES tuple in your settings.py

Then in your view you can check request.mobile - if it's True then treat it like a small screen device. If it's False then it's probably a desktop browser, or a spider or something else.

If you only have certain views that need the distinction, you can choose not to search every request you receive. All you need to do is wrap the relevant views like this:

from minidetector import detect_mobile

@detect_mobile
def my_mobile_view(request):
	if request.mobile:
		#do something with mobile

"Special" Devices

minidetector also adds some extra info for popular devices to the request object. The defaults for a desktop browser:

- request.wap = False
- request.browser_is_android = False
- request.browser_is_ios = False
- request.browser_is_ipad = False
- request.browser_is_iphone = False
- request.browser_is_webkit = False
- request.mobile_device = ''
- request.touch_device = False
- request.wide_device = True

Extra Info

for webkit:

request.browser_is_webkit = True

for the iPad:

request.browser_is_ipad = True
request.mobile_device = 'ipad'

for the iPhone or iPod touch:

request.browser_is_iphone = True
request.mobile_device = 'iphone'

for iOS in general:

request.browser_is_ios = True

and for Android:

request.browser_is_android = True
request.mobile_device = 'android'

I also send 'request.touch_device' as True for all webkit touch devices (iOS and android.)

I also now send 'request.wide_device' as True for all 'wide' devices. Currently this covers all desktops and the ipad. I'll revisit this when other tablets (webOS or android) come out (if I can detect it.)

Override mobile treatment

By default the iPad is not treated as a mobile browser. If you want to reverse this, add the following to your settings.py:

IPAD_IS_MOBILE = True

You can do the reverse for iphone and android (i.e. having them treat your site as non-mobile.)

IPHONE_IS_MOBILE = False
ANDROID_IS_MOBILE = False

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.

If sessions are enabled and a requestor returns to the non mobile site they will not be redirected again to the MOBILE_URL site.

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.

It is also recommended that mobile and non-mobile versions of the sites share the same SESSION_COOKIE_DOMAIN so the sessions are shared between sites: SESSION_COOKIE_DOMAIN = '.example.com'

minidetector's People

Contributors

brosner avatar saschwarz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.