summaryrefslogtreecommitdiff
path: root/lib/mobile_middleware/mobile_detect.py
blob: 6f5b9980798ed15b226ce782ed04d72d11498f2e (plain)
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
from django.http import HttpResponseRedirect
import re
mobile_url = 'http://m.luxagraf.net/'
agents_list = [
    'Nokia','bMOT','^LGE?b','SonyEricsson',
    'Ericsson','BlackBerry','DoCoMo','Symbian',
    'Windows CE','NetFront','Klondike','PalmOS',
    'PalmSource','portalmm','S[CG]H-','bSAGEM',
    'SEC-','jBrowser-WAP','Mitsu','Panasonic-',
    'SAMSUNG-','Samsung-','Sendo','SHARP-',
    'Vodaphone','BenQ','iPAQ','AvantGo',
    'Go.Web','Sanyo-','AUDIOVOX','PG-',
    'CDM[-d]','^KDDI-','^SIE-','TSM[-d]',
    '^KWC-','WAP','^KGT [NC]','iPhone',
]
def is_mobile(user_agent):
    for agent in agents_list:
        if re.search(agent, user_agent):
            return True
    return False

class MobileRedirect(object):
    def process_request(self, request):
        if not request.session.get('checked_ua', False):
            if is_mobile(request.META['HTTP_USER_AGENT']):
                request.session['checked_ua'] = True
                return HttpResponseRedirect(mobile_url)
            else:
                # Make sure it doesn't try this again
                request.session['checked_ua'] = True
        return None