Allow to cap the pagination

This way one could only generate one page of content by using MAX_PAGINATION = 1
This commit is contained in:
Martin Trigaux 2023-07-01 10:29:22 +02:00
commit 6fc577e505
4 changed files with 26 additions and 0 deletions

View file

@ -1038,6 +1038,11 @@ You can use the following settings to configure the pagination.
The maximum number of articles to include on a page, not including orphans.
False to disable pagination.
.. data:: MAX_PAGINATION = False
The maximum number of pages generated. ``False`` to paginate all available
pages.
.. data:: PAGINATED_TEMPLATES = {'index': None, 'tag': None, 'category': None, 'author': None}
The templates to use pagination with, and the number of articles to include

View file

@ -47,6 +47,8 @@ class Paginator:
if self._num_pages is None:
hits = max(1, self.count - self.orphans)
self._num_pages = int(ceil(hits / (float(self.per_page) or 1)))
if self.settings["MAX_PAGINATION"]:
self._num_pages = min(self.settings["MAX_PAGINATION"], self._num_pages)
return self._num_pages
num_pages = property(_get_num_pages)

View file

@ -90,6 +90,7 @@ DEFAULT_CONFIG = {
(1, '{name}{extension}', '{name}{extension}'),
(2, '{name}{number}{extension}', '{name}{number}{extension}'),
],
'MAX_PAGINATION': False,
'YEAR_ARCHIVE_URL': '',
'YEAR_ARCHIVE_SAVE_AS': '',
'MONTH_ARCHIVE_URL': '',

View file

@ -102,3 +102,21 @@ class TestPage(unittest.TestCase):
page3 = paginator.page(3)
self.assertEqual(page3.save_as, 'blog/index.html')
self.assertEqual(page3.url, '//blog.my.site/')
def test_max_pagination(self):
from pelican.paginator import PaginationRule
settings = get_settings()
settings['MAX_PAGINATION'] = 2
settings['PAGINATION_PATTERNS'] = [PaginationRule(*r) for r in [
(1, '/{url}{number}/', '{base_name}/{number}/index.html')
]]
self.page_kwargs['metadata']['author'] = Author('Blogger', settings)
object_list = [Article(**self.page_kwargs),
Article(**self.page_kwargs),
Article(**self.page_kwargs)]
paginator = Paginator('blog/index.html', '//blog.my.site/',
object_list, settings, 1)
self.assertEqual(paginator.count, 3)
self.assertEqual(paginator.per_page, 1)
self.assertEqual(paginator.num_pages, 2)