Skip to content

Commit

Permalink
Allow no posts pagination if JFME_NUMBER_OF_POSTS_BY_PAGE is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
Clément committed Jul 25, 2024
1 parent 9a34855 commit 74e2823
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Otherwise, you have to configure the following settings :
Other useful settings :
- Default metadata : `JFME_DEFAULT_METADATA_DICT` and `JFME_DEFAULT_METADATA_FILEPATH` allow to set default metadata for pages and posts. The first one is a python dictionary and the second one is a Path to a file having the same format as metadata section in pages.
The order, from less to most priority is : `JFME_DEFAULT_METADATA_DICT` then `JFME_DEFAULT_METADATA_FILEPATH` then page matadata.
- Posts pagination : `JFME_NUMBER_OF_POSTS_BY_PAGE` give the maximum number of posts in a posts list page. If not set, all posts will be in the first page.

### `Dockerfile` :
- In the `# Copy source dir` section, add `COPY <content-dir>/ <content-dir>/` for each content directory in `JFME_CONTENT_DIRS`
Expand Down
55 changes: 30 additions & 25 deletions content/templates/jinja2/post-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,36 @@ <h2 class="blog__post__text__title">{{ post.title }}</h2>
</div>
{% endfor %}
</div>
<div>
{% if object.page > 1 %}
{% if object.category != "" %}
<a href="/posts/category/{{object.category}}/page{{object.page-1}}.html">Page précédente</a>
{% else %}
<a href="/posts/page{{object.page-1}}.html">Page précédente</a>
{% endif %}
{% endif %}

{% if object.page < object.nb_pages %}
{% if object.category != "" %}
<a href="/posts/category/{{object.category}}/page{{object.page+1}}.html">Page suivante</a>
{% else %}
<a href="/posts/page{{object.page+1}}.html">Page suivante</a>
{% endif %}
{% if object.nb_pages > 1 %}
<div>
{% if object.page > 1 %}
{% if object.category != "" %}
<a href="/posts/category/{{object.category}}/page{{object.page-1}}.html">Page précédente</a>
{% else %}
<a href="/posts/page{{object.page-1}}.html">Page précédente</a>
{% endif %}
{% endif %}

{% if object.page < object.nb_pages %}
{% if object.category != "" %}
<a href="/posts/category/{{object.category}}/page{{object.page+1}}.html">Page suivante</a>
{% else %}
<a href="/posts/page{{object.page+1}}.html">Page suivante</a>
{% endif %}
{% endif %}
</div>

<div>
Pages :
{% for page in range(1, object.nb_pages+1) %}
{% if object.category != "" %}
<a href="/posts/category/{{object.category}}/page{{page}}.html">{{page}}</a>
{% else %}
<a href="/posts/page{{page}}.html">{{page}}</a>
{% endif %}
{% endfor %}
</div>
{% endif %}
</div>
<div>
Pages :
{% for page in range(1, object.nb_pages+1) %}
{% if object.category != "" %}
<a href="/posts/category/{{object.category}}/page{{page}}.html">{{page}}</a>
{% else %}
<a href="/posts/page{{page}}.html">{{page}}</a>
{% endif %}
{% endfor %}
</div>

{% endblock %}
15 changes: 11 additions & 4 deletions jssg/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,18 @@ class PostList :
def __init__(self, category = "", page = 1) -> None:
self.category = category
self.page = page
self.posts_by_page = settings.JFME_NUMBER_OF_POSTS_BY_PAGE
if self.category == "" :
self.nb_pages = ceil(len(list(Post.load_glob(all=True))) / self.posts_by_page) # number of posts / number of posts by page

if category == "" :
nb_posts = len(list(Post.load_glob(all = True)))
else :
self.nb_pages = ceil(len(list(filter(lambda p: p.metadata["category"] == self.category, Post.load_glob(all=True)))) / self.posts_by_page) # number of posts of the category / number of posts by page
nb_posts = len(list(filter(lambda p: p.metadata["category"] == self.category, Post.load_glob(all=True))))

if hasattr(settings, "JFME_NUMBER_OF_POSTS_BY_PAGE") :
self.posts_by_page = settings.JFME_NUMBER_OF_POSTS_BY_PAGE
else :
self.posts_by_page = nb_posts

self.nb_pages = ceil(nb_posts / self.posts_by_page) # number of posts / number of posts by page

@classmethod
def load_post_list_with_category(cls, category, page) :
Expand Down
2 changes: 1 addition & 1 deletion jssg/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
JFME_STATIC_DIRS = [path / "static" for path in JFME_CONTENT_DIRS]
JFME_DEFAULT_METADATA_DICT = {"slug": "index", } # The order of include is : JFME_DEFAULT_METADATA_DICT then JFME_DEFAULT_METADATA_FILEPATH then page metadata
JFME_DEFAULT_METADATA_FILEPATH = BASE_DIR / "jssg" / "default_metadata.txt" # If a metadata is specified more than once, the last included is retained
JFME_NUMBER_OF_POSTS_BY_PAGE = 3
JFME_NUMBER_OF_POSTS_BY_PAGE = 3 # no pagination of posts if not set
JFME_CONTENT_REQUIRED_METADATA = ["title", "slug", "lang", "description"]
JFME_SITEMAP_LASTMOD_DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S%z" # strftime format, see https://docs.python.org/fr/3.6/library/datetime.html#strftime-and-strptime-behavior, see https://www.sitemaps.org/protocol.html#lastmoddef for allowed datetime formats

Expand Down

0 comments on commit 74e2823

Please sign in to comment.