pass tablet through to search queries (bug 795167)

This commit is contained in:
Andy McKay 2012-10-17 15:43:34 -07:00
Родитель 3ac2ed97ac
Коммит edd1a6539c
5 изменённых файлов: 51 добавлений и 71 удалений

Просмотреть файл

@ -68,6 +68,10 @@ $(document).ready(function() {
if (document.cookie && z.capabilities.gaia) {
document.cookie = 'gaia=true;path=/';
}
// Sets a tablet cookie.
if (document.cookie && z.capabilities.tablet) {
document.cookie = 'tablet=true;path=/';
}
stick.basic();
});

Просмотреть файл

@ -149,6 +149,9 @@ def _app_search(request, category=None, browse=None):
qs = qs.filter(uses_flash=False)
query['device'] = 'mobile'
if request.TABLET:
query['device'] = 'tablet'
# Only show premium apps on gaia for now.
# TODO: remove this once we allow app purchases on desktop/android.
if not request.GAIA:

Просмотреть файл

@ -122,8 +122,7 @@ if REGION_STORES:
]
MIDDLEWARE_CLASSES += [
'mkt.site.middleware.VaryOnAJAXMiddleware',
'mkt.site.middleware.MobileDetectionMiddleware',
'mkt.site.middleware.GaiaDetectionMiddleware',
'mkt.site.middleware.DeviceDetectionMiddleware',
'mkt.site.middleware.HijackRedirectMiddleware',
# TODO: Remove this when we remove `request.can_view_consumer`.

Просмотреть файл

@ -247,42 +247,36 @@ class VaryOnAJAXMiddleware(object):
return response
class MobileDetectionMiddleware(object):
"""
If the user has flagged that they are mobile or we were able to detect that
the user is a mobile user, store that the user is a mobile user. If we find
that we've previously stored that the user is on mobile, tell the request
that the user is a mobile user.
"""
class DeviceDetectionMiddleware(object):
"""If the user has flagged that they are on a device. Store the device."""
devices = ['mobile', 'gaia', 'tablet']
def process_request(self, request):
mobile_qs = request.GET.get('mobile', False)
if mobile_qs:
if mobile_qs == 'false':
request.delete_cookie('mobile')
else:
request.set_cookie('mobile', 'true')
for device in self.devices:
qs = request.GET.get(device, False)
cookie = request.COOKIES.get(device, False)
# If the qs is True or there's a cookie set the device. But not if
# the qs is False.
if qs == 'true' or (cookie and not qs == 'false'):
setattr(request, device.upper(), True)
continue
request.MOBILE = (getattr(request, 'MOBILE', False) or
bool(request.COOKIES.get('mobile', False)))
# Otherwise set to False.
setattr(request, device.upper(), False)
def process_response(self, request, response):
for device in self.devices:
active = getattr(request, device.upper(), False)
cookie = request.COOKIES.get(device, False)
class GaiaDetectionMiddleware(object):
"""
Same as above for B2G (gaia) devices. /js/mkt/init.js will set the gaia
cookie to 'true' unless a 'gaia' query parameter overrides it for testing.
"""
if not active and cookie:
# If the device isn't active, but there is a cookie, remove it.
response.delete_cookie(device)
elif active and not cookie:
# Set the device if it's active and there's no cookie.
response.set_cookie(device, 'true')
def process_request(self, request):
gaia_qs = request.GET.get('gaia', False)
if gaia_qs:
if gaia_qs == 'false':
request.delete_cookie('gaia')
elif gaia_qs == 'true':
request.set_cookie('gaia', 'true')
request.GAIA = (getattr(request, 'GAIA', False) or
bool(request.COOKIES.get('gaia', False)))
return response
class HijackRedirectMiddleware(object):

Просмотреть файл

@ -400,8 +400,7 @@ class TestVaryMiddleware(MiddlewareCase):
'mkt.site.middleware.RequestCookiesMiddleware',
'mkt.site.middleware.LocaleMiddleware',
'mkt.site.middleware.RegionMiddleware',
'mkt.site.middleware.MobileDetectionMiddleware',
'mkt.site.middleware.GaiaDetectionMiddleware',
'mkt.site.middleware.DeviceDetectionMiddleware',
])
def test_no_user_agent(self):
# We've toggled the middleware to not rewrite the application and also
@ -417,49 +416,30 @@ class TestVaryMiddleware(MiddlewareCase):
'User-Agent should not be in the "Vary" header.')
class TestMobileMiddleware(amo.tests.TestCase):
class TestDeviceMiddleware(amo.tests.TestCase):
devices = ['mobile', 'gaia']
def test_no_effect(self):
r = self.client.get('/', follow=True)
assert not r.cookies.get('mobile')
assert not r.context['request'].MOBILE
for device in self.devices:
assert not r.cookies.get(device)
assert not getattr(r.context['request'], device.upper())
def test_force_mobile(self):
r = self.client.get('/?mobile=true', follow=True)
eq_(r.cookies['mobile'].value, 'true')
assert r.context['request'].MOBILE
def test_force(self):
for device in self.devices:
r = self.client.get('/?%s=true' % device, follow=True)
eq_(r.cookies[device].value, 'true')
assert getattr(r.context['request'], device.upper())
def test_force_unset_mobile(self):
r = self.client.get('/?mobile=true', follow=True)
assert r.cookies.get('mobile')
def test_force_unset(self):
for device in self.devices:
r = self.client.get('/?%s=true' % device, follow=True)
assert r.cookies.get(device)
r = self.client.get('/?mobile=false', follow=True)
eq_(r.cookies['mobile'].value, '')
assert not r.context['request'].MOBILE
r = self.client.get('/', follow=True)
eq_(r.cookies.get('mobile'), None)
assert not r.context['request'].MOBILE
class TestGaiaMiddleware(amo.tests.TestCase):
def test_force_gaia(self):
r = self.client.get('/?gaia=true', follow=True)
eq_(r.cookies['gaia'].value, 'true')
assert r.context['request'].GAIA
def test_force_unset_gaia(self):
r = self.client.get('/?gaia=true', follow=True)
assert r.cookies.get('gaia')
r = self.client.get('/?gaia=false', follow=True)
eq_(r.cookies['gaia'].value, '')
assert not r.context['request'].GAIA
r = self.client.get('/', follow=True)
eq_(r.cookies.get('gaia'), None)
assert not r.context['request'].GAIA
r = self.client.get('/?%s=false' % device, follow=True)
eq_(r.cookies[device].value, '')
assert not getattr(r.context['request'], device.upper())
class TestHijackRedirectMiddleware(amo.tests.TestCase):