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

Commit

Permalink
Added premium type filter to search API (bug 842693)
Browse files Browse the repository at this point in the history
  • Loading branch information
robhudson committed Feb 22, 2013
1 parent ed22173 commit b88dd11
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
5 changes: 5 additions & 0 deletions apps/constants/base.py
Expand Up @@ -260,7 +260,12 @@
# Non-locale versions for the API.
ADDON_PREMIUM_API = {
ADDON_FREE: 'free',
ADDON_PREMIUM: 'premium',
ADDON_PREMIUM_INAPP: 'premium-inapp',
ADDON_FREE_INAPP: 'free-inapp',
ADDON_OTHER_INAPP: 'other',
}
ADDON_PREMIUM_API_LOOKUP = dict((v, k) for k, v in ADDON_PREMIUM_API.items())

# Apps that require some sort of payment prior to installing.
ADDON_PREMIUMS = (ADDON_PREMIUM, ADDON_PREMIUM_INAPP)
Expand Down
3 changes: 3 additions & 0 deletions docs/topics/api/search.rst
Expand Up @@ -21,6 +21,9 @@ described below:
find the ids of the categories.
* `device` (optional): Filters by supported device. One of 'desktop',
'mobile', 'tablet', or 'gaia'.
* `premium_types` (optional): Filters by whether the app is free or
premium or has in-app purchasing. Any of 'free', 'free-inapp',
'premium', 'premium-inapp', or 'other'.
* `sort` (optional): The field to sort by. One of 'downloads', 'rating',
'price', 'created'. Sorts by relevance by default.

Expand Down
13 changes: 13 additions & 0 deletions mkt/search/forms.py
Expand Up @@ -118,9 +118,22 @@ class ApiSearchForm(forms.Form):
cat = forms.TypedChoiceField(required=False, coerce=int, empty_value=None,
choices=[])
device = forms.ChoiceField(required=False, choices=DEVICE_CHOICES)
premium_types = forms.MultipleChoiceField(
required=False,
choices=[(p, p) for p in amo.ADDON_PREMIUM_API.values()])

def __init__(self, *args, **kw):
super(ApiSearchForm, self).__init__(*args, **kw)
CATS = (Category.objects.filter(type=amo.ADDON_WEBAPP, weight__gte=0)
.values_list('id', flat=True))
self.fields['cat'].choices = [(pk, pk) for pk in CATS]

def clean_premium_types(self):
pt_ids = []
for pt in self.cleaned_data.get('premium_types'):
pt_id = amo.ADDON_PREMIUM_API_LOOKUP.get(pt)
if pt_id is not None:
pt_ids.append(pt_id)
if pt_ids:
return pt_ids
return []
22 changes: 22 additions & 0 deletions mkt/search/tests/test_api.py
Expand Up @@ -86,3 +86,25 @@ def test_device(self):
eq_(res.status_code, 200)
obj = json.loads(res.content)['objects'][0]
eq_(obj['app_slug'], self.webapp.app_slug)

def test_premium_types(self):
res = self.client.get(self.list_url + (
{'premium_types': 'free'},))
eq_(res.status_code, 200)
obj = json.loads(res.content)['objects'][0]
eq_(obj['app_slug'], self.webapp.app_slug)

def test_premium_types_empty(self):
res = self.client.get(self.list_url + (
{'premium_types': 'premium'},))
eq_(res.status_code, 200)
objs = json.loads(res.content)['objects']
eq_(len(objs), 0)

def test_multiple_premium_types(self):
res = self.client.get(self.list_url + (
{'premium_types': 'free'},
{'premium_types': 'premium'}))
eq_(res.status_code, 200)
obj = json.loads(res.content)['objects'][0]
eq_(obj['app_slug'], self.webapp.app_slug)
6 changes: 4 additions & 2 deletions mkt/search/views.py
@@ -1,7 +1,6 @@
from django.shortcuts import redirect

import jingo
import waffle
from tower import ugettext as _

import amo
Expand All @@ -27,7 +26,7 @@ def __init__(self, text, urlparams, selected=False, children=None):
self.null_urlparams['page'] = None


DEFAULT_FILTERS = ['cat', 'price', 'device', 'sort']
DEFAULT_FILTERS = ['cat', 'device', 'premium_types', 'price', 'sort']
DEFAULT_SORTING = {
'popularity': '-popularity',
# TODO: Should popularity replace downloads?
Expand Down Expand Up @@ -59,6 +58,9 @@ def _filter_search(qs, query, filters=None, sorting=None,
qs = qs.filter(premium_type__in=amo.ADDON_FREES, price=0)
if 'device' in show:
qs = qs.filter(device=forms.DEVICE_CHOICES_IDS[query['device']])
if 'premium_types' in show:
if query.get('premium_types'):
qs = qs.filter(premium_type__in=query.get('premium_types'))
if 'sort' in show:
sort_by = None
if query['sort'] in sorting:
Expand Down

0 comments on commit b88dd11

Please sign in to comment.