diff --git a/db/sqlite.py b/db/sqlite.py index 802bc62..bf0fc4d 100644 --- a/db/sqlite.py +++ b/db/sqlite.py @@ -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): diff --git a/main.py b/main.py index df0ef74..9e67c05 100644 --- a/main.py +++ b/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//') 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//') diff --git a/templates/forum.html b/templates/forum.html index 22c440b..102a386 100644 --- a/templates/forum.html +++ b/templates/forum.html @@ -1,8 +1,17 @@ {% extends 'base.html' %} -{% block content %} +{%- macro nav() -%} +

+{%- if prev_page is not none %}prev{% endif -%} +{%- if prev_page is not none and next_page is not none %} | {% endif -%} +{%- if next_page is not none %}next{% endif -%} +

+{%- endmacro -%} + +{% block content -%}

{{ description }}

Create thread

+{{- nav() -}} @@ -19,6 +28,7 @@ - {% endfor %} + {%- endfor -%}
Topic{{ format_since(utime) }} {{ comment_count }}
+{{- nav() -}} {% endblock %}