Code

Ticket #2093: better_paginator_with_paginator_page_addition.patch

File better_paginator_with_paginator_page_addition.patch, 6.2 KB (added by SmileyChris, 8 years ago)

PaginatorPage object - page aware ObjectPaginator wrapper

  • paginator.py

     
    1 from copy import copy 
    2 from math import ceil 
    3  
    41class InvalidPage(Exception): 
    52    pass 
    63 
    74class ObjectPaginator(object): 
    85    """ 
    9     This class makes pagination easy. Feed it a QuerySet, plus the number of 
    10     objects you want on each page. Then read the hits and pages properties to 
     6    This class makes pagination easy. Feed it a QuerySet or list, plus the number 
     7    of objects you want on each page. Then read the hits and pages properties to 
    118    see how many pages it involves. Call get_page with a page number (starting 
    129    at 0) to get back a list of objects for that page. 
    1310 
    1411    Finally, check if a page number has a next/prev page using 
    1512    has_next_page(page_number) and has_previous_page(page_number). 
     13     
     14    Use orphan to avoid small final pages. For example: 
     15    13 records, num_per_page=10, orphan=2 --> pages==2, len(self.get_page(0))==10 
     16    12 records, num_per_page=10, orphan=2 --> pages==1, len(self.get_page(0))==12 
    1617    """ 
    17     def __init__(self, query_set, num_per_page): 
     18    def __init__(self, query_set, num_per_page, orphan=0): 
    1819        self.query_set = query_set 
    1920        self.num_per_page = num_per_page 
    20         self._hits, self._pages = None, None 
    21         self._has_next = {} # Caches page_number -> has_next_boolean 
     21        self.orphan = orphan 
     22        self._hits = self._pages = None 
    2223 
    23     def get_page(self, page_number): 
     24    def validate_page_number(self, page_number): 
    2425        try: 
    2526            page_number = int(page_number) 
    2627        except ValueError: 
    2728            raise InvalidPage 
    28         if page_number < 0: 
     29        if page_number < 0 or page_number > self.pages - 1: 
    2930            raise InvalidPage 
     31        return page_number 
    3032 
    31         # Retrieve one extra record, and check for the existence of that extra 
    32         # record to determine whether there's a next page. 
    33         limit = self.num_per_page + 1 
    34         offset = page_number * self.num_per_page 
     33    def get_page(self, page_number): 
     34        page_number = self.validate_page_number(page_number) 
     35        bottom = page_number * self.num_per_page 
     36        top = bottom + self.num_per_page 
     37        if top + self.orphan >= self.hits: 
     38            top = self.hits 
     39        return self.query_set[bottom:top] 
    3540 
    36         object_list = list(self.query_set[offset:offset+limit]) 
    37  
    38         if not object_list: 
    39             raise InvalidPage 
    40  
    41         self._has_next[page_number] = (len(object_list) > self.num_per_page) 
    42         return object_list[:self.num_per_page] 
    43  
    4441    def has_next_page(self, page_number): 
    4542        "Does page $page_number have a 'next' page?" 
    46         if not self._has_next.has_key(page_number): 
    47             if self._pages is None: 
    48                 offset = (page_number + 1) * self.num_per_page 
    49                 self._has_next[page_number] = len(self.query_set[offset:offset+1]) > 0 
    50             else: 
    51                 self._has_next[page_number] = page_number < (self.pages - 1) 
    52         return self._has_next[page_number] 
     43        return page_number < self.pages - 1 
    5344 
    5445    def has_previous_page(self, page_number): 
    5546        return page_number > 0 
     
    5950        Returns the 1-based index of the first object on the given page, 
    6051        relative to total objects found (hits). 
    6152        """ 
    62         if page_number == 0: 
    63             return 1 
     53        page_number = self.validate_page_number(page_number) 
    6454        return (self.num_per_page * page_number) + 1 
    6555 
    6656    def last_on_page(self, page_number): 
     
    6858        Returns the 1-based index of the last object on the given page, 
    6959        relative to total objects found (hits). 
    7060        """ 
    71         if page_number == 0 and self.num_per_page >= self._hits: 
    72             return self._hits 
    73         elif page_number == (self._pages - 1) and (page_number + 1) * self.num_per_page > self._hits: 
    74             return self._hits 
    75         return (page_number + 1) * self.num_per_page 
     61        page_number = self.validate_page_number(page_number) 
     62        page_number += 1   # 1-base 
     63        if page_number == self.pages: 
     64            return self.hits 
     65        return page_number * self.num_per_page 
    7666 
    7767    def _get_hits(self): 
    7868        if self._hits is None: 
    79             self._hits = self.query_set.count() 
     69            # If query_set is a list or tuple, use len(self.query_set) otherwise assume 
     70            # that it is a QuerySet and use .count(). 
     71            if isinstance(self.query_set, list) or isinstance(self.query_set, tuple): 
     72                self._hits = len(self.query_set) 
     73            else: 
     74                self._hits = self.query_set.count() 
    8075        return self._hits 
    8176 
    8277    def _get_pages(self): 
    8378        if self._pages is None: 
    84             self._pages = int(ceil(self.hits / float(self.num_per_page))) 
     79            hits = (self.hits - 1 - self.orphan) 
     80            if hits < 1: 
     81                hits = 0 
     82            self._pages = hits // self.num_per_page + 1 
    8583        return self._pages 
    8684 
    8785    hits = property(_get_hits) 
    8886    pages = property(_get_pages) 
     87 
     88 
     89class PaginatorPage: 
     90    """ 
     91    This class is used to work with a page of a paginator object. 
     92    Pass a paginator object, and a valid page number. 
     93    It uses the same methods as a paginator, except you do not need 
     94    to use a page number. 
     95    """ 
     96    def __init__(self, paginator, page_number): 
     97        self.paginator = paginator 
     98        self.page_number = paginator.validate_page_number(page_number) 
     99    def get_page(self): 
     100        if not hasattr(self, '_page'): 
     101            self._page = self.paginator.get_page(self.page_number) 
     102        return self._page 
     103    def has_next_page(self): 
     104        return self.paginator.has_next_page(self.page_number) 
     105    def has_previous_page(self): 
     106        return self.paginator.has_previous_page(self.page_number) 
     107    def first_on_page(self): 
     108        return self.paginator.first_on_page(self.page_number) 
     109    def last_on_page(self): 
     110        return self.paginator.last_on_page(self.page_number) 
     111    def _get_hits(self): 
     112        return self.paginator.hits 
     113    hits = property(_get_hits) 
     114    def _get_pages(self): 
     115        return self.paginator.pages 
     116    pages = property(_get_pages)