From 3a35c26bd7acbf495b2a3ebf946b79a1fbe69685 Mon Sep 17 00:00:00 2001 From: Darks Date: Sun, 2 Aug 2020 17:28:43 +0200 Subject: [PATCH] forum: add pagination to topic list (#64) --- app/models/forum.py | 3 +++ app/models/topic.py | 4 +++- app/routes/forum/index.py | 10 ++++++++-- app/templates/forum/forum.html | 17 ++++++++++++----- app/templates/widgets/pagination.html | 18 +++++++++++++++--- 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/app/models/forum.py b/app/models/forum.py index acc2fff..48aaffc 100644 --- a/app/models/forum.py +++ b/app/models/forum.py @@ -22,6 +22,9 @@ class Forum(db.Model): # Other fields populated automatically through relations: # List of topics in this exact forum (of type Topic) + # Some configuration + TOPICS_PER_PAGE = 30 + def __init__(self, url, name, prefix, descr="", parent=None): self.url = url self.name = name diff --git a/app/models/topic.py b/app/models/topic.py index 7b9465f..4afe5db 100644 --- a/app/models/topic.py +++ b/app/models/topic.py @@ -1,5 +1,6 @@ from app import db from app.models.post import Post +from sqlalchemy.orm import backref class Topic(Post): __tablename__ = 'topic' @@ -22,7 +23,8 @@ class Topic(Post): # Parent forum forum_id = db.Column(db.Integer, db.ForeignKey('forum.id'), nullable=False) - forum = db.relationship('Forum', backref='topics',foreign_keys=forum_id) + forum = db.relationship('Forum', + backref=backref('topics', lazy='dynamic'),foreign_keys=forum_id) # Associated thread thread_id = db.Column(db.Integer,db.ForeignKey('thread.id'),nullable=False) diff --git a/app/routes/forum/index.py b/app/routes/forum/index.py index 4e8176d..892ad90 100644 --- a/app/routes/forum/index.py +++ b/app/routes/forum/index.py @@ -18,7 +18,8 @@ def forum_index(): return render('/forum/index.html') @app.route('/forum//', methods=['GET', 'POST']) -def forum_page(f): +@app.route('/forum//p/', methods=['GET', 'POST']) +def forum_page(f, page=1): if current_user.is_authenticated: form = TopicCreationForm() else: @@ -77,4 +78,9 @@ def forum_page(f): flash('Le sujet a bien été créé', 'ok') return redirect(url_for('forum_topic', f=f, page=(t,1))) - return render('/forum/forum.html', f=f, form=form) + # Paginate topic pages + # TODO: order by last comment date + topics = f.topics.order_by(Topic.date_created.desc()).paginate( + page, Forum.TOPICS_PER_PAGE, True) + + return render('/forum/forum.html', f=f, topics=topics, form=form) diff --git a/app/templates/forum/forum.html b/app/templates/forum/forum.html index 1dcac5c..ae59fda 100644 --- a/app/templates/forum/forum.html +++ b/app/templates/forum/forum.html @@ -1,5 +1,6 @@ {% extends "base/base.html" %} {% import "widgets/editor.html" as widget_editor %} +{% import "widgets/pagination.html" as widget_pagination with context %} {% block title %} Forum de Planète Casio »

{{ f.name }}

@@ -9,13 +10,16 @@

{{ f.descr }}

- {% if f.topics %} + {% if topics.items %}

Sujets

+ + {{ widget_pagination.paginate(topics, 'forum_page', None, {'f': f}) }} + - + - {% for t in f.topics %} + {% for t in topics.items %} @@ -23,8 +27,11 @@ {% endfor %}
SujetAuteurDate de créationCommentairesVues
CommentairesVues
{{ t.title }} {{ t.author.name }} {{ t.date_created | date }}{{ t.views }}
+ + {{ widget_pagination.paginate(topics, 'forum_page', None, {'f': f}) }} + {% elif not f.sub_forums %} -

Il n'y a aucun topic sur ce forum ! Animons-le vite !

+

Il n'y a aucun topic sur ce forum ! Animons-le vite !

{% endif %} {% if f.sub_forums %} @@ -34,7 +41,7 @@ {% for sf in f.sub_forums %} {{ sf.name }} - {{ sf.topics | length }} + {{ sf.topics.count() }} {{ sf.descr }} {% endfor %} diff --git a/app/templates/widgets/pagination.html b/app/templates/widgets/pagination.html index 1a3a89e..e12425d 100644 --- a/app/templates/widgets/pagination.html +++ b/app/templates/widgets/pagination.html @@ -1,21 +1,33 @@ {% macro paginate(objects, route, obj, route_args) %} {% endmacro %}