Define 'page' query field name in one place.

This commit is contained in:
Ben Konrath 2012-10-11 11:39:23 +02:00
parent 6fbd411254
commit 5454162b04

View File

@ -3,7 +3,11 @@ from rest_framework import serializers
# TODO: Support URLconf kwarg-style paging # TODO: Support URLconf kwarg-style paging
class NextPageField(serializers.Field): class PageField(serializers.Field):
page_field = 'page'
class NextPageField(PageField):
""" """
Field that returns a link to the next page in paginated results. Field that returns a link to the next page in paginated results.
""" """
@ -12,16 +16,16 @@ class NextPageField(serializers.Field):
return None return None
page = value.next_page_number() page = value.next_page_number()
request = self.context.get('request') request = self.context.get('request')
relative_url = '?page=%d' % page relative_url = '?%s=%d' % (self.page_field, page)
if request: if request:
for field, value in request.QUERY_PARAMS.iteritems(): for field, value in request.QUERY_PARAMS.iteritems():
if field != 'page': if field != self.page_field:
relative_url += '&%s=%s' % (field, value) relative_url += '&%s=%s' % (field, value)
return request.build_absolute_uri(relative_url) return request.build_absolute_uri(relative_url)
return relative_url return relative_url
class PreviousPageField(serializers.Field): class PreviousPageField(PageField):
""" """
Field that returns a link to the previous page in paginated results. Field that returns a link to the previous page in paginated results.
""" """
@ -30,10 +34,10 @@ class PreviousPageField(serializers.Field):
return None return None
page = value.previous_page_number() page = value.previous_page_number()
request = self.context.get('request') request = self.context.get('request')
relative_url = '?page=%d' % page relative_url = '?%s=%d' % (self.page_field, page)
if request: if request:
for field, value in request.QUERY_PARAMS.iteritems(): for field, value in request.QUERY_PARAMS.iteritems():
if field != 'page': if field != self.page_field:
relative_url += '&%s=%s' % (field, value) relative_url += '&%s=%s' % (field, value)
return request.build_absolute_uri(relative_url) return request.build_absolute_uri(relative_url)
return relative_url return relative_url