This commit is contained in:
Chris Elston 2013-11-19 09:49:46 -08:00
commit d7aac6b900
5 changed files with 76 additions and 2 deletions

View file

@ -488,6 +488,12 @@ Setting name (default value) What does it do?
`DEFAULT_PAGINATION` (``False``) The maximum number of articles to include on a
page, not including orphans. False to disable
pagination.
`TEMPLATE_PAGINATION` (``{}``) A dictionary used to set per-template pagination,
used when you wish different templates to have
a specific number of articles per page.
For example: ``{'index': 10}``
`PAGINATION_PATTERNS` A set of patterns that are used to determine advanced
pagination output.
================================================ =====================================================

View file

@ -20,12 +20,18 @@ PaginationRule = namedtuple(
class Paginator(object):
def __init__(self, name, object_list, settings):
def __init__(self, name, template_name, object_list, settings):
self.name = name
self.object_list = object_list
self.settings = settings
if settings.get('DEFAULT_PAGINATION'):
# Are there specific pagination settings for this template?
per_page = settings.get('TEMPLATE_PAGINATION').get(template_name)
if per_page:
self.per_page = per_page
self.orphans = settings.get('DEFAULT_ORPHANS')
elif settings.get('DEFAULT_PAGINATION'):
self.per_page = settings.get('DEFAULT_PAGINATION')
self.orphans = settings.get('DEFAULT_ORPHANS')
else:

View file

@ -95,6 +95,7 @@ DEFAULT_CONFIG = {
'JINJA_EXTENSIONS': [],
'JINJA_FILTERS': {},
'LOCALE': [''], # defaults to user locale
'TEMPLATE_PAGINATION': {},
'DEFAULT_PAGINATION': False,
'DEFAULT_ORPHANS': 0,
'DEFAULT_METADATA': (),

View file

@ -0,0 +1,59 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
from pelican.generators import ArticlesGenerator
from pelican.writers import Writer
from pelican.tests.support import unittest, get_settings
from pelican.paginator import Paginator
CUR_DIR = os.path.dirname(__file__)
CONTENT_DIR = os.path.join(CUR_DIR, 'content')
class TestPaginator(unittest.TestCase):
@classmethod
def setUpClass(cls):
settings = get_settings(filenames={})
settings['DEFAULT_CATEGORY'] = 'Default'
settings['DEFAULT_DATE'] = (1970, 1, 1)
cls.generator = ArticlesGenerator(
context=settings.copy(), settings=settings,
path=CONTENT_DIR, theme=settings['THEME'], output_path=None)
cls.generator.generate_context()
cls.articles = [[page.title, page.status, page.category.name,
page.template] for page in cls.generator.articles]
def test_default_pagination_default(self):
settings = get_settings()
paginator = Paginator('index', 'index', self.articles, settings)
self.assertEqual(paginator._get_num_pages(), 1)
def test_default_pagination_value(self):
settings = get_settings()
settings['DEFAULT_PAGINATION'] = 5
paginator = Paginator('articles', 'articles', self.articles, settings)
self.assertTrue(paginator._get_num_pages() > 1)
def test_per_template_pagination_unaffected(self):
settings = get_settings()
settings['TEMPLATE_PAGINATION'] = { 'articles' : 5 }
paginator = Paginator('index', 'index', self.articles, settings)
self.assertEqual(paginator._get_num_pages(), 1)
def test_per_template_pagination_affected(self):
settings = get_settings()
settings['TEMPLATE_PAGINATION'] = { 'articles' : 5 }
paginator = Paginator('articles', 'articles', self.articles, settings)
self.assertTrue(paginator._get_num_pages() > 1)

View file

@ -180,6 +180,7 @@ class Writer(object):
paginated = paginated or {}
if paginated:
name_root = os.path.splitext(name)[0]
template_root = os.path.splitext(template.name)[0]
# pagination needed, init paginators
paginators = {}
@ -188,6 +189,7 @@ class Writer(object):
paginators[key] = Paginator(
name_root,
template_root,
object_list,
self.settings,
)