Skip to content

Commit

Permalink
Merge pull request mozilla-services#1067 from chuckharmston/901710-ap…
Browse files Browse the repository at this point in the history
…i_fireplace_featured

Add Collections to Fireplace featured API endpoint (bug 901710)
  • Loading branch information
chuckharmston committed Aug 29, 2013
2 parents 6fd9656 + a402567 commit da72cce
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
3 changes: 3 additions & 0 deletions docs/api/topics/search.rst
Expand Up @@ -85,6 +85,9 @@ Featured App Listing
:param objects: A :ref:`listing <objects-response-label>` of
:ref:`apps <app-response-label>` satisfying the search parameters.
:type objects: array
:param collections: A list of collections for the requested
category/region/carrier set, if any
:type collections: array
:param featured: A list of :ref:`apps <app-response-label>` featured
for the requested category, if any
:type featured: array
Expand Down
2 changes: 1 addition & 1 deletion mkt/collections/filters.py
Expand Up @@ -88,8 +88,8 @@ def qs(self):
qs = super(CollectionFilterSetWithFallback, self).qs
# FIXME: being able to return self.form.errors would greatly help
# debugging.

next_fallback = self.next_fallback()

if next_fallback and not qs.exists():
# FIXME: add current filter set to API-Filter in response. It
# should be possible to implement using <filtersetinstance>.data.
Expand Down
2 changes: 1 addition & 1 deletion mkt/fireplace/urls.py
@@ -1,8 +1,8 @@
from django.conf.urls import include, patterns, url

from tastypie.api import Api
from mkt.fireplace.api import AppResource

from mkt.fireplace.api import AppResource
from mkt.ratings.resources import FireplaceRatingResource
from mkt.search.api import WithFeaturedResource

Expand Down
21 changes: 21 additions & 0 deletions mkt/search/api.py
@@ -1,5 +1,6 @@
from django.conf.urls import url

import waffle
from tastypie.authorization import ReadOnlyAuthorization
from tastypie.throttle import BaseThrottle
from tastypie.utils import trailing_slash
Expand All @@ -12,6 +13,10 @@
from mkt.api.base import CORSResource, MarketplaceResource
from mkt.api.resources import AppResource
from mkt.api.serializers import SuggestionsSerializer
from mkt.collections.constants import COLLECTIONS_TYPE_BASIC
from mkt.collections.filters import CollectionFilterSetWithFallback
from mkt.collections.models import Collection
from mkt.collections.serializers import CollectionSerializer
from mkt.constants.features import FeatureProfile
from mkt.search.views import _filter_search
from mkt.search.forms import ApiSearchForm
Expand Down Expand Up @@ -134,6 +139,16 @@ class Meta(SearchResource.Meta):
resource_name = 'search/featured'
slug_lookup = None

def collections(self, request, collection_type=None):
filters = request.GET.dict()
if collection_type is not None:
qs = Collection.objects.filter(collection_type=collection_type)
else:
qs = Collection.objects.all()
filterset = CollectionFilterSetWithFallback(filters, queryset=qs)
serializer = CollectionSerializer(filterset)
return serializer.data

def alter_list_data_to_serialize(self, request, data):
form_data = self.get_search_data(request)
region = getattr(request, 'REGION', mkt.regions.WORLDWIDE)
Expand All @@ -149,8 +164,14 @@ def alter_list_data_to_serialize(self, request, data):
bundles = [self.build_bundle(obj=obj, request=request) for obj in qs]
data['featured'] = [AppResource().full_dehydrate(bundle)
for bundle in bundles]

if waffle.switch_is_active('rocketfuel'):
data['collections'] = self.collections(request,
collection_type=COLLECTIONS_TYPE_BASIC)

# Alter the _view_name so that statsd logs seperately from search.
request._view_name = 'featured'

return data


Expand Down
48 changes: 45 additions & 3 deletions mkt/search/tests/test_api.py
Expand Up @@ -7,6 +7,7 @@
from nose.tools import eq_, ok_

import amo
import mkt
import mkt.regions
from addons.models import (AddonCategory, AddonDeviceType, AddonUpsell,
Category)
Expand All @@ -19,6 +20,9 @@

from mkt.api.base import list_url
from mkt.api.tests.test_oauth import BaseOAuth, OAuthClient
from mkt.collections.constants import (COLLECTIONS_TYPE_BASIC,
COLLECTIONS_TYPE_FEATURED)
from mkt.collections.models import Collection
from mkt.constants.features import FeatureProfile
from mkt.search.forms import DEVICE_CHOICES_IDS
from mkt.site.fixtures import fixture
Expand Down Expand Up @@ -477,12 +481,12 @@ def test_non_matching_profile_desktop_no_category(self):
eq_(int(res.json['featured'][0]['id']), self.app.pk)


class TestFeaturedWithCategories(BaseOAuth, ESTestCase):
class BaseFeaturedTests(BaseOAuth, ESTestCase):
fixtures = fixture('user_2519', 'webapp_337141')
list_url = list_url('search/featured')

def setUp(self):
super(TestFeaturedWithCategories, self).setUp(api_name='fireplace')
def setUp(self, api_name=None):
super(BaseFeaturedTests, self).setUp(api_name='fireplace')
self.create_switch('buchets')
self.cat = Category.objects.create(type=amo.ADDON_WEBAPP, slug='shiny')
self.app = Webapp.objects.get(pk=337141)
Expand All @@ -494,6 +498,9 @@ def setUp(self):
sms=True).to_signature()
self.qs = {'cat': 'shiny', 'pro': self.profile, 'dev': 'firefoxos'}


class TestFeaturedWithCategories(BaseFeaturedTests):

def test_featured_plus_category(self):
app2 = amo.tests.app_factory()
AddonCategory.objects.get_or_create(addon=app2, category=self.cat)
Expand Down Expand Up @@ -550,6 +557,41 @@ def test_non_matching_profile_desktop_with_category(self):
eq_(int(res.json['featured'][0]['id']), self.app.pk)


class TestFeaturedWithCollections(BaseFeaturedTests):
"""
Tests to ensure that CollectionFilterSetWithFallback is being called and
its results are being added to the response. We'll rely on that class'
tests to ensure that it is properly functioning.
"""

def setUp(self):
self.create_switch('rocketfuel')
super(TestFeaturedWithCollections, self).setUp()
self.col = Collection.objects.create(name='Hi', description='Mom',
collection_type=COLLECTIONS_TYPE_BASIC, category=self.cat)

def get_featured(self):
res = self.client.get(self.list_url, self.qs)
eq_(res.status_code, 200)
return res, res.json

def test_collection_results_added(self):
res, json = self.get_featured()
ok_('collections' in res.json.keys())
eq_(len(res.json['collections']), 1)
eq_(res.json['collections'][0]['id'], self.col.id)

def test_collection_only_basics(self):
self.col2 = Collection.objects.create(name='Bye', description='Dad',
collection_type=COLLECTIONS_TYPE_FEATURED, category=self.cat)
self.test_collection_results_added()

@patch('mkt.search.api.CollectionFilterSetWithFallback')
def test_collection_filterset_called(self, mock_filterset):
res, json = self.get_featured()
eq_(mock_filterset.call_count, 1)


@patch.object(settings, 'SITE_URL', 'http://testserver')
class TestSuggestionsApi(ESTestCase):
fixtures = fixture('webapp_337141')
Expand Down

0 comments on commit da72cce

Please sign in to comment.