forum: enable topic modification

This works only after a top comment has been selected (but topics
without top comments are an anomaly of databases initialized before it
was added, so we don't care).
This commit is contained in:
Lephe 2021-07-12 19:06:23 +02:00
parent b8ed0bba99
commit 9f0cbc810b
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
6 changed files with 149 additions and 61 deletions

View File

@ -29,6 +29,7 @@ class AnonymousCommentForm(CommentForm):
ab = AntibotField()
class CommentEditForm(CommentForm):
# Boolean fields to remove files are added dynamically
attachments = MultipleFileField(
@ -58,3 +59,11 @@ class TopicCreationForm(CommentForm):
class AnonymousTopicCreationForm(TopicCreationForm, AnonymousCommentForm):
ab = AntibotField()
class TopicEditForm(CommentEditForm):
title = StringField(
'Nom du sujet',
validators=[InputRequired(), Length(min=3, max=128)])
submit = SubmitField('Modifier le sujet')

View File

@ -1,12 +1,13 @@
from app import app, db
from app.models.user import Member
from app.models.post import Post
from app.models.comment import Comment
from app.models.attachment import Attachment
from app.models.topic import Topic
from app.models.program import Program
from app.utils.render import render
from app.utils.check_csrf import check_csrf
from app.forms.forum import CommentEditForm, AnonymousCommentEditForm
from app.forms.forum import CommentEditForm, AnonymousCommentEditForm, TopicEditForm
from wtforms import BooleanField
from urllib.parse import urlparse
from flask import redirect, url_for, abort, request
@ -25,44 +26,61 @@ def edit_post(postid):
if current_user.is_anonymous or not current_user.can_edit_post(p):
abort(403)
if p.type == "comment":
class CommentForm(CommentEditForm):
pass
for a in p.attachments:
setattr(CommentForm, f'a{a.id}', BooleanField(f'a{a.id}'))
setattr(CommentForm, 'attachment_list',
{ f'a{a.id}': a for a in p.attachments })
form = CommentForm()
if form.validate_on_submit():
p.text = form.message.data
# Remove attachments
for id, a in form.attachment_list.items():
if form[id].data:
a.delete()
# Add new attachments
attachments = []
for file in form.attachments.data:
if file.filename != "":
a = Attachment(file, p)
attachments.append((a, file))
db.session.add(a)
db.session.add(p)
db.session.commit()
for a, file in attachments:
a.set_file(file)
return redirect(referrer)
form.message.data = p.text
return render('forum/edit_comment.html', comment=p, form=form)
if isinstance(p, Comment):
base = CommentEditForm
comment = p
elif isinstance(p, Topic):
base = TopicEditForm
comment = p.thread.top_comment
else:
abort(404)
class TheForm(base):
pass
for a in comment.attachments:
setattr(TheForm, f'a{a.id}', BooleanField(f'a{a.id}'))
setattr(TheForm, 'attachment_list',
{ f'a{a.id}': a for a in comment.attachments })
form = TheForm()
if form.validate_on_submit():
comment.text = form.message.data
# Remove attachments
for id, a in form.attachment_list.items():
if form[id].data:
a.delete()
# Add new attachments
attachments = []
for file in form.attachments.data:
if file.filename != "":
a = Attachment(file, comment)
attachments.append((a, file))
db.session.add(a)
db.session.add(comment)
if isinstance(p, Topic):
p.title = form.title.data
db.session.add(p)
db.session.commit()
for a, file in attachments:
a.set_file(file)
return redirect(referrer)
# Non-submitted form
if isinstance(p, Comment):
form.message.data = p.text
return render('forum/edit_comment.html', comment=p, form=form)
elif isinstance(p, Topic):
form.message.data = p.thread.top_comment.text
form.title.data = p.title
return render('forum/edit_topic.html', t=p, form=form)
@app.route('/post/supprimer/<int:postid>', methods=['GET','POST'])
@login_required
@check_csrf

View File

@ -24,20 +24,24 @@
{{ form.hidden_tag() }}
{% if form.pseudo %}
{{ form.pseudo.label }}
{{ form.pseudo }}
{% for error in form.pseudo.errors %}
<span class="msgerror">{{ error }}</span>
{% endfor %}
<div>
{{ form.pseudo.label }}
{{ form.pseudo }}
{% for error in form.pseudo.errors %}
<span class="msgerror">{{ error }}</span>
{% endfor %}
</div>
{% endif %}
{{ widget_editor.text_editor(form.message, label=False, autofocus=True) }}
<div>Supprimer des pièces jointes<br>
{% for id, a in form.attachment_list.items() %}
{{ form[id]() }} <code>{{ a.name }}</code> ({{ a.size }} octets)<br>
{% endfor %}
</div>
{% if form.attachment_list %}
<div>Supprimer des pièces jointes<br>
{% for id, a in form.attachment_list.items() %}
{{ form[id]() }} <code>{{ a.name }}</code> ({{ a.size }} octets)<br>
{% endfor %}
</div>
{% endif %}
<div>
{{ form.attachments.label }}

View File

@ -1,19 +1,74 @@
{% extends "base/base.html" %}
{% import "widgets/attachments.html" as widget_attachments %}
{% import "widgets/thread.html" as widget_thread with context %}
{% import "widgets/editor.html" as widget_editor %}
{% import "widgets/member.html" as widget_member %}
{% import "widgets/user.html" as widget_user %}
{% block title %}
<a href='/forum'>Forum de Planète Casio</a> » <a href="{{ url_for('forum_page', f=t.forum) }}">{{ t.forum.name }}</a> » <h1>{{ t.title }}</h1>
<a href='/forum'>Forum de Planète Casio</a> » <a href="{{ url_for('forum_page', f=t.forum) }}">{{ t.forum.name }}</a> » <h1>Édition de sujet</h1>
{% endblock %}
{% block content %}
<section>
<h1>Édition du topic {{ t.title }}</h1>
<h1>Édition du sujet: {{ t.title }}</h1>
<div class=form>
<h3>Commenter le sujet</h3>
<h3>Sujet actuel</h3>
<p><i>Pour modifier substantiellement ou réécrire le commentaire d'en-tête, il vaut mieux poster un nouveau commentaire et le désigner comme en-tête ; ça permet à la conversation de rester dans son contexte.</i></p>
{% call widget_thread.thread_leader(t.thread.top_comment) %}
<div class="info">
<div>Posté le {{ t.date_created | dyndate }}</div>
{{ widget_thread.post_actions(t) }}
</div>
{{ t.thread.top_comment.text | md }}
{{ widget_attachments.attachments(t.thread.top_comment) }}
{% endcall %}
<div class="form">
<h3>Nouveau sujet</h3>
<form action="" method="post" enctype="multipart/form-data">
Un formulaire
{{ form.hidden_tag() }}
<div>
{{ form.title.label }}
{{ form.title }}
{% for error in form.title.errors %}
<span class="msgerror">{{ error }}</span>
{% endfor %}
</div>
{% if form.pseudo %}
<div>
{{ form.pseudo.label }}
{{ form.pseudo }}
{% for error in form.pseudo.errors %}
<span class="msgerror">{{ error }}</span>
{% endfor %}
</div>
{% endif %}
{{ widget_editor.text_editor(form.message, label=False, autofocus=True) }}
{% if form.attachment_list %}
<div>Supprimer des pièces jointes<br>
{% for id, a in form.attachment_list.items() %}
{{ form[id]() }} <code>{{ a.name }}</code> ({{ a.size }} octets)<br>
{% endfor %}
</div>
{% endif %}
<div>
{{ form.attachments.label }}
<div>
{{ form.attachments }}
{% for error in form.attachments.errors %}
<span class="msgerror">{{ error }}</span>
{% endfor %}
</div>
</div>
<div>{{ form.submit(class_='bg-ok') }}</div>
</form>
</div>
</section>

View File

@ -13,14 +13,16 @@
<section>
<h1>{{ t.title }}</h1>
{% call widget_thread.thread_leader(t.thread.top_comment) %}
<div class="info">
<div>Posté le {{ t.date_created | dyndate }}</div>
{{ widget_thread.post_actions(t) }}
</div>
{{ t.thread.top_comment.text | md }}
{{ widget_attachments.attachments(t.thread.top_comment) }}
{% endcall %}
{% if t.thread.top_comment %}
{% call widget_thread.thread_leader(t.thread.top_comment) %}
<div class="info">
<div>Posté le {{ t.date_created | dyndate }}</div>
{{ widget_thread.post_actions(t) }}
</div>
{{ t.thread.top_comment.text | md }}
{{ widget_attachments.attachments(t.thread.top_comment) }}
{% endcall %}
{% endif %}
{{ widget_pagination.paginate(comments, 'forum_topic', t, {'f': t.forum}) }}

View File

@ -13,7 +13,7 @@
{% set can_topcomm = auth and current_user.can_set_topcomment(post) %}
{% if post.type == "topic" %}
{% set suffix = " le topic" %}
{% set suffix = " le sujet" %}
{% elif post.type == "program" %}
{% set suffix = " le programme" %}
{% endif %}