Created
January 17, 2011 23:17
-
-
Save 0x46616c6b/783684 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
class BrowserDetectionMiddleware(object): | |
""" | |
Middleware to detect a wap, mobile html or full compatible browser | |
""" | |
def process_request(self, request): | |
browser_type = None | |
if (request.META.has_key('HTTP_ACCEPT')): | |
http_accept = request.META['HTTP_ACCEPT'] | |
# normal browser doesn't accept wml to render, so we can split this case | |
if 'wml' in http_accept: | |
browser_type = 'wap' | |
else: | |
user_agent = request.META['HTTP_USER_AGENT'].lower() | |
pattern = "(mobile|iphone|android|symbian|blackberry|rim)" | |
prog = re.compile(pattern, re.IGNORECASE) | |
match = prog.search(user_agent) | |
if match: | |
browser_type = 'mobile' | |
else: | |
browser_type = 'full' | |
request.browser_type = browser_type |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment