use new pagination system in /comments

master
Aevann 2023-05-05 01:15:13 +03:00
parent 525c69248f
commit 081d0065a7
4 changed files with 71 additions and 86 deletions

View File

@ -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() word = word.replace('\\', '').replace('_', '\_').replace('%', '\%').strip()
posts=posts.filter(not_(Submission.title.ilike(f'%{word}%'))) posts=posts.filter(not_(Submission.title.ilike(f'%{word}%')))
next_exists = posts.count()
posts = sort_objects(sort, posts, Submission) posts = sort_objects(sort, posts, Submission)
if v: size = v.frontsize or 0 if v: size = v.frontsize or 0
else: size = PAGE_SIZE else: size = PAGE_SIZE
next_exists = posts.count()
posts = posts.options(load_only(Submission.id)).offset(size * (page - 1)) posts = posts.options(load_only(Submission.id)).offset(size * (page - 1))
if SITE_NAME == 'WPD' and sort == "hot" and sub == None: if SITE_NAME == 'WPD' and sort == "hot" and sub == None:
@ -180,8 +181,9 @@ def random_user(v:User):
@cache.memoize() @cache.memoize()
def comment_idlist(v=None, page=1, sort="new", t="day", gt=0, lt=0): 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) \ .outerjoin(Comment.post) \
.options(load_only(Comment.id)) \
.filter( .filter(
or_(Comment.parent_submission != None, Comment.wall_user_id != None), 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: if not gt and not lt:
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()
return [x[0] for x in comments] return [x.id for x in comments], next_exists
@app.get("/comments") @app.get("/comments")
@limiter.limit(DEFAULT_RATELIMIT) @limiter.limit(DEFAULT_RATELIMIT)
@ -221,7 +224,7 @@ def all_comments(v:User):
try: lt=int(request.values.get("before", 0)) try: lt=int(request.values.get("before", 0))
except: lt=0 except: lt=0
idlist = comment_idlist(v=v, idlist, next_exists = comment_idlist(v=v,
page=page, page=page,
sort=sort, sort=sort,
t=t, t=t,
@ -230,8 +233,6 @@ def all_comments(v:User):
) )
comments = get_comments(idlist, v=v) 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]} 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)

View File

@ -154,65 +154,7 @@
{% block pagenav %} {% block pagenav %}
{% if listing %} {% if listing %}
<nav> {% include "pagination.html" %}
<ul class="pagination pagination-sm mb-0 mt-4 pl-1">
{% set num_pages = (next_exists / size) | round(0, 'ceil') | int %}
{% set start_point = page - 2 %}
{% if start_point < 1 %}
{% set start_point = 1 %}
{% endif %}
{% set end_point = start_point+4 %}
{% if end_point > num_pages %}
{% set start_point = start_point-(end_point-num_pages) %}
{% set end_point = num_pages %}
{% endif %}
{% if start_point < 1 %}
{% set start_point = 1 %}
{% endif %}
{% if start_point > 1 %}
<li class="page-item">
<small><a class="page-link" href="?sort={{sort}}&page=1&t={{t}}">1</a></small>
</li>
{% if start_point == 3 %}
<li class="page-item">
<small><a class="page-link" href="?sort={{sort}}&page=2&t={{t}}">2</a></small>
</li>
{% elif start_point != 2 %}
<li class="page-item">
<small class="page-link dots">...</small>
</li>
{% endif %}
{% endif %}
{% for x in range(start_point, end_point+1) %}
<li class="page-item">
<small><a class="page-link {% if x == page %}active{% endif %}" href="?sort={{sort}}&page={{x}}&t={{t}}">{{x}}</a></small>
</li>
{% endfor %}
{% if end_point < num_pages %}
{% if end_point == num_pages-2 %}
<li class="page-item">
<small><a class="page-link" href="?sort={{sort}}&page={{num_pages-1}}&t={{t}}">{{num_pages-1}}</a></small>
</li>
{% elif end_point != num_pages-1 %}
<li class="page-item">
<small class="page-link dots">...</small>
</li>
{% endif %}
<li class="page-item">
<small><a class="page-link" href="?sort={{sort}}&page={{num_pages}}&t={{t}}">{{num_pages}}</a></small>
</li>
{% endif %}
</ul>
</nav>
{% endif %} {% endif %}
{% if request.path == '/' and v %} {% if request.path == '/' and v %}

View File

@ -67,22 +67,5 @@
{% endblock %} {% endblock %}
{% block pagenav %} {% block pagenav %}
<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>
{% endblock %} {% endblock %}

View File

@ -0,0 +1,59 @@
<nav>
<ul class="pagination pagination-sm mb-0 mt-4 pl-1">
{% set num_pages = (next_exists / size) | round(0, 'ceil') | int %}
{% set start_point = page - 2 %}
{% if start_point < 1 %}
{% set start_point = 1 %}
{% endif %}
{% set end_point = start_point+4 %}
{% if end_point > num_pages %}
{% set start_point = start_point-(end_point-num_pages) %}
{% set end_point = num_pages %}
{% endif %}
{% if start_point < 1 %}
{% set start_point = 1 %}
{% endif %}
{% if start_point > 1 %}
<li class="page-item">
<small><a class="page-link" href="?sort={{sort}}&page=1&t={{t}}">1</a></small>
</li>
{% if start_point == 3 %}
<li class="page-item">
<small><a class="page-link" href="?sort={{sort}}&page=2&t={{t}}">2</a></small>
</li>
{% elif start_point != 2 %}
<li class="page-item">
<small class="page-link dots">...</small>
</li>
{% endif %}
{% endif %}
{% for x in range(start_point, end_point+1) %}
<li class="page-item">
<small><a class="page-link {% if x == page %}active{% endif %}" href="?sort={{sort}}&page={{x}}&t={{t}}">{{x}}</a></small>
</li>
{% endfor %}
{% if end_point < num_pages %}
{% if end_point == num_pages-2 %}
<li class="page-item">
<small><a class="page-link" href="?sort={{sort}}&page={{num_pages-1}}&t={{t}}">{{num_pages-1}}</a></small>
</li>
{% elif end_point != num_pages-1 %}
<li class="page-item">
<small class="page-link dots">...</small>
</li>
{% endif %}
<li class="page-item">
<small><a class="page-link" href="?sort={{sort}}&page={{num_pages}}&t={{t}}">{{num_pages}}</a></small>
</li>
{% endif %}
</ul>
</nav>