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/
: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 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.
@ -40,13 +40,20 @@ user has already posted a review for the current version of an add-on.
.. _review-filtering-param:
By default, the review list API will only return not-deleted reviews. You
can change that with the ``filter=with_deleted`` query parameter, which
requires the Addons:Edit permission.
By default, the review list API will only return not-deleted reviews, and
include reviews without text. You can change that with the ``filter`` query
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
b) the user has the Addons:Edit permission.
=================== ======================================================
Value Description
=================== ======================================================
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

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

@ -874,25 +874,28 @@ class TestReviewViewSetGet(TestCase):
create_review()
create_review()
create_review(body=None)
create_review(body=None)
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})
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'])
# Except if it's your review
self.client.login_api(self.user)
response = self.client.get(self.url, {'addon': self.addon.pk})
# And maybe you only want your own empty reviews
response = self.client.get(
self.url, {'addon': self.addon.pk,
'filter': 'without_empty_body,with_yours'})
data = json.loads(response.content)
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):
self.user = user_factory()
review1 = Review.objects.create(

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

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