Skip to content
This repository has been archived by the owner on Mar 15, 2018. It is now read-only.

Commit

Permalink
remove paid apps from home/search
Browse files Browse the repository at this point in the history
  • Loading branch information
spasovski committed Oct 2, 2012
1 parent 98b2bfd commit 03e458b
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 18 deletions.
3 changes: 2 additions & 1 deletion apps/search/views.py
Expand Up @@ -385,13 +385,14 @@ class WebappSuggestionsAjax(SearchSuggestionsAjax):

def __init__(self, request, excluded_ids=(), category=None):
self.category = category
self.gaia = request.GAIA
SearchSuggestionsAjax.__init__(self, request, excluded_ids)

def queryset(self):
res = SearchSuggestionsAjax.queryset(self)
if self.category:
res = res.filter(category__in=[self.category])
if waffle.switch_is_active('disabled-payments'):
if waffle.switch_is_active('disabled-payments') or not self.gaia:
res = res.filter(premium_type__in=amo.ADDON_FREES, price=0)

region = getattr(self.request, 'REGION', mkt.regions.WORLDWIDE)
Expand Down
7 changes: 6 additions & 1 deletion media/js/mkt/init.js
Expand Up @@ -66,6 +66,12 @@ $(document).ready(function() {
pre_auth: data_user.pre_auth
});

// Set cookie if user is on B2G.
// TODO: remove this once we allow purchases on desktop/android.
if (document.cookie && z.capabilities.gaia) {
document.cookie = 'gaia=true;path=/';
}

stick.basic();
});

Expand Down Expand Up @@ -129,7 +135,6 @@ z.page.on('fragmentloaded', function() {
// Yea...
var sortoption = z.getVars(location.href);

// This will not scale if we have more than two.
$('#filter-sort li a').removeClass('sel');
switch(sortoption.sort) {
case 'None':
Expand Down
6 changes: 4 additions & 2 deletions mkt/home/views.py
Expand Up @@ -19,8 +19,10 @@ def home(request):
return jingo.render(request, 'home/home_walled.html')
region = getattr(request, 'REGION', mkt.regions.WORLDWIDE)
featured = Webapp.featured(region=region, cat=None)[:9]
popular = _add_mobile_filter(request, Webapp.popular(region=region))[:10]
latest = _add_mobile_filter(request, Webapp.latest(region=region))[:10]
popular = _add_mobile_filter(request,
Webapp.popular(region=region, gaia=request.GAIA))[:10]
latest = _add_mobile_filter(request,
Webapp.latest(region=region, gaia=request.GAIA))[:10]
return jingo.render(request, 'home/home.html', {
'featured': featured,
})
2 changes: 1 addition & 1 deletion mkt/search/api.py
Expand Up @@ -32,7 +32,7 @@ def get_list(self, request=None, **kwargs):

# Search specific processing of the results.
region = getattr(request, 'REGION', mkt.regions.WORLDWIDE)
qs = _get_query(region)
qs = _get_query(region, request.GAIA)
qs = _filter_search(qs, form.cleaned_data, region=region)
res = amo.utils.paginate(request, qs)

Expand Down
2 changes: 1 addition & 1 deletion mkt/search/templates/search/results.html
Expand Up @@ -46,7 +46,7 @@ <h1>{{ _('Filter') }}</h1>
<div class="filters-body">
<form action="{{ url('search.search') }}" method="get">
<div>
{% if prices and not waffle.switch('disabled-payments') %}
{% if prices and not waffle.switch('disabled-payments') and request.GAIA %}
<h2>{{ _('Filter by price') }}</h2>
<ul class="toggles c" id="filter-prices">
{% for price in prices %}
Expand Down
55 changes: 55 additions & 0 deletions mkt/search/tests/test_views.py
Expand Up @@ -540,3 +540,58 @@ def test_mobile_no_flash(self):
r = self.client.get(urlparams(reverse('search.search'), q='Basta'))
eq_(r.status_code, 200)
eq_(list(r.context['pager'].object_list), [self.webapp])


class TestFilterGaiaCompat(amo.tests.ESTestCase):
"""
Test that premium apps outside of B2G(gaia) are hidden from any listings.
"""

def setUp(self):
self.app_name = 'Basta Pasta'
self.webapp = Webapp.objects.create(name=self.app_name,
type=amo.ADDON_WEBAPP,
status=amo.STATUS_PUBLIC)
self.make_premium(self.webapp)
self.reindex(Webapp)

@nottest
def test_url(self, url, app_is_premium=True, device_is_gaia=False):
"""
Test a view to make sure that it excludes premium apps from non gaia
devices.
"""
url = urlparams(url, gaia='true' if device_is_gaia else 'false')

self.refresh()
r = self.client.get(url, follow=True)
eq_(r.status_code, 200)

# If the app is premium and the device isn't gaia, assert
# that the app doesn't show up in the listing.
if app_is_premium and not device_is_gaia:
assert self.app_name not in r.content, (
'Found premium app in non-gaia for %s' % url)
elif app_is_premium and device_is_gaia:
# Otherwise assert that it does.
assert self.app_name in r.content, (
"Couldn't find premium app in gaia for %s" % url)

def _generate(self):
views = [reverse('browse.apps'),
reverse('search.search') + '?q=',
reverse('search.search') + '?q=Basta',
reverse('search.suggestions') + '?q=Basta&cat=apps']

for view in views:

for app_is_premium in (True, False):
for device_is_gaia in (False, True):
yield self.test_url, view, app_is_premium, device_is_gaia

def test_generator(self):
# This is necessary until we can get test generator methods worked out
# to run properly.
for test_params in self._generate():
func, params = test_params[0], test_params[1:]
func(*params)
12 changes: 8 additions & 4 deletions mkt/search/views.py
Expand Up @@ -7,7 +7,6 @@
import amo
import amo.utils
from amo.decorators import json_view
from amo.urlresolvers import reverse
from apps.addons.models import Category
from apps.search.views import name_query, WebappSuggestionsAjax

Expand Down Expand Up @@ -127,8 +126,8 @@ def sort_sidebar(query, form):
for key, text in form.fields['sort'].choices]


def _get_query(region):
return Webapp.from_search(region=region).facet('category')
def _get_query(region, gaia):
return Webapp.from_search(region=region, gaia=gaia).facet('category')


def _app_search(request, category=None, browse=None):
Expand All @@ -144,12 +143,17 @@ def _app_search(request, category=None, browse=None):

region = getattr(request, 'REGION', mkt.regions.WORLDWIDE)

qs = _get_query(region)
qs = _get_query(region, request.GAIA)
# On mobile, always only show mobile apps. Bug 767620
if request.MOBILE:
qs = qs.filter(uses_flash=False)
query['device'] = 'mobile'

# Only show premium apps on gaia for now.
# TODO: remove this once we allow app purchases on desktop/android.
if not request.GAIA:
qs = qs.filter(premium_type__in=amo.ADDON_FREES)

qs = _filter_search(qs, query, region=region)

# If we're mobile, leave no witnesses. (i.e.: hide "Applied Filters:
Expand Down
1 change: 1 addition & 0 deletions mkt/settings.py
Expand Up @@ -123,6 +123,7 @@
MIDDLEWARE_CLASSES += [
'mkt.site.middleware.VaryOnAJAXMiddleware',
'mkt.site.middleware.MobileDetectionMiddleware',
'mkt.site.middleware.GaiaDetectionMiddleware',

# TODO: Remove this when we remove `request.can_view_consumer`.
'amo.middleware.DefaultConsumerMiddleware',
Expand Down
18 changes: 18 additions & 0 deletions mkt/site/middleware.py
Expand Up @@ -262,3 +262,21 @@ def process_request(self, request):

request.MOBILE = (getattr(request, 'MOBILE', False) or
bool(request.COOKIES.get('mobile', 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.
"""

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)))
21 changes: 21 additions & 0 deletions mkt/site/tests/test_middleware.py
Expand Up @@ -399,6 +399,7 @@ def test_no_vary_cookie(self):
'mkt.site.middleware.LocaleMiddleware',
'mkt.site.middleware.RegionMiddleware',
'mkt.site.middleware.MobileDetectionMiddleware',
'mkt.site.middleware.GaiaDetectionMiddleware',
])
def test_no_user_agent(self):
# We've toggled the middleware to not rewrite the application and also
Expand Down Expand Up @@ -439,6 +440,26 @@ def test_force_unset_mobile(self):
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


def normal_view(request):
return ''

Expand Down
16 changes: 8 additions & 8 deletions mkt/webapps/models.py
Expand Up @@ -491,7 +491,7 @@ def now(cls):
return datetime.date.today()

@classmethod
def featured(cls, cat=None, region=None, limit=6):
def featured(cls, cat=None, region=None, limit=6, gaia=False):
FeaturedApp = models.get_model('zadmin', 'FeaturedApp')
qs = (FeaturedApp.objects
.filter(app__status=amo.STATUS_PUBLIC,
Expand All @@ -502,7 +502,7 @@ def featured(cls, cat=None, region=None, limit=6):
qs = (qs.filter(end_date__gte=cls.now())
| qs.filter(end_date__isnull=True))

if waffle.switch_is_active('disabled-payments'):
if waffle.switch_is_active('disabled-payments') or not gaia:
qs = qs.filter(app__premium_type__in=amo.ADDON_FREES)

if isinstance(cat, list):
Expand Down Expand Up @@ -544,7 +544,7 @@ def get_excluded_in(cls, region):
.values_list('addon', flat=True))

@classmethod
def from_search(cls, cat=None, region=None):
def from_search(cls, cat=None, region=None, gaia=False):
filters = dict(type=amo.ADDON_WEBAPP,
status=amo.STATUS_PUBLIC,
is_disabled=False)
Expand All @@ -558,20 +558,20 @@ def from_search(cls, cat=None, region=None):
if excluded:
srch = srch.filter(~F(id__in=excluded))

if waffle.switch_is_active('disabled-payments'):
if waffle.switch_is_active('disabled-payments') or not gaia:
srch = srch.filter(premium_type__in=amo.ADDON_FREES, price=0)

return srch

@classmethod
def popular(cls, cat=None, region=None):
def popular(cls, cat=None, region=None, gaia=False):
"""Elastically grab the most popular apps."""
return cls.from_search(cat, region).order_by('-popularity')
return cls.from_search(cat, region, gaia=gaia).order_by('-popularity')

@classmethod
def latest(cls, cat=None, region=None):
def latest(cls, cat=None, region=None, gaia=False):
"""Elastically grab the most recent apps."""
return cls.from_search(cat, region).order_by('-created')
return cls.from_search(cat, region, gaia=gaia).order_by('-created')

@classmethod
def category(cls, slug):
Expand Down

0 comments on commit 03e458b

Please sign in to comment.