From 081d0065a795158b6f052dbcba49b5519b6ddab4 Mon Sep 17 00:00:00 2001 From: Aevann Date: Fri, 5 May 2023 01:15:13 +0300 Subject: [PATCH] use new pagination system in /comments --- files/routes/front.py | 19 +++++----- files/templates/home.html | 60 +----------------------------- files/templates/home_comments.html | 19 +--------- files/templates/pagination.html | 59 +++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 86 deletions(-) create mode 100644 files/templates/pagination.html diff --git a/files/routes/front.py b/files/routes/front.py index d3859e130b..cc3ee874b4 100644 --- a/files/routes/front.py +++ b/files/routes/front.py @@ -108,12 +108,13 @@ def frontlist(v=None, sort="hot", page=1, t="all", ids_only=True, filter_words=' word = word.replace('\\', '').replace('_', '\_').replace('%', '\%').strip() posts=posts.filter(not_(Submission.title.ilike(f'%{word}%'))) + next_exists = posts.count() + posts = sort_objects(sort, posts, Submission) if v: size = v.frontsize or 0 else: size = PAGE_SIZE - next_exists = posts.count() posts = posts.options(load_only(Submission.id)).offset(size * (page - 1)) if SITE_NAME == 'WPD' and sort == "hot" and sub == None: @@ -180,8 +181,9 @@ def random_user(v:User): @cache.memoize() def comment_idlist(v=None, page=1, sort="new", t="day", gt=0, lt=0): - comments = g.db.query(Comment.id) \ + comments = g.db.query(Comment) \ .outerjoin(Comment.post) \ + .options(load_only(Comment.id)) \ .filter( or_(Comment.parent_submission != None, Comment.wall_user_id != None), ) @@ -200,10 +202,11 @@ def comment_idlist(v=None, page=1, sort="new", t="day", gt=0, lt=0): if not gt and not lt: comments = apply_time_filter(t, comments, Comment) + next_exists = comments.count() comments = sort_objects(sort, comments, Comment) - - comments = comments.offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE + 1).all() - return [x[0] for x in comments] + + comments = comments.offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE).all() + return [x.id for x in comments], next_exists @app.get("/comments") @limiter.limit(DEFAULT_RATELIMIT) @@ -221,7 +224,7 @@ def all_comments(v:User): try: lt=int(request.values.get("before", 0)) except: lt=0 - idlist = comment_idlist(v=v, + idlist, next_exists = comment_idlist(v=v, page=page, sort=sort, t=t, @@ -230,8 +233,6 @@ def all_comments(v:User): ) comments = get_comments(idlist, v=v) - next_exists = len(idlist) > PAGE_SIZE - idlist = idlist[:PAGE_SIZE] if v.client: return {"data": [x.json(g.db) for x in comments]} - return render_template("home_comments.html", v=v, sort=sort, t=t, page=page, comments=comments, standalone=True, next_exists=next_exists) + return render_template("home_comments.html", v=v, sort=sort, t=t, page=page, comments=comments, standalone=True, next_exists=next_exists, size = PAGE_SIZE) diff --git a/files/templates/home.html b/files/templates/home.html index 6935171889..195f651d08 100644 --- a/files/templates/home.html +++ b/files/templates/home.html @@ -154,65 +154,7 @@ {% block pagenav %} {% if listing %} - + {% include "pagination.html" %} {% endif %} {% if request.path == '/' and v %} diff --git a/files/templates/home_comments.html b/files/templates/home_comments.html index 8b52f10836..2326bb9d0a 100644 --- a/files/templates/home_comments.html +++ b/files/templates/home_comments.html @@ -67,22 +67,5 @@ {% endblock %} {% block pagenav %} - +{% include "pagination.html" %} {% endblock %} diff --git a/files/templates/pagination.html b/files/templates/pagination.html new file mode 100644 index 0000000000..7a0a388626 --- /dev/null +++ b/files/templates/pagination.html @@ -0,0 +1,59 @@ +