forked from rDrama/rDrama
1
0
Fork 0

implement new pagination system in profile pages

master
Aevann 2023-05-05 04:04:07 +03:00
parent 0df074c974
commit 74bc00199d
2 changed files with 13 additions and 35 deletions

View File

@ -7,7 +7,7 @@ from typing import Literal
import gevent import gevent
import qrcode import qrcode
from sqlalchemy.orm import aliased from sqlalchemy.orm import aliased, load_only
from files.classes import * from files.classes import *
from files.classes.leaderboard import Leaderboard from files.classes.leaderboard import Leaderboard
@ -806,13 +806,14 @@ def visitors(v:User, username:str):
@cache.memoize() @cache.memoize()
def userpagelisting(user:User, v=None, page:int=1, sort="new", t="all"): def userpagelisting(user:User, v=None, page:int=1, sort="new", t="all"):
posts = g.db.query(Submission.id).filter_by(author_id=user.id, is_pinned=False) posts = g.db.query(Submission).filter_by(author_id=user.id, is_pinned=False).options(load_only(Submission.id))
if not (v and (v.admin_level >= PERMS['POST_COMMENT_MODERATION'] or v.id == user.id)): if not (v and (v.admin_level >= PERMS['POST_COMMENT_MODERATION'] or v.id == user.id)):
posts = posts.filter_by(is_banned=False, private=False, ghost=False, deleted_utc=0) posts = posts.filter_by(is_banned=False, private=False, ghost=False, deleted_utc=0)
posts = apply_time_filter(t, posts, Submission) posts = apply_time_filter(t, posts, Submission)
next_exists = posts.count()
posts = sort_objects(sort, posts, Submission) posts = sort_objects(sort, posts, Submission)
posts = posts.offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE+1).all() posts = posts.offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE).all()
return [x[0] for x in posts] return [x.id for x in posts], next_exists
@app.get("/@<username>") @app.get("/@<username>")
@limiter.limit(DEFAULT_RATELIMIT) @limiter.limit(DEFAULT_RATELIMIT)
@ -851,14 +852,12 @@ def u_username_wall(v:Optional[User], username:str):
Comment.deleted_utc == 0 Comment.deleted_utc == 0
) )
next_exists = comments.count()
comments = comments.order_by(Comment.created_utc.desc()) \ comments = comments.order_by(Comment.created_utc.desc()) \
.offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE+1).all() .offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE).all()
if v: if v:
comments = [c[0] for c in comments] comments = [c[0] for c in comments]
next_exists = (len(comments) > PAGE_SIZE)
comments = comments[:PAGE_SIZE]
if v and v.client: if v and v.client:
return {"data": [c.json(g.db) for c in comments]} return {"data": [c.json(g.db) for c in comments]}
@ -946,10 +945,7 @@ def u_username(v:Optional[User], username:str):
try: page = max(int(request.values.get("page", 1)), 1) try: page = max(int(request.values.get("page", 1)), 1)
except: page = 1 except: page = 1
ids = userpagelisting(u, v=v, page=page, sort=sort, t=t) ids, next_exists = userpagelisting(u, v=v, page=page, sort=sort, t=t)
next_exists = (len(ids) > PAGE_SIZE)
ids = ids[:PAGE_SIZE]
if page == 1 and sort == 'new': if page == 1 and sort == 'new':
sticky = [] sticky = []
@ -1022,7 +1018,7 @@ def u_username_comments(username, v=None):
t=request.values.get("t","all") t=request.values.get("t","all")
comment_post_author = aliased(User) comment_post_author = aliased(User)
comments = g.db.query(Comment.id) \ comments = g.db.query(Comment).options(load_only(Comment.id)) \
.outerjoin(Comment.post) \ .outerjoin(Comment.post) \
.outerjoin(comment_post_author, Submission.author) \ .outerjoin(comment_post_author, Submission.author) \
.filter( .filter(
@ -1039,14 +1035,13 @@ def u_username_comments(username, v=None):
comments = apply_time_filter(t, comments, Comment) comments = apply_time_filter(t, comments, Comment)
next_exists = comments.count()
comments = sort_objects(sort, comments, Comment) comments = sort_objects(sort, comments, Comment)
comments = comments.offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE+1).all() comments = comments.offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE).all()
ids = [x.id for x in comments] ids = [x.id for x in comments]
next_exists = (len(ids) > PAGE_SIZE)
ids = ids[:PAGE_SIZE]
listing = get_comments(ids, v=v) listing = get_comments(ids, v=v)
if v and v.client: if v and v.client:

View File

@ -34,24 +34,7 @@
{% block pagenav %} {% block pagenav %}
{% if listing %} {% if listing %}
<nav> {% include "pagination.html" %}
<ul class="pagination pagination-sm mb-0">
{% if page>1 %}
<li class="page-item">
<small><a class="page-link" href="?page={{page-1}}&sort={{sort}}&t={{t}}" tabindex="-1">Prev</a></small>
</li>
{% else %}
<li class="page-item disabled"><span class="page-link">Prev</span></li>
{% endif %}
{% if next_exists %}
<li class="page-item">
<small><a class="page-link" href="?page={{page+1}}&sort={{sort}}&t={{t}}">Next</a></small>
</li>
{% else %}
<li class="page-item disabled"><span class="page-link">Next</span></li>
{% endif %}
</ul>
</nav>
{% endif %} {% endif %}
{% if not request.path.endswith('/comments') and not request.path.endswith(u.username) %} {% if not request.path.endswith('/comments') and not request.path.endswith(u.username) %}