diff --git a/app/forms/forum.py b/app/forms/forum.py index a6aac90..30bcd90 100644 --- a/app/forms/forum.py +++ b/app/forms/forum.py @@ -17,6 +17,10 @@ class AnonymousCommentForm(CommentForm): class CommentEditForm(CommentForm): + # Boolean fields to remove files are added dynamically + attachments = MultipleFileField('Ajouter des pièces jointes', + validators=[vd.file.optional, vd.file.count, vd.file.extension, + vd.file.size, vd.file.namelength]) submit = SubmitField('Modifier') diff --git a/app/models/comment.py b/app/models/comment.py index ceb81ee..5b29629 100644 --- a/app/models/comment.py +++ b/app/models/comment.py @@ -20,6 +20,8 @@ class Comment(Post): backref=backref('comments', lazy='dynamic'), foreign_keys=thread_id) + # attachments (relation from Attachment) + def __init__(self, author, text, thread): """ diff --git a/app/routes/posts/edit.py b/app/routes/posts/edit.py index aac8f6e..3ee2f2b 100644 --- a/app/routes/posts/edit.py +++ b/app/routes/posts/edit.py @@ -1,8 +1,10 @@ from app import app, db from app.models.post import Post +from app.models.attachment import Attachment from app.utils.render import render from app.utils.check_csrf import check_csrf from app.forms.forum import CommentEditForm, AnonymousCommentEditForm +from wtforms import BooleanField from urllib.parse import urlparse from flask import redirect, url_for, abort, request from flask_login import login_required, current_user @@ -21,16 +23,37 @@ def edit_post(postid): abort(403) if p.type == "comment": - form = CommentEditForm() + 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 - if form.submit.data: - db.session.add(p) - db.session.commit() + # Remove attachments + for id, a in form.attachment_list.items(): + if form[id].data: + a.delete() - return redirect(referrer) + # 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) diff --git a/app/templates/forum/edit_comment.html b/app/templates/forum/edit_comment.html index 46780b4..d832cae 100644 --- a/app/templates/forum/edit_comment.html +++ b/app/templates/forum/edit_comment.html @@ -11,7 +11,7 @@

Édition de commentaire

Commentaire actuel

- +
@@ -33,6 +33,22 @@ {{ 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 %} +
+ +
+ {{ form.attachments.label }} +
+ {{ form.attachments }} + {% for error in form.attachments.errors %} + {{ error }} + {% endfor %} +
+
+
{{ form.submit(class_='bg-ok') }}
diff --git a/app/templates/forum/forum.html b/app/templates/forum/forum.html index dbd5783..8a33226 100644 --- a/app/templates/forum/forum.html +++ b/app/templates/forum/forum.html @@ -74,10 +74,15 @@ {{ widget_editor.text_editor(form.message) }} - {{ form.attachments }} - {% for error in form.attachments.errors %} - {{ error }} - {% endfor %} +
+ {{ 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 9d5de05..5853b28 100644 --- a/app/templates/forum/topic.html +++ b/app/templates/forum/topic.html @@ -42,10 +42,15 @@ {{ widget_editor.text_editor(form.message, label=False) }} - {{ form.attachments }} - {% for error in form.attachments.errors %} - {{ error }} - {% endfor %} +
+ {{ form.attachments.label }} +
+ {{ form.attachments }} + {% for error in form.attachments.errors %} + {{ error }} + {% endfor %} +
+
{{ form.submit(class_='bg-ok') }}
{{ widget_user.profile(comment.author) }}
{{ comment.text | md }}