forum: add the forum index page

Also prepare some functions for topic listings for each forum.
This commit is contained in:
Lephe 2019-09-05 23:42:47 +02:00
parent 35f1335f64
commit 9f30bd36a0
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
6 changed files with 116 additions and 2 deletions

View File

@ -5,12 +5,15 @@ from flask_login import LoginManager
from config import Config
import time
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
from app.utils.converters import *
app.url_map.converters['topicslug'] = TopicSlugConverter
app.url_map.converters['forum'] = ForumConverter
@app.before_request
def request_time():
@ -28,8 +31,11 @@ from app.models.thread import Thread
from app.models.forum import Forum
from app.models.topic import Topic
from app.models.notification import Notification
from app.routes import index, search, users # To load routes at initialization
from app.routes.account import login, account, notification
from app.routes.admin import index, groups, account, trophies, forums
from app.routes.forum import index
from app.utils import pluralize # To use pluralize into the templates
from app.utils import is_title

View File

@ -0,0 +1,8 @@
from app.utils.render import render
from app.models.forum import Forum
from app import app
@app.route('/forum')
def forum():
main_forum = Forum.query.filter_by(parent=None).first()
return render('/forum/index.html', main_forum=main_forum)

View File

@ -17,3 +17,27 @@ table th {
table td {
padding: 4px 6px;
}
/* Forum and sub-forum listings */
table.forumlist {
border-collapse: separate;
border-spacing: 0;
margin: 16px 0;
width: 100%;
}
/* table.forumlist th {
background: #d05950;
border-color: #b04940;
color: white;
} */
table.forumlist tr {
background: unset;
}
table.forumlist tr:nth-child(4n+2),
table.forumlist tr:nth-child(4n+3) {
background: rgba(0, 0, 0, .05);
}

View File

@ -5,6 +5,10 @@
</svg>
Forum
</h2>
<a href='/forum'>Index du forum</a>
<hr>
<a href="#">Vie communautaire</a>
<a href="#">Projets de programmation</a>
<a href="#">Questions et problèmes</a>
@ -12,7 +16,7 @@
<a href="#">Administration</a>
<a href="#">CreativeCalc</a>
<hr />
<hr>
<h3>Derniers commentaires</h3>
<ul>

View File

@ -0,0 +1,38 @@
{% extends "base/base.html" %}
{% block title %}
<h1>Forum de Planète Casio</h1>
{% endblock %}
{% block content %}
<section>
<p>Bienvenue sur le forum de Planète Casio&nbsp;! Vous pouvez créer des
nouveaux sujets ou poster des réponses avec un compte ou en postant en
tant qu'invité.</p>
{% if main_forum == None %}
<p>Il n'y a aucun forum.</p>
{% else %}
{% for l1 in main_forum.sub_forums %}
<table class=forumlist>
<tr><th>{{ l1.name }}</th><th>Nombre de sujets</th></tr>
{% if l1.sub_forums == [] %}
<tr><td><a href='/forum{{ l1.url }}'>{{ l1.name }}</td>
<td>{{ l1.topics | length }}</td></tr>
<tr><td>{{ l1.descr }}</td><td></td></tr>
{% endif %}
{% for l2 in l1.sub_forums %}
<tr><td><a href='/forum{{ l2.url }}'>{{ l2.name }}</td>
<td>{{ l2.topics | length }}</td></tr>
<tr><td>{{ l2.descr }}</td><td></td></tr>
{% endfor %}
</table>
{% endfor %}
{% endif %}
</section>
{% endblock %}

34
app/utils/converters.py Normal file
View File

@ -0,0 +1,34 @@
"""
utils.converter: Custom URL converters to match patterns in @app.route()
"""
from werkzeug.routing import BaseConverter
from app.models.forum import Forum
import re
class ForumConverter(BaseConverter):
def to_python(self, url):
f = Forum.query.filter_by(url=url).first()
if f is None:
raise Exception(f"ForumConverter: no forum with url {value}")
return f
def to_url(self, forum):
return forum.url
class TopicSlugConverter(BaseConverter):
def to_python(self, url):
"""Convert an URL pattern to a Python object, or raise an exception."""
m = re.fullmatch(r'(\d+)(?:-[\w-]*)?', url)
if m is None:
raise Exception(f"TopicSlugConverter: conversation failed")
return int(m[1], 10)
def to_url(self, topic_id):
return str(topic_id)
# Export only the converter classes
__all__ = "ForumConverter TopicSlugConverter".split()