Skip to content

Commit

Permalink
Merge pull request #3211 from GerardPaligot/feat_order_private_post
Browse files Browse the repository at this point in the history
feat(api): Order private post by position, pubdate and update.
  • Loading branch information
Laville Augustin committed Dec 9, 2015
2 parents fa0ba64 + e689cbf commit a7dbdcd
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
43 changes: 43 additions & 0 deletions zds/mp/api/tests.py
Expand Up @@ -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 = []
Expand Down
6 changes: 6 additions & 0 deletions zds/mp/api/views.py
Expand Up @@ -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()

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a7dbdcd

Please sign in to comment.