Add API endpoint returning consumer information (bug 964524)
It returns the region, and if the user is logged in, the list of apps ids he has installed, developed and purchased.
This commit is contained in:
Родитель
8526841a26
Коммит
08a5053f2c
|
@ -59,3 +59,25 @@ Error reporter
|
|||
**Response**
|
||||
|
||||
:status 204: Message sent.
|
||||
|
||||
Consumer Information
|
||||
====================
|
||||
|
||||
.. http:get:: /api/v1/fireplace/consumer-info/
|
||||
|
||||
Return information about the client making the request.
|
||||
|
||||
**Response**
|
||||
|
||||
:param region: The region slug for this client.
|
||||
:type region: string
|
||||
|
||||
If user authentication information is passed to the request, the following
|
||||
will also be added to the response:
|
||||
|
||||
:param developed: IDs of apps the user has developed.
|
||||
:type active: array
|
||||
:param installed: IDs of apps the user has installed.
|
||||
:type active: array
|
||||
:param purchased: IDs of apps the user has purchased.
|
||||
:type active: array
|
||||
|
|
|
@ -1,3 +1,15 @@
|
|||
import json
|
||||
|
||||
from django.http import HttpResponse
|
||||
from rest_framework.generics import RetrieveAPIView
|
||||
from rest_framework.permissions import AllowAny
|
||||
|
||||
import amo
|
||||
|
||||
from mkt.api.base import CORSMixin
|
||||
from mkt.api.authentication import (RestAnonymousAuthentication,
|
||||
RestOAuthAuthentication,
|
||||
RestSharedSecretAuthentication)
|
||||
from mkt.search.api import FeaturedSearchView as BaseFeaturedSearchView
|
||||
from mkt.search.serializers import SimpleESAppSerializer
|
||||
from mkt.webapps.api import SimpleAppSerializer, AppViewSet as BaseAppViewset
|
||||
|
@ -27,3 +39,29 @@ class AppViewSet(BaseAppViewset):
|
|||
|
||||
class FeaturedSearchView(BaseFeaturedSearchView):
|
||||
serializer_class = FireplaceESAppSerializer
|
||||
|
||||
|
||||
class ConsumerInfoView(CORSMixin, RetrieveAPIView):
|
||||
authentication_classes = [RestOAuthAuthentication,
|
||||
RestSharedSecretAuthentication,
|
||||
RestAnonymousAuthentication]
|
||||
cors_allowed_methods = ['get']
|
||||
permission_classes = (AllowAny,)
|
||||
|
||||
def retrieve(self, request, *args, **kwargs):
|
||||
data = {
|
||||
'region': request.REGION.slug
|
||||
}
|
||||
if request.amo_user:
|
||||
user = request.amo_user
|
||||
# FIXME: values_list() doesn't appear to be cached by cachemachine,
|
||||
# is that going to be a problem ?
|
||||
data['developed'] = list(user.addonuser_set.filter(
|
||||
role=amo.AUTHOR_ROLE_OWNER).values_list('addon_id', flat=True))
|
||||
data['installed'] = list(user.installed_set.values_list('addon_id',
|
||||
flat=True))
|
||||
data['purchased'] = list(user.purchase_ids())
|
||||
|
||||
# Return an HttpResponse directly to be as fast as possible.
|
||||
return HttpResponse(json.dumps(data),
|
||||
content_type='application/json; charset=utf-8')
|
||||
|
|
|
@ -1,14 +1,22 @@
|
|||
import json
|
||||
|
||||
from mock import patch
|
||||
from nose.tools import eq_, ok_
|
||||
from test_utils import RequestFactory
|
||||
|
||||
from amo.tests import ESTestCase
|
||||
from addons.models import AddonUser
|
||||
from amo.tests import app_factory, ESTestCase, TestCase
|
||||
from amo.urlresolvers import reverse
|
||||
from users.models import UserProfile
|
||||
|
||||
import mkt
|
||||
from mkt.api.tests import BaseAPI
|
||||
from mkt.api.tests.test_oauth import RestOAuth
|
||||
from mkt.fireplace.api import FireplaceAppSerializer
|
||||
from mkt.webapps.models import Webapp
|
||||
from mkt.site.fixtures import fixture
|
||||
from mkt.webapps.models import Installed
|
||||
|
||||
|
||||
# https://bugzilla.mozilla.org/show_bug.cgi?id=958608#c1 and #c2.
|
||||
FIREPLACE_EXCLUDED_FIELDS = (
|
||||
|
@ -65,3 +73,63 @@ class TestFeaturedSearchView(ESTestCase):
|
|||
ok_(not field in data, field)
|
||||
for field in FireplaceAppSerializer.Meta.fields:
|
||||
ok_(field in data, field)
|
||||
|
||||
|
||||
class TestConsumerInfoView(RestOAuth, TestCase):
|
||||
fixtures = fixture('user_2519')
|
||||
|
||||
def setUp(self):
|
||||
super(TestConsumerInfoView, self).setUp()
|
||||
self.request = RequestFactory().get('/')
|
||||
self.url = reverse('fireplace-consumer-info')
|
||||
self.user = UserProfile.objects.get(pk=2519)
|
||||
|
||||
@patch('mkt.regions.middleware.RegionMiddleware.region_from_request')
|
||||
def test_no_user_just_region(self, region_from_request):
|
||||
region_from_request.return_value = mkt.regions.UK.slug
|
||||
res = self.anon.get(self.url)
|
||||
data = json.loads(res.content)
|
||||
eq_(data['region'], 'uk')
|
||||
ok_(not 'developed' in data)
|
||||
ok_(not 'installed' in data)
|
||||
ok_(not 'purchased' in data)
|
||||
|
||||
@patch('mkt.regions.middleware.RegionMiddleware.region_from_request')
|
||||
def test_with_user_developed(self, region_from_request):
|
||||
region_from_request.return_value = mkt.regions.BR.slug
|
||||
developed_app = app_factory()
|
||||
AddonUser.objects.create(user=self.user, addon=developed_app)
|
||||
self.client.login(username=self.user.email, password='password')
|
||||
res = self.client.get(self.url)
|
||||
data = json.loads(res.content)
|
||||
eq_(data['region'], 'br')
|
||||
eq_(data['installed'], [])
|
||||
eq_(data['developed'], [developed_app.pk])
|
||||
eq_(data['purchased'], [])
|
||||
|
||||
@patch('mkt.regions.middleware.RegionMiddleware.region_from_request')
|
||||
def test_with_user_installed(self, region_from_request):
|
||||
region_from_request.return_value = mkt.regions.BR.slug
|
||||
installed_app = app_factory()
|
||||
Installed.objects.create(user=self.user, addon=installed_app)
|
||||
self.client.login(username=self.user.email, password='password')
|
||||
res = self.client.get(self.url)
|
||||
data = json.loads(res.content)
|
||||
eq_(data['region'], 'br')
|
||||
eq_(data['installed'], [installed_app.pk])
|
||||
eq_(data['developed'], [])
|
||||
eq_(data['purchased'], [])
|
||||
|
||||
@patch('users.models.UserProfile.purchase_ids')
|
||||
@patch('mkt.regions.middleware.RegionMiddleware.region_from_request')
|
||||
def test_with_user_purchased(self, region_from_request, purchase_ids):
|
||||
region_from_request.return_value = mkt.regions.BR.slug
|
||||
purchased_app = app_factory()
|
||||
purchase_ids.return_value = [purchased_app.pk]
|
||||
self.client.login(username=self.user.email, password='password')
|
||||
res = self.client.get(self.url)
|
||||
data = json.loads(res.content)
|
||||
eq_(data['region'], 'br')
|
||||
eq_(data['installed'], [])
|
||||
eq_(data['developed'], [])
|
||||
eq_(data['purchased'], [purchased_app.pk])
|
||||
|
|
|
@ -2,13 +2,16 @@ from django.conf.urls import include, patterns, url
|
|||
|
||||
from rest_framework.routers import SimpleRouter
|
||||
|
||||
from mkt.fireplace.api import AppViewSet, FeaturedSearchView
|
||||
from mkt.fireplace.api import AppViewSet, ConsumerInfoView, FeaturedSearchView
|
||||
|
||||
apps = SimpleRouter()
|
||||
apps.register(r'app', AppViewSet, base_name='fireplace-app')
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^fireplace/', include(apps.urls)),
|
||||
url(r'^fireplace/consumer-info/',
|
||||
ConsumerInfoView.as_view(),
|
||||
name='fireplace-consumer-info'),
|
||||
url(r'^fireplace/search/featured/',
|
||||
FeaturedSearchView.as_view(),
|
||||
name='fireplace-featured-search-api'),
|
||||
|
|
Загрузка…
Ссылка в новой задаче