From a2e408daf9d0155f1acc43e360da5ac5c09cb783 Mon Sep 17 00:00:00 2001 From: Darks Date: Sat, 7 Dec 2019 16:06:00 +0100 Subject: [PATCH] Ajout du load dynmaique des messages (#39) --- app/models/comment.py | 4 +++- app/routes/forum/topic.py | 10 +++++++++- app/templates/forum/topic.html | 4 ++-- config.py | 2 ++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/models/comment.py b/app/models/comment.py index b9e60f2..a27617c 100644 --- a/app/models/comment.py +++ b/app/models/comment.py @@ -1,5 +1,6 @@ from app import db from app.models.post import Post +from sqlalchemy.orm import backref class Comment(Post): __tablename__ = 'comment' @@ -14,7 +15,8 @@ class Comment(Post): # Parent thread thread_id = db.Column(db.Integer, db.ForeignKey('thread.id'), nullable=False) - thread = db.relationship('Thread', backref='comments', + thread = db.relationship('Thread', + backref=backref('comments', lazy='dynamic'), foreign_keys=thread_id) def __init__(self, author, text, thread): diff --git a/app/routes/forum/topic.py b/app/routes/forum/topic.py index bf02827..dcebd61 100644 --- a/app/routes/forum/topic.py +++ b/app/routes/forum/topic.py @@ -38,4 +38,12 @@ def forum_topic(f, t): db.session.merge(t) db.session.commit() - return render('/forum/topic.html', t=t, form=form) + if request.args.get('page') == "last": + page = t.thread.comments.count() // V5Config.COMMENTS_PER_PAGE + 1 + else: + page = request.args.get('page', 1, type=int) + + comments = t.thread.comments.paginate(page, + V5Config.COMMENTS_PER_PAGE, True) + + return render('/forum/topic.html', t=t, form=form, comments=comments) diff --git a/app/templates/forum/topic.html b/app/templates/forum/topic.html index 456b34d..0008d36 100644 --- a/app/templates/forum/topic.html +++ b/app/templates/forum/topic.html @@ -15,7 +15,7 @@ - {% for i, c in enumerate(t.thread.comments) %} + {% for c in comments.items %} {% if c != t.thread.top_comment %} @@ -27,7 +27,7 @@ {% endif %}

{{ c.text }}

- {% elif i != 0 %} + {% elif loop.index0 != 0 %}
Ce message est le top comment
{% endif %} diff --git a/config.py b/config.py index 1b50051..65b2ae0 100644 --- a/config.py +++ b/config.py @@ -25,6 +25,8 @@ class V5Config(LocalConfig): PASSWORD_MINLEN = 10 # Maximum thread name length THREAD_NAME_MAXLEN = 32 + # Amount of comments per thread page + COMMENTS_PER_PAGE = 20 # Remember-me cookie duration time REMEMBER_COOKIE_DURATION = datetime.timedelta(days=7) # XP points for content posting (and deletion)
{{ widget_member.profile(c.author ) }}