diff options
author | ubq323 <ubq323> | 2021-08-04 00:04:15 +0000 |
---|---|---|
committer | ubq323 <ubq323> | 2021-08-04 00:04:15 +0000 |
commit | c51c87b6d012c2a250054b47e330e1c504aebb4a (patch) | |
tree | 3815b6163b362fcbf87874a0ad9741c06b40af53 | |
parent | 40c337c3133410857b707689eec6d6eaf41951bc (diff) |
pagination (navbar will be done later)
-rw-r--r-- | apioforum/thread.py | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/apioforum/thread.py b/apioforum/thread.py index 991dc0b..415a45c 100644 --- a/apioforum/thread.py +++ b/apioforum/thread.py @@ -1,6 +1,6 @@ # view posts in thread -import itertools +import itertools, math from flask import ( Blueprint, render_template, abort, request, g, redirect, @@ -11,11 +11,33 @@ from .forum import get_avail_tags bp = Blueprint("thread", __name__, url_prefix="/thread") +POSTS_PER_PAGE = 20 + +def which_page(post_id): + # on which page lieth the post in question? + # forget not that page numbers employeth a system that has a base of 1. + # the + # we need impart the knowledgf e into ourselves pertaining to the + # number of things + # before the thing + # yes + + db = get_db() + # ASSUMES THAT post ids are consecutive and things + # this is probably a reasonable assumption + number_of_things_before_the_thing = db.execute('select count(*) as c from posts where thread = (select thread from posts where id = ?) and id < ?;',(post_id,post_id)).fetchone()['c'] + + return 1+math.floor(number_of_things_before_the_thing/POSTS_PER_PAGE) + + def post_jump(thread_id, post_id): return url_for("thread.view_thread",thread_id=thread_id)+"#post_"+str(post_id) @bp.route("/<int:thread_id>") -def view_thread(thread_id): +@bp.route("/<int:thread_id>/page/<int:page>") +def view_thread(thread_id,page=1): + if page < 1: + abort(400) db = get_db() thread = db.execute("SELECT * FROM threads WHERE id = ?;",(thread_id,)).fetchone() if thread is None: @@ -24,8 +46,13 @@ def view_thread(thread_id): posts = db.execute(""" SELECT * FROM posts WHERE posts.thread = ? - ORDER BY created ASC; - """,(thread_id,)).fetchall() + ORDER BY created ASC + LIMIT ? OFFSET ?; + """,( + thread_id, + POSTS_PER_PAGE, + (page-1)*POSTS_PER_PAGE, + )).fetchall() tags = db.execute( """SELECT tags.* FROM tags INNER JOIN thread_tags ON thread_tags.tag = tags.id |