diff options
-rw-r--r-- | apioforum/templates/view_user.html | 5 | ||||
-rw-r--r-- | 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 %} <h1>{%block title %}{{user.username|e}}{% endblock %}</h1> @@ -31,5 +31,6 @@ {{ disp_post(post, False) }} {% endfor %} </div> +{{ 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("/<username>") -def view_user(username): +@bp.route("/<username>/page/<int: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("/<username>/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 |