From e689cbfbaa8e304afbdb549a09857dfd1cb9f091 Mon Sep 17 00:00:00 2001 From: GerardPaligot Date: Thu, 26 Nov 2015 23:48:49 +0100 Subject: [PATCH] feat(api): Order private post by position, pubdate and update. --- zds/mp/api/tests.py | 43 +++++++++++++++++++++++++++++++++++++++++++ zds/mp/api/views.py | 6 ++++++ 2 files changed, 49 insertions(+) diff --git a/zds/mp/api/tests.py b/zds/mp/api/tests.py index 5646e9e3c8..2e65a93bbd 100644 --- a/zds/mp/api/tests.py +++ b/zds/mp/api/tests.py @@ -748,6 +748,49 @@ def test_list_of_private_posts_with_x_data_format_markdown(self): self.assertIsNotNone(response.data.get('results')[0].get('text')) self.assertIsNone(response.data.get('results')[0].get('text_html')) + def test_ordering_list_of_private_posts_by_position_in_topic(self): + """ + Gets list of private posts ordered by position_in_topic. + """ + private_topic = PrivateTopicFactory(author=self.profile.user) + self.create_multiple_private_posts_for_member(self.profile.user, private_topic, + settings.REST_FRAMEWORK['PAGINATE_BY']) + + response = self.client.get(reverse('api-mp-message-list', args=[private_topic.id]) + + '?ordering=position_in_topic') + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data.get('count'), settings.REST_FRAMEWORK['PAGINATE_BY']) + self.assertIsNone(response.data.get('next')) + self.assertIsNone(response.data.get('previous')) + + def test_ordering_list_of_private_posts_by_pubdate(self): + """ + Gets list of private posts ordered by pubdate. + """ + private_topic = PrivateTopicFactory(author=self.profile.user) + self.create_multiple_private_posts_for_member(self.profile.user, private_topic, + settings.REST_FRAMEWORK['PAGINATE_BY']) + + response = self.client.get(reverse('api-mp-message-list', args=[private_topic.id]) + '?ordering=pubdate') + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data.get('count'), settings.REST_FRAMEWORK['PAGINATE_BY']) + self.assertIsNone(response.data.get('next')) + self.assertIsNone(response.data.get('previous')) + + def test_ordering_list_of_private_posts_by_update(self): + """ + Gets list of private posts ordered by update. + """ + private_topic = PrivateTopicFactory(author=self.profile.user) + self.create_multiple_private_posts_for_member(self.profile.user, private_topic, + settings.REST_FRAMEWORK['PAGINATE_BY']) + + response = self.client.get(reverse('api-mp-message-list', args=[private_topic.id]) + '?ordering=update') + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.data.get('count'), settings.REST_FRAMEWORK['PAGINATE_BY']) + self.assertIsNone(response.data.get('next')) + self.assertIsNone(response.data.get('previous')) + def create_multiple_private_posts_for_member(self, user, private_topic, number_of_users=settings.REST_FRAMEWORK['PAGINATE_BY']): list = [] diff --git a/zds/mp/api/views.py b/zds/mp/api/views.py index 683381d4ba..43cf860c01 100644 --- a/zds/mp/api/views.py +++ b/zds/mp/api/views.py @@ -41,6 +41,7 @@ class DetailKeyConstructor(DefaultKeyConstructor): class PagingPrivatePostListKeyConstructor(DefaultKeyConstructor): pagination = DJRF3xPaginationKeyBit() + search = bits.QueryParamsKeyBit(['ordering']) list_sql_query = bits.ListSqlQueryKeyBit() unique_view_id = bits.UniqueViewIdKeyBit() @@ -279,6 +280,8 @@ class PrivatePostListAPI(MarkPrivateTopicAsRead, ListCreateAPIView): """ permission_classes = (IsAuthenticated, IsParticipantFromPrivatePost) + filter_backends = (filters.OrderingFilter,) + ordering_fields = ('position_in_topic', 'pubdate', 'update') list_key_func = PagingPrivatePostListKeyConstructor() def dispatch(self, request, *args, **kwargs): @@ -308,6 +311,9 @@ def get(self, request, *args, **kwargs): description: Sets size of the pagination. required: false paramType: query + - name: ordering + description: Applies an order at the list. You can order by (-)position_in_topic, (-)pubdate or (-)update. + paramType: query - name: expand description: Expand a field with an identifier. required: false