Skip to content

Commit

Permalink
Simple pagination via limit/offset in place.
Browse files Browse the repository at this point in the history
  • Loading branch information
toastdriven committed Apr 7, 2010
1 parent 75664b8 commit 7c41e88
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
21 changes: 19 additions & 2 deletions tastypie/resources.py
Expand Up @@ -212,13 +212,30 @@ def get_list(self, request):
"""
Should return a HttpResponse (200 OK).
"""
try:
offset = int(request.GET.get('offset', 0))

if offset < 0:
return HttpBadRequest("The starting offset must be >= 0.")
except ValueError:
return HttpBadRequest("The starting offset must be an integer.")

try:
limit = int(request.GET.get('limit', self.limit))

if limit < 0:
return HttpBadRequest("The limit must be >= 0.")
except ValueError:
return HttpBadRequest("The limit must be an integer.")

object_list = {
'results': [],
'offset': offset,
'limit': limit,
}

# FIXME: Need to solve pagination.
# FIXME: Need to pass api_name & resource_name on to the instances.
for result in self.representation.get_list()[:self.limit]:
for result in self.representation.get_list()[offset:offset + limit]:
object_list['results'].append(result.to_dict())

desired_format = self.determine_format(request)
Expand Down
44 changes: 42 additions & 2 deletions tests/core/tests/resources.py
Expand Up @@ -172,7 +172,47 @@ def test_get_list(self):

resp = resource.get_list(request)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.content, '{"results": [{"content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "2010-03-30 20:05:00", "is_active": true, "slug": "first-post", "title": "First Post!", "updated": "2010-03-30 20:05:00"}, {"content": "The dog ate my cat today. He looks seriously uncomfortable.", "created": "2010-03-31 20:05:00", "is_active": true, "slug": "another-post", "title": "Another Post", "updated": "2010-03-31 20:05:00"}, {"content": "My neighborhood\'s been kinda weird lately, especially after the lava flow took out the corner store. Granny can hardly outrun the magma with her walker.", "created": "2010-04-01 20:05:00", "is_active": true, "slug": "recent-volcanic-activity", "title": "Recent Volcanic Activity.", "updated": "2010-04-01 20:05:00"}, {"content": "Man, the second eruption came on fast. Granny didn\'t have a chance. On the upshot, I was able to save her walker and I got a cool shawl out of the deal!", "created": "2010-04-02 10:05:00", "is_active": true, "slug": "grannys-gone", "title": "Granny\'s Gone", "updated": "2010-04-02 10:05:00"}]}')
self.assertEqual(resp.content, '{"limit": 20, "offset": 0, "results": [{"content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "2010-03-30 20:05:00", "is_active": true, "slug": "first-post", "title": "First Post!", "updated": "2010-03-30 20:05:00"}, {"content": "The dog ate my cat today. He looks seriously uncomfortable.", "created": "2010-03-31 20:05:00", "is_active": true, "slug": "another-post", "title": "Another Post", "updated": "2010-03-31 20:05:00"}, {"content": "My neighborhood\'s been kinda weird lately, especially after the lava flow took out the corner store. Granny can hardly outrun the magma with her walker.", "created": "2010-04-01 20:05:00", "is_active": true, "slug": "recent-volcanic-activity", "title": "Recent Volcanic Activity.", "updated": "2010-04-01 20:05:00"}, {"content": "Man, the second eruption came on fast. Granny didn\'t have a chance. On the upshot, I was able to save her walker and I got a cool shawl out of the deal!", "created": "2010-04-02 10:05:00", "is_active": true, "slug": "grannys-gone", "title": "Granny\'s Gone", "updated": "2010-04-02 10:05:00"}]}')

# Test slicing.
# First an invalid offset.
request.GET = {'format': 'json', 'offset': 'abc', 'limit': 1}
resp = resource.get_list(request)
self.assertEqual(resp.status_code, 400)

# Then an out of range offset.
request.GET = {'format': 'json', 'offset': -1, 'limit': 1}
resp = resource.get_list(request)
self.assertEqual(resp.status_code, 400)

# Then an out of range limit.
request.GET = {'format': 'json', 'offset': 0, 'limit': -1}
resp = resource.get_list(request)
self.assertEqual(resp.status_code, 400)

# Valid slice.
request.GET = {'format': 'json', 'offset': 0, 'limit': 2}
resp = resource.get_list(request)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.content, '{"limit": 2, "offset": 0, "results": [{"content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "2010-03-30 20:05:00", "is_active": true, "slug": "first-post", "title": "First Post!", "updated": "2010-03-30 20:05:00"}, {"content": "The dog ate my cat today. He looks seriously uncomfortable.", "created": "2010-03-31 20:05:00", "is_active": true, "slug": "another-post", "title": "Another Post", "updated": "2010-03-31 20:05:00"}]}')

# Valid, slightly overlapping slice.
request.GET = {'format': 'json', 'offset': 1, 'limit': 2}
resp = resource.get_list(request)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.content, '{"limit": 2, "offset": 1, "results": [{"content": "The dog ate my cat today. He looks seriously uncomfortable.", "created": "2010-03-31 20:05:00", "is_active": true, "slug": "another-post", "title": "Another Post", "updated": "2010-03-31 20:05:00"}, {"content": "My neighborhood\'s been kinda weird lately, especially after the lava flow took out the corner store. Granny can hardly outrun the magma with her walker.", "created": "2010-04-01 20:05:00", "is_active": true, "slug": "recent-volcanic-activity", "title": "Recent Volcanic Activity.", "updated": "2010-04-01 20:05:00"}]}')

# Valid, non-overlapping slice.
request.GET = {'format': 'json', 'offset': 3, 'limit': 2}
resp = resource.get_list(request)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.content, '{"limit": 2, "offset": 3, "results": [{"content": "Man, the second eruption came on fast. Granny didn\'t have a chance. On the upshot, I was able to save her walker and I got a cool shawl out of the deal!", "created": "2010-04-02 10:05:00", "is_active": true, "slug": "grannys-gone", "title": "Granny\'s Gone", "updated": "2010-04-02 10:05:00"}]}')

# Valid, but beyond the bounds slice.
request.GET = {'format': 'json', 'offset': 100, 'limit': 2}
resp = resource.get_list(request)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.content, '{"limit": 2, "offset": 100, "results": []}')

def test_get_detail(self):
resource = NoteResource()
Expand Down Expand Up @@ -275,7 +315,7 @@ def test_dispatch_list(self):

resp = resource.dispatch_list(request)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.content, '{"results": [{"content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "2010-03-30 20:05:00", "is_active": true, "slug": "first-post", "title": "First Post!", "updated": "2010-03-30 20:05:00"}, {"content": "The dog ate my cat today. He looks seriously uncomfortable.", "created": "2010-03-31 20:05:00", "is_active": true, "slug": "another-post", "title": "Another Post", "updated": "2010-03-31 20:05:00"}, {"content": "My neighborhood\'s been kinda weird lately, especially after the lava flow took out the corner store. Granny can hardly outrun the magma with her walker.", "created": "2010-04-01 20:05:00", "is_active": true, "slug": "recent-volcanic-activity", "title": "Recent Volcanic Activity.", "updated": "2010-04-01 20:05:00"}, {"content": "Man, the second eruption came on fast. Granny didn\'t have a chance. On the upshot, I was able to save her walker and I got a cool shawl out of the deal!", "created": "2010-04-02 10:05:00", "is_active": true, "slug": "grannys-gone", "title": "Granny\'s Gone", "updated": "2010-04-02 10:05:00"}]}')
self.assertEqual(resp.content, '{"limit": 20, "offset": 0, "results": [{"content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "2010-03-30 20:05:00", "is_active": true, "slug": "first-post", "title": "First Post!", "updated": "2010-03-30 20:05:00"}, {"content": "The dog ate my cat today. He looks seriously uncomfortable.", "created": "2010-03-31 20:05:00", "is_active": true, "slug": "another-post", "title": "Another Post", "updated": "2010-03-31 20:05:00"}, {"content": "My neighborhood\'s been kinda weird lately, especially after the lava flow took out the corner store. Granny can hardly outrun the magma with her walker.", "created": "2010-04-01 20:05:00", "is_active": true, "slug": "recent-volcanic-activity", "title": "Recent Volcanic Activity.", "updated": "2010-04-01 20:05:00"}, {"content": "Man, the second eruption came on fast. Granny didn\'t have a chance. On the upshot, I was able to save her walker and I got a cool shawl out of the deal!", "created": "2010-04-02 10:05:00", "is_active": true, "slug": "grannys-gone", "title": "Granny\'s Gone", "updated": "2010-04-02 10:05:00"}]}')

def test_dispatch_detail(self):
resource = NoteResource()
Expand Down

0 comments on commit 7c41e88

Please sign in to comment.