mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Add new setting 'TEMPLATE_PAGINATION'
This change adds a new setting 'TEMPLATE_PAGINATION', which may be used when you want a specific page to have a different pagination value than the default. For example, you want an index page with 10 articles per page, but category pages should have 3 articles per page.
This commit is contained in:
parent
1fad70e5b9
commit
1d29c9d2f8
5 changed files with 76 additions and 2 deletions
|
|
@ -480,6 +480,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.
|
||||
================================================ =====================================================
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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': (),
|
||||
|
|
|
|||
59
pelican/tests/test_paginator.py
Normal file
59
pelican/tests/test_paginator.py
Normal 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)
|
||||
|
|
@ -175,6 +175,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 = {}
|
||||
|
|
@ -183,6 +184,7 @@ class Writer(object):
|
|||
|
||||
paginators[key] = Paginator(
|
||||
name_root,
|
||||
template_root,
|
||||
object_list,
|
||||
self.settings,
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue