diff --git a/app/forms/forum.py b/app/forms/forum.py index fbcf5c4..fddab16 100644 --- a/app/forms/forum.py +++ b/app/forms/forum.py @@ -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') diff --git a/app/routes/posts/edit.py b/app/routes/posts/edit.py index ab6269b..cb73ada 100644 --- a/app/routes/posts/edit.py +++ b/app/routes/posts/edit.py @@ -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/', methods=['GET','POST']) @login_required @check_csrf diff --git a/app/templates/forum/edit_comment.html b/app/templates/forum/edit_comment.html index d832cae..3e0a45c 100644 --- a/app/templates/forum/edit_comment.html +++ b/app/templates/forum/edit_comment.html @@ -24,20 +24,24 @@ {{ form.hidden_tag() }} {% if form.pseudo %} - {{ form.pseudo.label }} - {{ form.pseudo }} - {% for error in form.pseudo.errors %} - {{ error }} - {% endfor %} +
+ {{ form.pseudo.label }} + {{ form.pseudo }} + {% for error in form.pseudo.errors %} + {{ error }} + {% endfor %} +
{% endif %} {{ widget_editor.text_editor(form.message, label=False, autofocus=True) }} -
Supprimer des pièces jointes
- {% for id, a in form.attachment_list.items() %} - {{ form[id]() }} {{ a.name }} ({{ a.size }} octets)
- {% endfor %} -
+ {% if form.attachment_list %} +
Supprimer des pièces jointes
+ {% for id, a in form.attachment_list.items() %} + {{ form[id]() }} {{ a.name }} ({{ a.size }} octets)
+ {% endfor %} +
+ {% endif %}
{{ form.attachments.label }} diff --git a/app/templates/forum/edit_topic.html b/app/templates/forum/edit_topic.html index 91701cd..f825960 100644 --- a/app/templates/forum/edit_topic.html +++ b/app/templates/forum/edit_topic.html @@ -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 %} -Forum de Planète Casio » {{ t.forum.name }} »

{{ t.title }}

+Forum de Planète Casio » {{ t.forum.name }} »

Édition de sujet

{% endblock %} {% block content %}
-

Édition du topic {{ t.title }}

+

Édition du sujet: {{ t.title }}

-
-

Commenter le sujet

+

Sujet actuel

+ +

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.

+ + {% call widget_thread.thread_leader(t.thread.top_comment) %} +
+
Posté le {{ t.date_created | dyndate }}
+ {{ widget_thread.post_actions(t) }} +
+ {{ t.thread.top_comment.text | md }} + {{ widget_attachments.attachments(t.thread.top_comment) }} + {% endcall %} + +
+

Nouveau sujet

- Un formulaire + {{ form.hidden_tag() }} + +
+ {{ form.title.label }} + {{ form.title }} + {% for error in form.title.errors %} + {{ error }} + {% endfor %} +
+ + {% if form.pseudo %} +
+ {{ form.pseudo.label }} + {{ form.pseudo }} + {% for error in form.pseudo.errors %} + {{ error }} + {% endfor %} +
+ {% endif %} + + {{ widget_editor.text_editor(form.message, label=False, autofocus=True) }} + + {% if form.attachment_list %} +
Supprimer des pièces jointes
+ {% for id, a in form.attachment_list.items() %} + {{ form[id]() }} {{ a.name }} ({{ a.size }} octets)
+ {% endfor %} +
+ {% endif %} + +
+ {{ form.attachments.label }} +
+ {{ form.attachments }} + {% for error in form.attachments.errors %} + {{ error }} + {% endfor %} +
+
+ +
{{ form.submit(class_='bg-ok') }}
diff --git a/app/templates/forum/topic.html b/app/templates/forum/topic.html index aca7f5a..f4bcbd7 100644 --- a/app/templates/forum/topic.html +++ b/app/templates/forum/topic.html @@ -13,14 +13,16 @@

{{ t.title }}

- {% call widget_thread.thread_leader(t.thread.top_comment) %} -
-
Posté le {{ t.date_created | dyndate }}
- {{ widget_thread.post_actions(t) }} -
- {{ 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) %} +
+
Posté le {{ t.date_created | dyndate }}
+ {{ widget_thread.post_actions(t) }} +
+ {{ 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}) }} diff --git a/app/templates/widgets/thread.html b/app/templates/widgets/thread.html index bcb4534..f95fdc8 100644 --- a/app/templates/widgets/thread.html +++ b/app/templates/widgets/thread.html @@ -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 %}