From 84ab4edfd7257683665c6908eaa951297d881b72 Mon Sep 17 00:00:00 2001 From: Jmzd8 Date: Fri, 25 Feb 2022 18:00:15 +0000 Subject: added pagination to user pages --- apioforum/templates/view_user.html | 5 +++-- apioforum/user.py | 28 +++++++++++++++++++++------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/apioforum/templates/view_user.html b/apioforum/templates/view_user.html index 612c2c0..44a0b9d 100644 --- a/apioforum/templates/view_user.html +++ b/apioforum/templates/view_user.html @@ -1,4 +1,4 @@ -{% from 'common.html' import disp_post,ts %} +{% from 'common.html' import disp_post,ts,tag,thread_breadcrumb,pagination_nav %} {% extends 'base.html' %} {% block header %}

{%block title %}{{user.username|e}}{% endblock %}

@@ -31,5 +31,6 @@ {{ disp_post(post, False) }} {% endfor %} +{{ pagination_nav(page,max_pageno,'user.view_user', username=user.username) }} {% endif %} -{% endblock %} +{% endblock %} \ No newline at end of file diff --git a/apioforum/user.py b/apioforum/user.py index bbdd060..1a884aa 100644 --- a/apioforum/user.py +++ b/apioforum/user.py @@ -1,4 +1,5 @@ # user pages +POSTS_PER_PAGE = 20 from flask import ( Blueprint, render_template, abort, g, flash, redirect, url_for, request @@ -6,22 +7,35 @@ from flask import ( from werkzeug.security import check_password_hash, generate_password_hash from .db import get_db +import math bp = Blueprint("user", __name__, url_prefix="/user") - - + @bp.route("/") -def view_user(username): +@bp.route("//page/") +def view_user(username, page=1): + if page < 1: + abort(400) + db = get_db() user = db.execute("SELECT * FROM users WHERE username = ?;",(username,)).fetchone() + if user is None: abort(404) posts = db.execute(""" SELECT * FROM posts WHERE author = ? AND deleted = 0 - ORDER BY created DESC - LIMIT 25;""",(username,)).fetchall() - return render_template("view_user.html", user=user, posts=posts) + ORDER BY created DESC + LIMIT ? OFFSET ?;""",(username,POSTS_PER_PAGE,(page-1)*POSTS_PER_PAGE,)).fetchall() + num_posts = db.execute("SELECT count(*) as count FROM posts WHERE author = ?;",(username,)).fetchone()['count'] + max_pageno = math.ceil(num_posts/POSTS_PER_PAGE) + return render_template( + "view_user.html", + user=user, + posts=posts, + page=page, + max_pageno=max_pageno, + ) @bp.route("//edit", methods=["GET","POST"]) def edit_user(username): @@ -59,4 +73,4 @@ def edit_user(username): else: return redirect(url_for("user.view_user",username=username)) - return render_template("user_settings.html",user=user) + return render_template("user_settings.html",user=user) \ No newline at end of file -- cgit v1.2.3