Add pagination to forums archives.

This commit is contained in:
Adrian Kuschelyagi Malacoda 2020-08-28 14:19:09 -05:00
parent 0e3f1274cc
commit ef3f3dd60c
5 changed files with 23 additions and 3 deletions

View File

@ -1,6 +1,7 @@
import os
import logging
import shutil
import math
from itertools import chain
from traceback import print_exc
@ -9,6 +10,7 @@ import chevron
import bbcode
import html
from .forum import DEFAULT_POSTS_PER_PAGE
from .wiki import Template, Renderer, Linker, NAMESPACES as WIKI_NAMESPACES
logging.basicConfig(level=logging.INFO)
@ -190,6 +192,8 @@ class ArchiveGenerator():
"target": "page-0.html"
})
total_pages = math.ceil((thread.num_replies + 1) / DEFAULT_POSTS_PER_PAGE)
page_links = [{"label": page + 1, "link": f"page-{page}.html"} for page in range(total_pages)]
page = 0
while True:
posts = [prepare_post(post) for post in forum.get_posts_in_thread(thread, page)]
@ -204,6 +208,7 @@ class ArchiveGenerator():
"thread": thread,
"page": page,
"next": page + 1,
"page_links": page_links,
"prev": page - 1,
"posts": posts
})

View File

@ -23,6 +23,9 @@ GET_POSTS = """
LIMIT ? OFFSET ?
""".format(PREFIX)
DEFAULT_POSTS_PER_PAGE = 15
DEFAULT_THREADS_PER_PAGE = 2000
def fix_encoding (string):
return string.encode("latin1", errors="ignore").decode(errors="ignore")
@ -50,7 +53,7 @@ class Forum():
cursor.execute(GET_BOARDS)
return [Board(board) for board in cursor.fetchall()]
def get_threads_in_board (self, board, page=0, per_page=2000):
def get_threads_in_board (self, board, page=0, per_page=DEFAULT_THREADS_PER_PAGE):
try:
board = board.id
except ValueError: pass
@ -58,7 +61,7 @@ class Forum():
cursor.execute(GET_THREADS, (board, per_page, page * per_page))
return [Thread(thread) for thread in cursor.fetchall()]
def get_posts_in_thread (self, thread, page=0, per_page=15):
def get_posts_in_thread (self, thread, page=0, per_page=DEFAULT_POSTS_PER_PAGE):
try:
thread = thread.id
except ValueError: pass
@ -89,6 +92,7 @@ class Thread():
self.datetime = datetime.fromtimestamp(row['poster_time'])
self.subject = fix_encoding(row['subject'])
self.poster_name = fix_encoding(row['poster_name'])
self.num_replies = row['num_replies']
class Post():
def __init__ (self, row):

View File

@ -11,7 +11,11 @@ ul.boards { margin-left: 0; padding-left: 0; }
.label { font-weight: bold }
article { border-top: 1px solid black; }
section { margin-top: 15px; margin-bottom: 15px; }
.next { float: right; }
.pagination { margin-bottom: 10px; }
.pagination ul { list-style-type: none; margin-left: 0; padding-left: 0; display: inline; }
.pagination li { display: inline; }
.page { padding-top: 15px; }
.page table { width: 100%; }

View File

@ -1,4 +1,9 @@
<div class="pagination">
<a class="prev" href="page-{{prev}}.html">Previous Page</a>
<ul>
{{#page_links}}
<li><a href="{{link}}">{{label}}</a></li>
{{/page_links}}
</ul>
<a class="next" href="page-{{next}}.html">Next Page</a>
</div>

View File

@ -5,12 +5,14 @@
<th>Title</th>
<th>Poster</th>
<th>Date</th>
<th>Replies</th>
</tr>
{{#threads}}
<tr>
<td class="thread-subject"><a href="thread-{{id}}">{{subject}}</a></td>
<td class="thread-poster">{{poster_name}}</td>
<td class="thread-date">{{datetime}}</td>
<td class="replies">{{num_replies}}</td>
</tr>
{{/threads}}
</table>