Merge branch 'dev' into preprod

This commit is contained in:
Darks 2020-08-02 17:31:16 +02:00
commit ad9a2e447b
Signed by: Darks
GPG Key ID: F61F10FA138E797C
9 changed files with 69 additions and 14 deletions

View File

@ -8,7 +8,7 @@ class CommentForm(FlaskForm):
message = TextAreaField('Message', validators=[DataRequired()])
attachments = MultipleFileField('Pièces-jointes',
validators=[vd.file.optional, vd.file.count, vd.file.extension,
vd.file.size])
vd.file.size, vd.file.namelength])
submit = SubmitField('Commenter')
preview = SubmitField('Prévisualiser')

View File

@ -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

View File

@ -16,7 +16,8 @@ class Program(Post):
# TODO: Compatible calculator models
thread_id = db.Column(db.Integer,db.ForeignKey('thread.id'),nullable=False)
thread = db.relationship('Thread', foreign_keys=thread_id)
thread = db.relationship('Thread', foreign_keys=thread_id,
back_populates='owner_program')
# TODO: Number of views, statistics, attached files, etc

View File

@ -12,6 +12,11 @@ class Thread(db.Model):
top_comment_id = db.Column(db.Integer, db.ForeignKey('comment.id'))
top_comment = db.relationship('Comment', foreign_keys=top_comment_id)
# Post owning the thread, set only by Topic, Program, etc. In general, you
# should use [owner_post] which groups them together.
owner_topic = db.relationship('Topic')
owner_program = db.relationship('Program')
# Other fields populated automatically through relations:
# <comments> The list of comments (of type Comment)
@ -39,5 +44,13 @@ class Thread(db.Model):
self.top_comment = top_comment
@property
def owner_post(self):
if self.owner_topic != []:
return self.owner_topic[0]
if self.owner_program != []:
return self.owner_program[0]
return None
def __repr__(self):
return f'<Thread: #{self.id}>'

View File

@ -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,11 +23,13 @@ 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)
thread = db.relationship('Thread', foreign_keys=thread_id)
thread = db.relationship('Thread', foreign_keys=thread_id,
back_populates='owner_topic')
# Number of views in the forum
views = db.Column(db.Integer)

View File

@ -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)

View File

@ -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 %}

View File

@ -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 %}

View File

@ -49,3 +49,13 @@ def size(form, files):
else:
if size > 500e3: # 500 ko per comment for a guest
raise ValidationError("Fichiers trop lourds (max 500ko)")
def namelength(form, files):
errors = []
for f in files.data:
name = secure_filename(f.filename)
if len(name) > 64:
errors.append(f.filename)
if len(errors) > 0:
raise ValidationError(f"Noms trop longs, 64 caractères max " \
f"({', '.join(errors)})")