Add pagination to forums archives.
This commit is contained in:
parent
0e3f1274cc
commit
ef3f3dd60c
@ -1,6 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
import shutil
|
import shutil
|
||||||
|
import math
|
||||||
|
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
from traceback import print_exc
|
from traceback import print_exc
|
||||||
@ -9,6 +10,7 @@ import chevron
|
|||||||
import bbcode
|
import bbcode
|
||||||
import html
|
import html
|
||||||
|
|
||||||
|
from .forum import DEFAULT_POSTS_PER_PAGE
|
||||||
from .wiki import Template, Renderer, Linker, NAMESPACES as WIKI_NAMESPACES
|
from .wiki import Template, Renderer, Linker, NAMESPACES as WIKI_NAMESPACES
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
@ -190,6 +192,8 @@ class ArchiveGenerator():
|
|||||||
"target": "page-0.html"
|
"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
|
page = 0
|
||||||
while True:
|
while True:
|
||||||
posts = [prepare_post(post) for post in forum.get_posts_in_thread(thread, page)]
|
posts = [prepare_post(post) for post in forum.get_posts_in_thread(thread, page)]
|
||||||
@ -204,6 +208,7 @@ class ArchiveGenerator():
|
|||||||
"thread": thread,
|
"thread": thread,
|
||||||
"page": page,
|
"page": page,
|
||||||
"next": page + 1,
|
"next": page + 1,
|
||||||
|
"page_links": page_links,
|
||||||
"prev": page - 1,
|
"prev": page - 1,
|
||||||
"posts": posts
|
"posts": posts
|
||||||
})
|
})
|
||||||
|
@ -23,6 +23,9 @@ GET_POSTS = """
|
|||||||
LIMIT ? OFFSET ?
|
LIMIT ? OFFSET ?
|
||||||
""".format(PREFIX)
|
""".format(PREFIX)
|
||||||
|
|
||||||
|
DEFAULT_POSTS_PER_PAGE = 15
|
||||||
|
DEFAULT_THREADS_PER_PAGE = 2000
|
||||||
|
|
||||||
def fix_encoding (string):
|
def fix_encoding (string):
|
||||||
return string.encode("latin1", errors="ignore").decode(errors="ignore")
|
return string.encode("latin1", errors="ignore").decode(errors="ignore")
|
||||||
|
|
||||||
@ -50,7 +53,7 @@ class Forum():
|
|||||||
cursor.execute(GET_BOARDS)
|
cursor.execute(GET_BOARDS)
|
||||||
return [Board(board) for board in cursor.fetchall()]
|
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:
|
try:
|
||||||
board = board.id
|
board = board.id
|
||||||
except ValueError: pass
|
except ValueError: pass
|
||||||
@ -58,7 +61,7 @@ class Forum():
|
|||||||
cursor.execute(GET_THREADS, (board, per_page, page * per_page))
|
cursor.execute(GET_THREADS, (board, per_page, page * per_page))
|
||||||
return [Thread(thread) for thread in cursor.fetchall()]
|
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:
|
try:
|
||||||
thread = thread.id
|
thread = thread.id
|
||||||
except ValueError: pass
|
except ValueError: pass
|
||||||
@ -89,6 +92,7 @@ class Thread():
|
|||||||
self.datetime = datetime.fromtimestamp(row['poster_time'])
|
self.datetime = datetime.fromtimestamp(row['poster_time'])
|
||||||
self.subject = fix_encoding(row['subject'])
|
self.subject = fix_encoding(row['subject'])
|
||||||
self.poster_name = fix_encoding(row['poster_name'])
|
self.poster_name = fix_encoding(row['poster_name'])
|
||||||
|
self.num_replies = row['num_replies']
|
||||||
|
|
||||||
class Post():
|
class Post():
|
||||||
def __init__ (self, row):
|
def __init__ (self, row):
|
||||||
|
@ -11,7 +11,11 @@ ul.boards { margin-left: 0; padding-left: 0; }
|
|||||||
.label { font-weight: bold }
|
.label { font-weight: bold }
|
||||||
article { border-top: 1px solid black; }
|
article { border-top: 1px solid black; }
|
||||||
section { margin-top: 15px; margin-bottom: 15px; }
|
section { margin-top: 15px; margin-bottom: 15px; }
|
||||||
|
|
||||||
.next { float: right; }
|
.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 { padding-top: 15px; }
|
||||||
.page table { width: 100%; }
|
.page table { width: 100%; }
|
@ -1,4 +1,9 @@
|
|||||||
<div class="pagination">
|
<div class="pagination">
|
||||||
<a class="prev" href="page-{{prev}}.html">Previous Page</a>
|
<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>
|
<a class="next" href="page-{{next}}.html">Next Page</a>
|
||||||
</div>
|
</div>
|
@ -3,14 +3,16 @@
|
|||||||
<table id="threads">
|
<table id="threads">
|
||||||
<tr>
|
<tr>
|
||||||
<th>Title</th>
|
<th>Title</th>
|
||||||
<th>Poster</th>
|
<th>Poster</th>
|
||||||
<th>Date</th>
|
<th>Date</th>
|
||||||
|
<th>Replies</th>
|
||||||
</tr>
|
</tr>
|
||||||
{{#threads}}
|
{{#threads}}
|
||||||
<tr>
|
<tr>
|
||||||
<td class="thread-subject"><a href="thread-{{id}}">{{subject}}</a></td>
|
<td class="thread-subject"><a href="thread-{{id}}">{{subject}}</a></td>
|
||||||
<td class="thread-poster">{{poster_name}}</td>
|
<td class="thread-poster">{{poster_name}}</td>
|
||||||
<td class="thread-date">{{datetime}}</td>
|
<td class="thread-date">{{datetime}}</td>
|
||||||
|
<td class="replies">{{num_replies}}</td>
|
||||||
</tr>
|
</tr>
|
||||||
{{/threads}}
|
{{/threads}}
|
||||||
</table>
|
</table>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user