Change review list api to include empty reviews by default; (#5685)

This commit is contained in:
Andrew Williamson 2017-06-19 19:32:40 +01:00 коммит произвёл GitHub
Родитель 4379b76e6b
Коммит 5b94db4d7c
3 изменённых файлов: 38 добавлений и 24 удалений

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

@ -26,7 +26,7 @@ user has already posted a review for the current version of an add-on.
.. http:get:: /api/v3/reviews/review/ .. http:get:: /api/v3/reviews/review/
:query string addon: The add-on id to fetch reviews from. When passed, the reviews shown will always be the latest posted by each user on this particular add-on (which means there should only be one review per user in the results), unless the ``version`` parameter is also passed. :query string addon: The add-on id to fetch reviews from. When passed, the reviews shown will always be the latest posted by each user on this particular add-on (which means there should only be one review per user in the results), unless the ``version`` parameter is also passed.
:query string filter: The :ref:`filter <review-filtering-param>` to apply. :query string filter: The :ref:`filter(s) <review-filtering-param>` to apply.
:query string user: The user id to fetch reviews from. :query string user: The user id to fetch reviews from.
:query boolean show_grouped_ratings: Whether or not to show ratings aggregates for this add-on in the response (Use "true"/"1" as truthy values, "0"/"false" as falsy ones). :query boolean show_grouped_ratings: Whether or not to show ratings aggregates for this add-on in the response (Use "true"/"1" as truthy values, "0"/"false" as falsy ones).
:query string version: The version id to fetch reviews from. :query string version: The version id to fetch reviews from.
@ -40,13 +40,20 @@ user has already posted a review for the current version of an add-on.
.. _review-filtering-param: .. _review-filtering-param:
By default, the review list API will only return not-deleted reviews. You By default, the review list API will only return not-deleted reviews, and
can change that with the ``filter=with_deleted`` query parameter, which include reviews without text. You can change that with the ``filter`` query
requires the Addons:Edit permission. parameter. You can filter by multiple values, e.g. ``filter=with_deleted,without_textless``
Only non-empty reviews (reviews with both a rating and text body) are returned, =================== ======================================================
unless a) the reviews were created by the user making the API request or Value Description
b) the user has the Addons:Edit permission. =================== ======================================================
with_deleted Returns deleted reviews too. This requires the
Addons:Edit permission.
without_empty_body Excludes reviews that only contain a rating, and no
textual content.
with_yours Used in combination `without_empty_body` to include
your own reviews, even if they have no text.
=================== ======================================================
------ ------
Detail Detail

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

@ -874,25 +874,28 @@ class TestReviewViewSetGet(TestCase):
create_review() create_review()
create_review() create_review()
create_review(body=None) create_review(body=None)
create_review(body=None)
create_review(body=None, user=self.user) create_review(body=None, user=self.user)
# Don't show the reviews with no body. # Do show the reviews with no body by default
response = self.client.get(self.url, {'addon': self.addon.pk}) response = self.client.get(self.url, {'addon': self.addon.pk})
data = json.loads(response.content) data = json.loads(response.content)
assert data['count'] == 5 == len(data['results'])
self.client.login_api(self.user)
# Unless you filter them out
response = self.client.get(
self.url, {'addon': self.addon.pk, 'filter': 'without_empty_body'})
data = json.loads(response.content)
assert data['count'] == 2 == len(data['results']) assert data['count'] == 2 == len(data['results'])
# Except if it's your review # And maybe you only want your own empty reviews
self.client.login_api(self.user) response = self.client.get(
response = self.client.get(self.url, {'addon': self.addon.pk}) self.url, {'addon': self.addon.pk,
'filter': 'without_empty_body,with_yours'})
data = json.loads(response.content) data = json.loads(response.content)
assert data['count'] == 3 == len(data['results']) assert data['count'] == 3 == len(data['results'])
# Or you're an admin
self.grant_permission(self.user, 'Addons:Edit')
response = self.client.get(self.url, {'addon': self.addon.pk})
data = json.loads(response.content)
assert data['count'] == 4 == len(data['results'])
def test_list_user(self, **kwargs): def test_list_user(self, **kwargs):
self.user = user_factory() self.user = user_factory()
review1 = Review.objects.create( review1 = Review.objects.create(

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

@ -341,7 +341,7 @@ class ReviewViewSet(AddonChildMixin, ModelViewSet):
# because the frontend wants to call this before and after # because the frontend wants to call this before and after
# having posted a new review, and needs accurate results. # having posted a new review, and needs accurate results.
self.pagination_class = OneOrZeroPageNumberPagination self.pagination_class = OneOrZeroPageNumberPagination
return qs return super(ReviewViewSet, self).filter_queryset(qs)
def get_paginated_response(self, data): def get_paginated_response(self, data):
response = super(ReviewViewSet, self).get_paginated_response(data) response = super(ReviewViewSet, self).get_paginated_response(data)
@ -359,7 +359,7 @@ class ReviewViewSet(AddonChildMixin, ModelViewSet):
return response return response
def get_queryset(self): def get_queryset(self):
requested = self.request.GET.get('filter') requested = self.request.GET.get('filter', '').split(',')
has_addons_edit = acl.action_allowed(self.request, has_addons_edit = acl.action_allowed(self.request,
amo.permissions.ADDONS_EDIT) amo.permissions.ADDONS_EDIT)
@ -367,7 +367,7 @@ class ReviewViewSet(AddonChildMixin, ModelViewSet):
# information to the serializer to show/hide delete replies. # information to the serializer to show/hide delete replies.
if not hasattr(self, 'should_access_deleted_reviews'): if not hasattr(self, 'should_access_deleted_reviews'):
self.should_access_deleted_reviews = ( self.should_access_deleted_reviews = (
(requested == 'with_deleted' or self.action != 'list') and ('with_deleted' in requested or self.action != 'list') and
self.request.user.is_authenticated() and self.request.user.is_authenticated() and
has_addons_edit) has_addons_edit)
@ -392,10 +392,14 @@ class ReviewViewSet(AddonChildMixin, ModelViewSet):
else: else:
queryset = Review.objects.all() queryset = Review.objects.all()
# Filter out (other user's) empty reviews for non-admins. # Filter out empty reviews if specified.
if self.action == 'list' and not has_addons_edit: # Should the users own empty reviews be filtered back in?
user_filter = (Q(user=self.request.user.pk) if 'with_yours' in requested and self.request.user.is_authenticated():
if self.request.user.is_authenticated() else Q()) user_filter = Q(user=self.request.user.pk)
else:
user_filter = Q()
# Apply the filter(s)
if 'without_empty_body' in requested:
queryset = queryset.filter(~Q(body=None) | user_filter) queryset = queryset.filter(~Q(body=None) | user_filter)
# The serializer needs reply, version (only the "version" field) and # The serializer needs reply, version (only the "version" field) and