Ticket #2575: better_paginator.patch
File better_paginator.patch, 5.2 KB (added by , 18 years ago) |
---|
-
core/paginator.py
1 from math import ceil2 3 1 class InvalidPage(Exception): 4 2 pass 5 3 6 4 class ObjectPaginator(object): 7 5 """ 8 This class makes pagination easy. Feed it a QuerySet , plus the number of9 o bjects you want on each page. Then read the hits and pages properties to6 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 10 8 see how many pages it involves. Call get_page with a page number (starting 11 9 at 0) to get back a list of objects for that page. 12 10 13 11 Finally, check if a page number has a next/prev page using 14 12 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 15 17 """ 16 def __init__(self, query_set, num_per_page ):18 def __init__(self, query_set, num_per_page, orphan=0): 17 19 self.query_set = query_set 18 20 self.num_per_page = num_per_page 19 self. _hits, self._pages = None, None20 self._h as_next = {} # Caches page_number -> has_next_boolean21 self.orphan = orphan 22 self._hits = self._pages = None 21 23 22 def get_page(self, page_number):24 def validate_page_number(self, page_number): 23 25 try: 24 26 page_number = int(page_number) 25 27 except ValueError: 26 28 raise InvalidPage 27 if page_number < 0 :29 if page_number < 0 or page_number > self.pages - 1: 28 30 raise InvalidPage 31 return page_number 29 32 30 # Retrieve one extra record, and check for the existence of that extra 31 # record to determine whether there's a next page. 32 limit = self.num_per_page + 1 33 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] 34 40 35 object_list = list(self.query_set[offset:offset+limit])36 37 if not object_list:38 raise InvalidPage39 40 self._has_next[page_number] = (len(object_list) > self.num_per_page)41 return object_list[:self.num_per_page]42 43 41 def has_next_page(self, page_number): 44 42 "Does page $page_number have a 'next' page?" 45 if not self._has_next.has_key(page_number): 46 if self._pages is None: 47 offset = (page_number + 1) * self.num_per_page 48 self._has_next[page_number] = len(self.query_set[offset:offset+1]) > 0 49 else: 50 self._has_next[page_number] = page_number < (self.pages - 1) 51 return self._has_next[page_number] 43 return page_number < self.pages - 1 52 44 53 45 def has_previous_page(self, page_number): 54 46 return page_number > 0 … … 58 50 Returns the 1-based index of the first object on the given page, 59 51 relative to total objects found (hits). 60 52 """ 61 if page_number == 0: 62 return 1 53 page_number = self.validate_page_number(page_number) 63 54 return (self.num_per_page * page_number) + 1 64 55 65 56 def last_on_page(self, page_number): … … 67 58 Returns the 1-based index of the last object on the given page, 68 59 relative to total objects found (hits). 69 60 """ 70 if page_number == 0 and self.num_per_page >= self._hits:71 return self._hits72 elif page_number == (self._pages - 1) and (page_number + 1) * self.num_per_page > self._hits:73 return self. _hits74 return (page_number + 1)* self.num_per_page61 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 75 66 76 67 def _get_hits(self): 77 68 if self._hits is None: 78 self._hits = self.query_set.count() 69 # Try .count() or fall back to len(). 70 try: 71 self._hits = int(self.query_set.count()) 72 except AttributeEror, TypeError, ValueError: 73 # AttributeError if query_set has no object count. 74 # TypeError if query_set.count() required arguments. 75 # ValueError if int() fails. 76 self._hits = len(self.query_set) 77 else: 79 78 return self._hits 80 79 81 80 def _get_pages(self): 82 81 if self._pages is None: 83 self._pages = int(ceil(self.hits / float(self.num_per_page))) 82 hits = (self.hits - 1 - self.orphan) 83 if hits < 1: 84 hits = 0 85 self._pages = hits // self.num_per_page + 1 84 86 return self._pages 85 87 86 88 hits = property(_get_hits) 87 pages = property(_get_pages) 89 pages = property(_get_pages) 90 No newline at end of file