add discovery=true filter to discovery editoral endpoint

This commit is contained in:
Andrew Williamson 2019-07-18 14:24:56 +01:00
Родитель 1d580f6126
Коммит 8483d30f7c
3 изменённых файлов: 32 добавлений и 1 удалений

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

@ -54,10 +54,12 @@ Editorial Content
This endpoint allows you to fetch all editorial content for Discovery Pane
Recommendations. This is used internally to generate .po files containing the
strings the content team came up with.
strings defined by the content team. It is also used by TAAR service to obtain a list
of appropriate add-ons to recommended.
.. http:get:: /api/v4/discovery/editorial/
:query boolean recommended: Filter to only add-ons recommended by Mozilla. Only ``recommended=true`` is supported.
:>json array results: The array containing the results for this query. There is no pagination, all results are returned.
:>json object results[].addon: A :ref:`add-on <addon-detail-object>` object for this item, but only containing one field: ``guid``.
:>json string|null results[].custom_heading: The custom heading for this item, if any.

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

@ -379,3 +379,24 @@ class TestDiscoveryItemViewSet(TestCase):
assert result['custom_description'] == (
u'This time with a custom description')
assert result['addon'] == {'guid': self.items[2].addon.guid}
def test_recommended(self):
with self.assertNumQueries(1):
response = self.client.get(self.url + '?recommended=true')
assert response.status_code == 200
assert len(response.data['results']) == 0
self.items[0].update(recommendable=True)
self.items[0].addon.current_version.update(
recommendation_approved=True)
self.items[2].update(recommendable=True)
self.items[2].addon.current_version.update(
recommendation_approved=True)
with self.assertNumQueries(1):
response = self.client.get(self.url + '?recommended=true')
assert response.status_code == 200
assert len(response.data['results']) == 2
assert response.data['results'][0]['addon']['guid'] == (
self.items[0].addon.guid)
assert response.data['results'][1]['addon']['guid'] == (
self.items[2].addon.guid)

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

@ -87,6 +87,14 @@ class DiscoveryItemViewSet(ListModelMixin, GenericViewSet):
'addon').order_by('pk')
serializer_class = DiscoveryEditorialContentSerializer
def filter_queryset(self, qs):
qs = super().filter_queryset(qs)
if self.request.query_params.get('recommended', False) == 'true':
qs = qs.filter(**{
'recommendable': True,
'addon___current_version__recommendation_approved': True})
return qs
def list(self, request, *args, **kwargs):
# Ignore pagination (fetch all items!) but do wrap the data in a
# `results` property to mimic what the rest of our APIs do.