-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move paginate_objects to PaginationMixin.
- Loading branch information
Showing
4 changed files
with
119 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger | ||
|
||
from qurl import qurl | ||
|
||
|
||
class PaginationMixin(object): | ||
|
||
paginate_by = 10 | ||
|
||
def paginate_objects(self, request, objects): | ||
paginator = Paginator(objects, self.paginate_by) | ||
page = request.GET.get('page', 1) | ||
|
||
try: | ||
object_list = paginator.page(page) | ||
except PageNotAnInteger: | ||
object_list = paginator.page(1) | ||
except EmptyPage: | ||
object_list = paginator.page(paginator.num_pages) | ||
|
||
pagination = { | ||
'count': object_list.paginator.count, | ||
'num_pages': object_list.paginator.num_pages, | ||
'previous_page_number': None, | ||
'previous_url': None, | ||
'next_page_number': None, | ||
'next_url': None | ||
} | ||
url = request.get_full_path() | ||
|
||
if object_list.has_next(): | ||
next_page_number = object_list.next_page_number() | ||
pagination['next_page_number'] = next_page_number | ||
pagination['next_url'] = 'http://{0}{1}'.format( | ||
request.get_host(), | ||
qurl( | ||
url, | ||
add={'page': object_list.next_page_number()} | ||
) | ||
) | ||
|
||
if object_list.has_previous(): | ||
previous_page_number = object_list.previous_page_number() | ||
pagination['previous_page_number'] = previous_page_number | ||
pagination['previous_url'] = 'http://{0}{1}'.format( | ||
request.get_host(), | ||
qurl( | ||
url, | ||
add={'page': object_list.previous_page_number()} | ||
) | ||
) | ||
|
||
return object_list, pagination |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
from django.test import TestCase, RequestFactory | ||
from django.contrib.auth.models import AnonymousUser | ||
|
||
import json | ||
import status | ||
|
||
from tiny_rest.views import APIView | ||
from tiny_rest.pagination import PaginationMixin | ||
|
||
|
||
class PaginationAPIView(PaginationMixin, APIView): | ||
|
||
paginate_by = 5 | ||
|
||
def list(self, request, *args, **kwargs): | ||
objects = range(10) | ||
object_list, pagination = self.paginate_objects(request, objects) | ||
data = { | ||
'pagination': pagination, | ||
'data': [number for number in object_list] | ||
} | ||
return self.response(data=data) | ||
|
||
|
||
class TestPaginationAPIView(TestCase): | ||
|
||
def setUp(self): | ||
self.factory = RequestFactory() | ||
|
||
def test_paginate_objects(self): | ||
request = self.factory.get('/') | ||
request.user = AnonymousUser() | ||
response = PaginationAPIView.as_view()(request) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
data = json.loads(response.content.decode()) | ||
self.assertEqual(data['pagination']['count'], 10) | ||
self.assertEqual(data['pagination']['num_pages'], 2) | ||
self.assertFalse(data['pagination']['previous_page_number']) | ||
self.assertFalse(data['pagination']['previous_url']) | ||
self.assertEqual(data['pagination']['next_page_number'], 2) | ||
self.assertEqual( | ||
data['pagination']['next_url'], 'http://testserver/?page=2' | ||
) | ||
self.assertEqual(data['data'], [0, 1, 2, 3, 4]) | ||
|
||
request = self.factory.get('/?page=2') | ||
request.user = AnonymousUser() | ||
response = PaginationAPIView.as_view()(request) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
data = json.loads(response.content.decode()) | ||
self.assertEqual(data['pagination']['count'], 10) | ||
self.assertEqual(data['pagination']['num_pages'], 2) | ||
self.assertEqual(data['pagination']['previous_page_number'], 1) | ||
self.assertEqual( | ||
data['pagination']['previous_url'], 'http://testserver/?page=1' | ||
) | ||
self.assertFalse(data['pagination']['next_page_number']) | ||
self.assertFalse(data['pagination']['next_url']) | ||
self.assertEqual(data['data'], [5, 6, 7, 8, 9]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters