forum: add pagination to topic list (#64)
This commit is contained in:
parent
a83cef5970
commit
3a35c26bd7
|
@ -22,6 +22,9 @@ class Forum(db.Model):
|
|||
# Other fields populated automatically through relations:
|
||||
# <topics> 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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -18,7 +18,8 @@ def forum_index():
|
|||
return render('/forum/index.html')
|
||||
|
||||
@app.route('/forum/<forum:f>/', methods=['GET', 'POST'])
|
||||
def forum_page(f):
|
||||
@app.route('/forum/<forum:f>/p/<int:page>', 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)
|
||||
|
|
|
@ -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 %}
|
||||
<a href='/forum'>Forum de Planète Casio</a> » <h1>{{ f.name }}</h1>
|
||||
|
@ -9,13 +10,16 @@
|
|||
<section>
|
||||
<p>{{ f.descr }}</p>
|
||||
|
||||
{% if f.topics %}
|
||||
{% if topics.items %}
|
||||
<h2>Sujets</h2>
|
||||
|
||||
{{ widget_pagination.paginate(topics, 'forum_page', None, {'f': f}) }}
|
||||
|
||||
<table class=topiclist>
|
||||
<tr><th>Sujet</th><th>Auteur</th><th>Date de création</th>
|
||||
<th>Commentaires</th><th>Vues</th></tr>
|
||||
<th>Commentaires</th><th>Vues</th></tr>
|
||||
|
||||
{% for t in f.topics %}
|
||||
{% for t in topics.items %}
|
||||
<tr><td><a href='{{ url_for('forum_topic', f=t.forum, page=(t,1)) }}'>{{ t.title }}</a></td>
|
||||
<td><a href='{{ url_for('user', username=t.author.name) }}'>{{ t.author.name }}</a></td>
|
||||
<td>{{ t.date_created | date }}</td>
|
||||
|
@ -23,8 +27,11 @@
|
|||
<td>{{ t.views }} </td></tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
|
||||
{{ widget_pagination.paginate(topics, 'forum_page', None, {'f': f}) }}
|
||||
|
||||
{% elif not f.sub_forums %}
|
||||
<p>Il n'y a aucun topic sur ce forum ! Animons-le vite !</p>
|
||||
<p>Il n'y a aucun topic sur ce forum ! Animons-le vite !</p>
|
||||
{% endif %}
|
||||
|
||||
{% if f.sub_forums %}
|
||||
|
@ -34,7 +41,7 @@
|
|||
|
||||
{% for sf in f.sub_forums %}
|
||||
<tr><td><a href='/forum{{ sf.url }}'>{{ sf.name }}</td>
|
||||
<td>{{ sf.topics | length }}</td></tr>
|
||||
<td>{{ sf.topics.count() }}</td></tr>
|
||||
<tr><td>{{ sf.descr }}</td><td></td></tr>
|
||||
{% endfor %}
|
||||
|
||||
|
|
|
@ -1,21 +1,33 @@
|
|||
{% macro paginate(objects, route, obj, route_args) %}
|
||||
<div class="pagination">
|
||||
{% if objects.has_prev %}
|
||||
<a href="{{ _url_for(route, route_args, page=(obj,objects.prev_num)) }}">Page précédente</a> |
|
||||
{% if obj %}
|
||||
<a href="{{ _url_for(route, route_args, page=(obj,objects.prev_num)) }}">Page précédente</a> |
|
||||
{% else %}
|
||||
<a href="{{ _url_for(route, route_args, page=objects.prev_num) }}">Page précédente</a> |
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
|
||||
{% for page in objects.iter_pages(1, 5, 6, 1) %}
|
||||
{% if not page %}
|
||||
…
|
||||
{% elif page != objects.page %}
|
||||
<a href="{{ _url_for(route, route_args, page=(obj,page)) }}">{{ page }}</a>
|
||||
{% if obj %}
|
||||
<a href="{{ _url_for(route, route_args, page=(obj,page)) }}">{{ page }}</a>
|
||||
{% else %}
|
||||
<a href="{{ _url_for(route, route_args, page=page) }}">{{ page }}</a>
|
||||
{% endif %}
|
||||
{% else %}
|
||||
<strong>{{ page }}</strong>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
|
||||
{% if objects.has_next %}
|
||||
| <a href="{{ _url_for(route, route_args, page=(obj,objects.next_num)) }}">Page suivante</a>
|
||||
{% if obj %}
|
||||
| <a href="{{ _url_for(route, route_args, page=(obj,objects.next_num)) }}">Page suivante</a>
|
||||
{% else %}
|
||||
| <a href="{{ _url_for(route, route_args, page=objects.next_num) }}">Page suivante</a>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endmacro %}
|
||||
|
|
Loading…
Reference in New Issue