Package: api_pagination
Pagination calculator designed for APIsWhen building an API, out of bound pages should be treated with empty result sets. The existing solutions were not doing this. We have designedapi-paginationto handle these edge cases from the perspective of an API.Getting StartedInstall the module with:pip install api_pagination# Load in dependenciesfromapi_paginationimportPaginator# Generate a paginator and get info about a pagepaginator=Paginator(total=100,items_per_page=10)page_info=paginator.get_page_info(page=2){'overall':{'first_page':1,'last_page':10,'pages':10,'total':100},'page':{'current_page':2,'next_page':3,'previous_page':1}}# Use a classmethod to get info in one fell swooppage_info=Paginator.page_info(page=1,total=100,items_per_page=10){'overall':{'first_page':1,'last_page':10,'pages':10,'total':100},'page':{'current_page':1,'next_page':2,'previous_page':None}}# Handle out of bounds properlypage_info=Paginator.page_info(page=20,total=100,items_per_page=10){'overall':{'first_page':1,'last_page':10,'pages':10,'total':100},'page':{'current_page':20,'next_page':None,'previous_page':10}}DocumentationWe expose thePaginatorclass via our packageapi_paginationPaginator(total, items_per_page)Class for calculating pagination info about itemstotalint- Overall amount of items presentitems_per_pageint- How many items to include on each pagepaginator.get_page_info(page)Gather information about a given pagepageint- Human based index for a pageFor example, page 1 will be for items 1-10 (whereitems_per_page=10)Returns:ret_valdict- Container for information about overall information and page specific informationoveralldict- Container for overall informationfirst_pageint- Human based index for first pageFor example, withtotal=20anditems_per_page=10, we havefirst_page=1last_pageint- Human based index for last pageFor example, withtotal=25anditems_per_page=10, we havelast_page=3(includes items 21-25)pagesint- Amount of pages overallFor example, withtotal=25anditems_per_page=10, we havepages=3totalint- Amount of items overallpagedict- Container for page specific informationcurrent_pageint- Human based index of requested pagenext_pageint|None- If there is another page after this one,next_pagewill be that page’s human based indexFor example, withtotal=25,items_per_page=10, andpage=2, we havenext_page=3(includes items 21-25)When on the last page (e.g.total=25,items_per_page=10,page=3)next_pagewill beNoneIf we are under bounds (e.g.page=-1), thennext_pagewill be the first page (page=1)previous_pageint|None- If there is another page before this one,previous_pagewill be that page’s human based indexFor example, withtotal=25,items_per_page=10, andpage=2, we haveprevious_page=1(includes items 1-10)When on the first page (e.g.total=25,items_per_page=10,page=1) thenprevious_pagewill beNoneIf we are over bounds (e.g.total=25,items_per_page=10,page=4), thenprevious_pagewill be the last page (page=3)Paginator.page_info(page, *args, **kwargs)Helper function to get page info without calling multiple actionspageint- Page to pass through topaginator.get_page_info*args - Ordered arguments to pass through toPaginatorconstructor**kwargs - Keyword based arguments to pass through toPaginatorconstructorReturns:Returns same format aspaginator.get_page_infoExample:page_info=Paginator.page_info(page=1,total=100,items_per_page=10)# Same as# paginator = Paginator(total=100, items_per_page=10)# page_info = paginator.get_page_info(page=1)ContributingIn lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Test via./test.sh.LicenseCopyright (c) 2015 Underdog.ioLicensed under the MIT license.