My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

from useragents import search_strings

class Middleware(object):
@staticmethod
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 request.META.has_key("HTTP_X_OPERAMINI_FEATURES"):
#Then it's running opera mini. 'Nuff said.
#Reference from:
# http://dev.opera.com/articles/view/opera-mini-request-headers/
request.mobile = True
return None

if request.META.has_key("HTTP_ACCEPT"):
s = request.META["HTTP_ACCEPT"].lower()
if 'application/vnd.wap.xhtml+xml' in s:
# Then it's a wap browser
request.mobile = True
return None

if request.META.has_key("HTTP_USER_AGENT"):
# This takes the most processing. Surprisingly enough, when I
# Experimented on my own machine, this was the most efficient
# algorithm. Certainly more so than regexes.
# Also, Caching didn't help much, with real-world caches.
s = request.META["HTTP_USER_AGENT"].lower()
for ua in search_strings:
if ua in s:
request.mobile = True
return None

#Otherwise it's not a mobile
request.mobile = False
return None

def detect_mobile(view):
"""View Decorator that 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"""

def detected(request, *args, **kwargs):
Middleware.process_request(request)
return view(request, *args, **kwargs)
detected.__doc__ = "%s\n[Wrapped by detect_mobile which detects if the request is from a phone]" % view.__doc__
return detected


__all__ = ['Middleware', 'detect_mobile']

Change log

r20 by remi.barraquand on Dec 11, 2010   Diff
* applying patch http://code.google.com/p/
minidetector/issues/detail?id=9&q=label
:Type-Defect
Go to: 
Project members, sign in to write a code review

Older revisions

r13 by metamoof on May 21, 2008   Diff
Fixed #5 where I was performing a
static assignment inside a loop
instead of outside. Thanks
SlightlyThomas!
r9 by metamoof on Dec 6, 2007   Diff
Reformatted some docstrings and added
some comments with sources.
r7 by metamoof on Dec 6, 2007   Diff
Everything's called minidetector now
All revisions of this file

File info

Size: 2104 bytes, 52 lines
Powered by Google Project Hosting