forum: improve attachment inputs and allow edition (#49)
This commit is contained in:
parent
619ea85eeb
commit
05f56a1cf4
|
@ -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')
|
||||
|
||||
|
||||
|
|
|
@ -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):
|
||||
"""
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
<h1>Édition de commentaire</h1>
|
||||
|
||||
<h3>Commentaire actuel</h3>
|
||||
<table class="thread">
|
||||
<table class="thread topcomment">
|
||||
<tr>
|
||||
<td class="author">{{ widget_user.profile(comment.author) }}</td>
|
||||
<td><div>{{ comment.text | md }}</div></td>
|
||||
|
@ -33,6 +33,22 @@
|
|||
|
||||
{{ 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>
|
||||
|
||||
<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>
|
||||
|
|
|
@ -74,10 +74,15 @@
|
|||
|
||||
{{ widget_editor.text_editor(form.message) }}
|
||||
|
||||
{{ form.attachments }}
|
||||
{% for error in form.attachments.errors %}
|
||||
<span class="msgerror">{{ error }}</span>
|
||||
{% endfor %}
|
||||
<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>
|
||||
|
|
|
@ -42,10 +42,15 @@
|
|||
|
||||
{{ widget_editor.text_editor(form.message, label=False) }}
|
||||
|
||||
{{ form.attachments }}
|
||||
{% for error in form.attachments.errors %}
|
||||
<span class="msgerror">{{ error }}</span>
|
||||
{% endfor %}
|
||||
<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>
|
||||
|
|
Loading…
Reference in New Issue