Skip to content

Commit

Permalink
Support new flags on request by using user-agents package
Browse files Browse the repository at this point in the history
Remove cookies feature in the process.
  • Loading branch information
wodow committed Aug 21, 2014
1 parent 41e925b commit 6b92262
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 49 deletions.
21 changes: 10 additions & 11 deletions flask_mobility/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import re

from flask import _request_ctx_stack as stack

from user_agents import parse as parse_user_agent


class Mobility(object):
def __init__(self, app=None):
Expand All @@ -12,12 +13,6 @@ def __init__(self, app=None):

def init_app(self, app):
self.app = app
app.config.setdefault(
'MOBILE_USER_AGENTS',
'android|fennec|iemobile|iphone|opera (?:mini|mobi)|mobile')
app.config.setdefault('MOBILE_COOKIE', 'mobile')

self.USER_AGENTS = re.compile(app.config.get('MOBILE_USER_AGENTS'))

@app.before_request
def before_request():
Expand All @@ -26,8 +21,12 @@ def before_request():
self.process_request(ctx.request)

def process_request(self, request):
ua = request.user_agent.string.lower()
mc = request.cookies.get(self.app.config.get('MOBILE_COOKIE'))
ua_str = request.user_agent.string.lower()

user_agent = parse_user_agent(ua_str)

request.MOBILE = (mc == 'on' or
(mc != 'off' and self.USER_AGENTS.search(ua)))
request.DESKTOP = user_agent.is_pc
request.MOBILE = user_agent.is_mobile
request.TABLET = user_agent.is_tablet
request.TOUCH_CAPABLE = user_agent.is_touch_capable
request.BOT = user_agent.is_bot
2 changes: 2 additions & 0 deletions flask_mobility/tests/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

ANDROID_BROWSER_UA = "Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19"
28 changes: 4 additions & 24 deletions flask_mobility/tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from flask.ext.mobility import Mobility
from flask.ext.mobility.decorators import mobile_template, mobilized

from constants import ANDROID_BROWSER_UA


class DecoratorsTestCase(unittest.TestCase):

Expand Down Expand Up @@ -36,38 +38,16 @@ def test_mobile_template_user_agent(self):
assert b'template.html' == self.client.get('/').data

# Check with mobile User-Agent header
headers = [('User-Agent', 'android')]
headers = [('User-Agent', ANDROID_BROWSER_UA)]
response = self.client.get('/', headers=headers)
assert b'mobile/template.html' == response.data

def test_mobile_template_cookie(self):
assert b'template.html' == self.client.get('/').data

MOBILE_COOKIE = self.app.config.get('MOBILE_COOKIE')

self.client.set_cookie('localhost', MOBILE_COOKIE, 'on')
assert b'mobile/template.html' == self.client.get('/').data

self.client.set_cookie('localhost', MOBILE_COOKIE, 'off')
assert b'template.html' == self.client.get('/').data

def test_mobilized_user_agent(self):
"""Test the mobilized decorator"""

# Check without mobile User-Agent header
assert b'False' == self.client.get('/mobilize').data

# Check with mobile User-Agent header
headers = [('User-Agent', 'android')]
headers = [('User-Agent', ANDROID_BROWSER_UA)]
assert b'True' == self.client.get('/mobilize', headers=headers).data

def test_mobilized_cookie(self):
assert b'False' == self.client.get('/mobilize').data

MOBILE_COOKIE = self.app.config.get('MOBILE_COOKIE')

self.client.set_cookie('localhost', MOBILE_COOKIE, 'on')
assert b'True' == self.client.get('/mobilize').data

self.client.set_cookie('localhost', MOBILE_COOKIE, 'off')
assert b'False' == self.client.get('/mobilize').data
17 changes: 3 additions & 14 deletions flask_mobility/tests/test_mobility.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from flask import Flask, render_template_string
from flask.ext.mobility import Mobility

from constants import ANDROID_BROWSER_UA


class MobilityTestCase(unittest.TestCase):

Expand All @@ -25,18 +27,5 @@ def test_detect_mobile_user_agent(self):
assert b'False' == self.app.get('/').data

# Check with mobile User-Agent header
headers = [('User-Agent', 'android')]
headers = [('User-Agent', ANDROID_BROWSER_UA)]
assert b'True' == self.app.get('/', headers=headers).data

def test_mobile_cookie(self):
"""Check that the mobile cookie value is respected"""
MOBILE_COOKIE = self.config.get('MOBILE_COOKIE')

# Check cookie is set to 'on'
self.app.set_cookie('localhost', MOBILE_COOKIE, 'on')
assert b'True' == self.app.get('/').data

# Check cookie is set to 'off'
self.app.set_cookie('localhost', MOBILE_COOKIE, 'off')
headers = [('User-Agent', 'android')]
assert b'False' == self.app.get('/', headers=headers).data
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Flask==0.10.1
Jinja2==2.6
user-agents==0.2.0

0 comments on commit 6b92262

Please sign in to comment.