Paginate thread list
This commit is contained in:
@@ -35,15 +35,18 @@ class DB:
|
||||
(forum_id,)
|
||||
).fetchone()
|
||||
|
||||
def get_threads(self, forum_id):
|
||||
def get_threads(self, forum_id, offset, limit):
|
||||
return self._db().execute('''
|
||||
select t.thread_id, title, t.create_time, t.update_time, t.author_id, name, count(c.thread_id)
|
||||
from threads t, users
|
||||
left join comments c on t.thread_id = c.thread_id
|
||||
where forum_id = ? and user_id = t.author_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):
|
||||
|
||||
12
main.py
12
main.py
@@ -1,4 +1,6 @@
|
||||
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 db.sqlite import DB
|
||||
@@ -39,7 +41,13 @@ def index():
|
||||
@app.route('/forum/<int:forum_id>/')
|
||||
def 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(
|
||||
'forum.html',
|
||||
title = title,
|
||||
@@ -48,6 +56,8 @@ def forum(forum_id):
|
||||
forum_id = forum_id,
|
||||
description = description,
|
||||
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>/')
|
||||
|
||||
@@ -1,8 +1,17 @@
|
||||
{% 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><a href="{{ url_for('new_thread', forum_id = forum_id) }}">Create thread</a></p>
|
||||
{{- nav() -}}
|
||||
<table>
|
||||
<tr>
|
||||
<th>Topic</th>
|
||||
@@ -19,6 +28,7 @@
|
||||
<td>{{ format_since(utime) }}</td>
|
||||
<td>{{ comment_count }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
{%- endfor -%}
|
||||
</table>
|
||||
{{- nav() -}}
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user