Skip to content

Commit

Permalink
Avoid
Browse files Browse the repository at this point in the history
Module plone.batching.batch, line 232, in previous_pages
ValueError: XXX is not in list
that can happen if b_start is too high and using orphan
See plone/Products.CMFPlone#639
  • Loading branch information
gbastien committed Jul 9, 2015
1 parent fd08400 commit 760cf78
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Changelog
1.0.5 (unreleased)
------------------

- Nothing changed yet.
- Make sure pagenumber value is not bigger that numpages
or it fails in previous_pages when using orphan
[gbastien]


1.0.4 (2015-04-28)
Expand Down
17 changes: 10 additions & 7 deletions plone/batching/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,22 @@ def initialize(self, start, end, size):
self.end = end

self.first = max(start - 1, 0)
if self.beyond:
self.first = self.end
self.length = self.end - self.first

self.last = self.sequence_length - size

# Set up the total number of pages
self.numpages = calculate_pagenumber(
self.sequence_length - self.orphan, self.pagesize, self.overlap)

# Set up the current page number
self._pagenumber = calculate_pagenumber(
self.start, self.pagesize, self.overlap)
if self.beyond:
self.first = self.end
# Make sure current page number is not > numpages
self._pagenumber = self.numpages
else:
# Set up the current page number
self._pagenumber = calculate_pagenumber(
self.start, self.pagesize, self.overlap)

self.length = self.end - self.first

@property
def navlist(self):
Expand Down
11 changes: 11 additions & 0 deletions plone/batching/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ def test_multiple_pages_longer(self):
batch = BaseBatch(range(12), 10)
self.assertEquals(batch.multiple_pages, True)

def test_pagenumber_never_over_numpages(self):
"""computed _pagenumber is never > numpages, this
makes previous_pages not fail."""
batch = BaseBatch([1, 2, 3, 4, 5, 6, 7], 3, 8)
self.assertEquals(batch.previous_pages, [1, 2])
self.assertEquals(batch._pagenumber, 3)
# works especially with orphan
batch = BaseBatch([1, 2, 3, 4, 5, 6, 7], 3, 8, orphan=2)
self.assertEquals(batch.previous_pages, [1])
self.assertEquals(batch._pagenumber, 2)


class TestQuantumBatch(unittest.TestCase):

Expand Down

0 comments on commit 760cf78

Please sign in to comment.