def process_request(self, request): if not getattr(settings, "MOBILE_DOMAIN", False): return # Cookie settings max_age = getattr(settings, "MOBILE_COOKIE_MAX_AGE", DEFAULT_COOKIE_MAX_AGE) expires_time = time.time() + max_age expires = cookie_date(expires_time) # test for browser return if ( # is mobile? is_mobile(request.META.get("HTTP_USER_AGENT", "")) and # but has param m2w? request.GET.get("m2w", False) and # does currently not have a is browser cookie with 1 request.COOKIES.get("isbrowser", "0") == "0" ): """ Set a cookie for Mobile 2 Web if a mobile browser does not want to browse mobile """ response = HttpResponseRedirect(request.META.get("PATH_INFO", "/")) response.set_cookie( "ismobile", "0", domain=settings.SESSION_COOKIE_DOMAIN, max_age=max_age, expires=expires ) response.set_cookie( "isbrowser", "1", domain=settings.SESSION_COOKIE_DOMAIN, max_age=max_age, expires=expires ) return response # test for mobile browser if ( # check for override cookie, do not check if present request.COOKIES.get("ismobile", "0") == "1" or ( # browser info present "HTTP_USER_AGENT" in request.META and # desktop browser override not set request.COOKIES.get("isbrowser", "0") != "1" and # check browser type is_mobile(request.META.get("HTTP_USER_AGENT", "")) and # check whether ipad should be redirected self.redirect_ipad(request.META.get("HTTP_USER_AGENT", "")) ) ): redirect = settings.MOBILE_DOMAIN if getattr(settings, "MOBILE_REDIRECT_PRESERVE_URL", False): redirect = redirect.rstrip("/") + request.path_info # redirect to mobile domain response = HttpResponseRedirect(redirect) response.set_cookie( "ismobile", "1", domain=settings.SESSION_COOKIE_DOMAIN, max_age=max_age, expires=expires ) return response
def process_request(self, request): if not getattr(settings, 'MOBILE_DOMAIN', False): return # Cookie settings max_age = getattr(settings, 'MOBILE_COOKIE_MAX_AGE', DEFAULT_COOKIE_MAX_AGE) expires_time = time.time() + max_age expires = cookie_date(expires_time) # test for browser return if ( # is mobile? is_mobile(request.META.get('HTTP_USER_AGENT', '')) and # but has param m2w? request.GET.get('m2w', False) and # does currently not have a is browser cookie with 1 request.COOKIES.get('isbrowser', '0') == '0' ): ''' Set a cookie for Mobile 2 Web if a mobile browser does not want to browse mobile ''' response = HttpResponseRedirect(request.META.get('PATH_INFO', '/')) response.set_cookie('ismobile', '0', domain=settings.SESSION_COOKIE_DOMAIN, max_age=max_age, expires=expires) response.set_cookie('isbrowser', '1', domain=settings.SESSION_COOKIE_DOMAIN, max_age=max_age, expires=expires) return response # test for mobile browser if ( # check for override cookie, do not check if present request.COOKIES.get('ismobile', '0') == '1' or ( # browser info present 'HTTP_USER_AGENT' in request.META and # desktop browser override not set request.COOKIES.get('isbrowser', '0') != '1' and # check browser type is_mobile(request.META.get('HTTP_USER_AGENT', '')) and # check whether ipad should be redirected self.redirect_ipad(request.META.get('HTTP_USER_AGENT', '')) ) ): redirect = settings.MOBILE_DOMAIN if getattr(settings, 'MOBILE_REDIRECT_PRESERVE_URL', False): redirect = redirect.rstrip('/') + request.path_info # redirect to mobile domain response = HttpResponseRedirect(redirect) response.set_cookie('ismobile', '1', domain=settings.SESSION_COOKIE_DOMAIN, max_age=max_age, expires=expires) return response
def process_request(self, request): if not getattr(settings, 'MOBILE_DOMAIN', False): return # test for mobile browser if ( # check for override cookie, do not check if present request.COOKIES.get('ismobile', '0') == '1' or ( # browser info present 'HTTP_USER_AGENT' in request.META and # desktop browser override not set request.COOKIES.get('isbrowser', '0') != '1' and # check browser type is_mobile(request.META['HTTP_USER_AGENT']) ) ): # redirect to mobile domain response = HttpResponseRedirect(settings.MOBILE_DOMAIN) # set cookie to identify the browser as mobile max_age = getattr(settings, 'MOBILE_COOKIE_MAX_AGE', DEFAULT_COOKIE_MAX_AGE) expires_time = time.time() + max_age expires = cookie_date(expires_time) response.set_cookie('ismobile', '1', domain=settings.SESSION_COOKIE_DOMAIN, max_age=max_age, expires=expires) return response
def process_request(self, request): if not getattr(settings, 'MOBILE_DOMAIN', False): return # test for mobile browser if ( # check for override cookie, do not check if present request.COOKIES.get('ismobile', '0') == '1' or ( # browser info present 'HTTP_USER_AGENT' in request.META and # desktop browser override not set request.COOKIES.get('isbrowser', '0') != '1' and # check browser type is_mobile(request.META['HTTP_USER_AGENT']))): redirect = settings.MOBILE_DOMAIN if getattr(settings, 'MOBILE_REDIRECT_PRESERVE_URL', False): redirect = redirect.rstrip('/') + request.path_info # redirect to mobile domain response = HttpResponseRedirect(redirect) # set cookie to identify the browser as mobile max_age = getattr(settings, 'MOBILE_COOKIE_MAX_AGE', DEFAULT_COOKIE_MAX_AGE) expires_time = time.time() + max_age expires = cookie_date(expires_time) response.set_cookie('ismobile', '1', domain=settings.SESSION_COOKIE_DOMAIN, max_age=max_age, expires=expires) return response
def process_request(self, request): if not getattr(settings, "MOBILE_DOMAIN", False) or request.GET.get("_isbrowser", False): return # test for mobile browser if ( # check for override cookie, do not check if present request.COOKIES.get("ismobile", "0") == "1" or ( # browser info present "HTTP_USER_AGENT" in request.META and # desktop browser override not set request.COOKIES.get("isbrowser", "0") != "1" and # check browser type is_mobile(request.META["HTTP_USER_AGENT"]) ) ): redirect = settings.MOBILE_DOMAIN if getattr(settings, "MOBILE_REDIRECT_PRESERVE_URL", False): redirect = redirect.rstrip("/") + request.path_info # redirect to mobile domain response = HttpResponseRedirect(redirect) # set cookie to identify the browser as mobile max_age = getattr(settings, "MOBILE_COOKIE_MAX_AGE", DEFAULT_COOKIE_MAX_AGE) expires_time = time.time() + max_age expires = cookie_date(expires_time) response.set_cookie( "ismobile", "1", domain=settings.SESSION_COOKIE_DOMAIN, max_age=max_age, expires=expires ) return response
def test_identify_known_mobile_browsers(self): fails = [] for m in self.mobile: if not is_mobile(m): fails.append(m) self.assert_equals([], fails)
def test_identify_known_desktop_browsers(self): fails = [] for m in self.desktop: if is_mobile(m): fails.append(m) self.assert_equals([], fails)
def test_returns_false_for_unknown_browser(self): self.assert_false(is_mobile('Unknown'))
def test_returns_false_for_empty_user_agent(self): self.assert_false(is_mobile(''))