From d7db0af8f7ce9e608180047bbbdc4fe9dd624f50 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Fri, 20 Aug 2021 01:48:50 +0000 Subject: pagination and links and things (probably not complete) --- apioforum/__init__.py | 1 + apioforum/templates/view_forum.html | 4 ++-- apioforum/templates/view_post.html | 2 +- apioforum/thread.py | 24 ++++++++++++++++-------- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/apioforum/__init__.py b/apioforum/__init__.py index 087df81..60777fb 100644 --- a/apioforum/__init__.py +++ b/apioforum/__init__.py @@ -54,6 +54,7 @@ def create_app(): return dict(path_for_next=p) app.jinja_env.globals.update(forum_path=forum.forum_path) + app.jinja_env.globals.update(post_jump=thread.post_jump) from .roles import has_permission, is_bureaucrat, get_user_role app.jinja_env.globals.update( has_permission=has_permission, diff --git a/apioforum/templates/view_forum.html b/apioforum/templates/view_forum.html index dec9234..a5e444e 100644 --- a/apioforum/templates/view_forum.html +++ b/apioforum/templates/view_forum.html @@ -112,7 +112,7 @@ you do not have permission to create threads in this forum {{ ts(thread.mrp_created) }} - + {{ thread.mrp_content[:500]|e }} @@ -120,7 +120,7 @@ you do not have permission to create threads in this forum {% else %}
+ href="{{post_jump(thread.mrp_id)}}"> latest post
diff --git a/apioforum/templates/view_post.html b/apioforum/templates/view_post.html index 993c005..fcaf29b 100644 --- a/apioforum/templates/view_post.html +++ b/apioforum/templates/view_post.html @@ -8,5 +8,5 @@ {{disp_post(post,False)}}

post source:

-i am satisfied +i am satisfied {% endblock %} diff --git a/apioforum/thread.py b/apioforum/thread.py index 0b12141..f3bcc59 100644 --- a/apioforum/thread.py +++ b/apioforum/thread.py @@ -14,7 +14,7 @@ bp = Blueprint("thread", __name__, url_prefix="/thread") POSTS_PER_PAGE = 20 -def which_page(post_id): +def which_page(post_id,return_thread_id=False): # on which page lieth the post in question? # forget not that page numbers employeth a system that has a base of 1. # the @@ -25,14 +25,22 @@ def which_page(post_id): 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'] + # this is probably a reasonable assumption + + thread_id = db.execute('select thread from posts where id = ?',(post_id,)).fetchone()['thread'] - return 1+math.floor(number_of_things_before_the_thing/POSTS_PER_PAGE) + number_of_things_before_the_thing = db.execute('select count(*) as c, thread as t from posts where thread = ? and id < ?;',(thread_id,post_id)).fetchone()['c'] + + page = 1+math.floor(number_of_things_before_the_thing/POSTS_PER_PAGE) + if return_thread_id: + return page, thread_id + else: + return page -def post_jump(thread_id, post_id): - return url_for("thread.view_thread",thread_id=thread_id)+"#post_"+str(post_id) +def post_jump(post_id): + page,thread_id=which_page(post_id,True) + return url_for("thread.view_thread",thread_id=thread_id,page=page)+"#post_"+str(post_id) @bp.route("/") @bp.route("//page/") @@ -248,7 +256,7 @@ def create_post(thread_id): ) db.commit() flash("post posted postfully") - return redirect(post_jump(thread_id, post_id)) + return redirect(post_jump(post_id)) return redirect(url_for('thread.view_thread',thread_id=thread_id)) @bp.route("/delete_post/", methods=["GET","POST"]) @@ -320,7 +328,7 @@ def edit_post(post_id): "UPDATE posts SET content = ?, edited = 1, updated = current_timestamp WHERE id = ?",(newcontent,post_id)) db.commit() flash("post edited editiously") - return redirect(post_jump(post['thread'],post_id)) + return redirect(post_jump(post_id)) else: flash(err) return render_template("edit_post.html",post=post) -- cgit v1.2.3