Paginate thread list

This commit is contained in:
David Hoppenbrouwers
2022-10-09 16:09:21 +02:00
parent 3bcbbf844d
commit c17a76d18f
3 changed files with 28 additions and 5 deletions

View File

@@ -35,15 +35,18 @@ class DB:
(forum_id,) (forum_id,)
).fetchone() ).fetchone()
def get_threads(self, forum_id): def get_threads(self, forum_id, offset, limit):
return self._db().execute(''' return self._db().execute('''
select t.thread_id, title, t.create_time, t.update_time, t.author_id, name, count(c.thread_id) select t.thread_id, title, t.create_time, t.update_time, t.author_id, name, count(c.thread_id)
from threads t, users from threads t, users
left join comments c on t.thread_id = c.thread_id left join comments c on t.thread_id = c.thread_id
where forum_id = ? and user_id = t.author_id where forum_id = ? and user_id = t.author_id
group by t.thread_id group by t.thread_id
order by t.update_time desc
limit ?
offset ?
''', ''',
(forum_id,) (forum_id, limit, offset)
) )
def get_thread(self, thread): def get_thread(self, thread):

12
main.py
View File

@@ -1,4 +1,6 @@
VERSION = 'agreper-v0.1' VERSION = 'agreper-v0.1'
# TODO put in config table
THREADS_PER_PAGE = 50
from flask import Flask, render_template, session, request, redirect, url_for, flash, g from flask import Flask, render_template, session, request, redirect, url_for, flash, g
from db.sqlite import DB from db.sqlite import DB
@@ -39,7 +41,13 @@ def index():
@app.route('/forum/<int:forum_id>/') @app.route('/forum/<int:forum_id>/')
def forum(forum_id): def forum(forum_id):
title, description = db.get_forum(forum_id) title, description = db.get_forum(forum_id)
threads = db.get_threads(forum_id) offset = int(request.args.get('p', 0))
threads = [*db.get_threads(forum_id, offset, THREADS_PER_PAGE + 1)]
if len(threads) == THREADS_PER_PAGE + 1:
threads.pop()
next_page = offset + THREADS_PER_PAGE
else:
next_page = None
return render_template( return render_template(
'forum.html', 'forum.html',
title = title, title = title,
@@ -48,6 +56,8 @@ def forum(forum_id):
forum_id = forum_id, forum_id = forum_id,
description = description, description = description,
threads = threads, threads = threads,
next_page = next_page,
prev_page = max(offset - THREADS_PER_PAGE, 0) if offset > 0 else None,
) )
@app.route('/thread/<int:thread_id>/') @app.route('/thread/<int:thread_id>/')

View File

@@ -1,8 +1,17 @@
{% extends 'base.html' %} {% extends 'base.html' %}
{% block content %} {%- macro nav() -%}
<p style=text-align:center>
{%- if prev_page is not none %}<a href="./?p={{ prev_page }}">prev</a>{% endif -%}
{%- if prev_page is not none and next_page is not none %} | {% endif -%}
{%- if next_page is not none %}<a href="./?p={{ next_page }}">next</a>{% endif -%}
</p>
{%- endmacro -%}
{% block content -%}
<p>{{ description }}</p> <p>{{ description }}</p>
<p><a href="{{ url_for('new_thread', forum_id = forum_id) }}">Create thread</a></p> <p><a href="{{ url_for('new_thread', forum_id = forum_id) }}">Create thread</a></p>
{{- nav() -}}
<table> <table>
<tr> <tr>
<th>Topic</th> <th>Topic</th>
@@ -19,6 +28,7 @@
<td>{{ format_since(utime) }}</td> <td>{{ format_since(utime) }}</td>
<td>{{ comment_count }}</td> <td>{{ comment_count }}</td>
</tr> </tr>
{% endfor %} {%- endfor -%}
</table> </table>
{{- nav() -}}
{% endblock %} {% endblock %}