Django

Code

Ticket #6997: paginator2.diff

File paginator2.diff, 1.4 kB (added by simeon, 6 months ago)

Updated patch to hande orphans param, match svn head and add tests

  • django/core/paginator.py

    old new  
    5353    def _get_num_pages(self): 
    5454        "Returns the total number of pages." 
    5555        if self._num_pages is None: 
    56             hits = self.count - 1 - self.orphans 
    57             if hits < 1: 
    58                 hits = 0 
    59             if hits == 0 and not self.allow_empty_first_page: 
     56            if self.count - self.orphans <= 0 and not self.allow_empty_first_page: 
    6057                self._num_pages = 0 
    6158            else: 
     59                hits = self.count - 1 - self.orphans 
     60                if hits < 0: 
     61                    hits = 0 
    6262                self._num_pages = hits // self.per_page + 1 
    6363        return self._num_pages 
    6464    num_pages = property(_get_num_pages) 
  • tests/modeltests/pagination/models.py

    old new  
    140140>>> p.end_index() 
    1411415 
    142142 
     143# Check edge case: with allow_empty_first_page=False and list len=1, num_pages should be 1 
     144>>> paginator = Paginator([1], 5, 0, False) 
     145>>> paginator.count 
     1461 
     147>>> paginator.num_pages 
     1481 
     149>>> paginator.page_range 
     150[1] 
     151 
     152 
    143153################################ 
    144154# Legacy API (ObjectPaginator) # 
    145155################################