forum: add basic comment edition with preview

Limitations:
* Works only for registered users
* Does not redirect to the proper MainPost page
* Does not check permissions
This commit is contained in:
Lephe 2020-08-01 17:25:08 +02:00
parent 79600e8598
commit 7e11469183
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
6 changed files with 87 additions and 2 deletions

View File

@ -21,3 +21,12 @@ class CommentForm(FlaskForm):
class AnonymousCommentForm(CommentForm):
pseudo = StringField('Pseudo',
validators=[DataRequired(), vd.name_valid, vd.name_available])
class CommentEditForm(CommentForm):
submit = SubmitField('Valider les modifications')
preview = SubmitField('Prévisualiser')
class AnonymousCommentEditForm(CommentEditForm):
pseudo = StringField('Pseudo',
validators=[DataRequired(), vd.name_valid, vd.name_available])

View File

@ -5,3 +5,4 @@ from app.routes.account import login, account, notification
from app.routes.admin import index, groups, account, trophies, forums
from app.routes.forum import index, topic
from app.routes.programs import index
from app.routes.posts import edit

View File

@ -130,7 +130,7 @@ def validation():
try:
mail = request.args['email']
except Exception as e:
print("Error: {e}")
print(f"Error: {e}")
abort(404)
if current_user.is_authenticated:

34
app/routes/posts/edit.py Normal file
View File

@ -0,0 +1,34 @@
from app import app, db
from app.models.post import Post
from app.utils.render import render
from app.forms.forum import CommentEditForm, AnonymousCommentEditForm
from flask import redirect, url_for, abort
from flask_login import login_required, current_user
@app.route('/post/<int:postid>', methods=['GET','POST'])
# TODO: Allow guest edit of posts
@login_required
def edit_post(postid):
p = Post.query.filter_by(id=postid).first_or_404()
# TODO: Check whether privileged user has access to board
if p.author != current_user and not current_user.priv("edit-posts"):
abort(403)
if p.type == "comment":
form = CommentEditForm()
if form.validate_on_submit():
p.text = form.message.data
if form.submit.data:
db.session.add(p)
db.session.commit()
# TODO: Proper redirection
return redirect(url_for('index'))
form.message.data = p.text
return render('forum/edit_comment.html', comment=p, form=form)
else:
abort(404)

View File

@ -0,0 +1,41 @@
{% extends "base/base.html" %}
{% import "widgets/editor.html" as widget_editor %}
{% import "widgets/user.html" as widget_user %}
{% block title %}
<a href='/forum'>Forum de Planète Casio</a> » Édition de commentaire</h1>
{% endblock %}
{% block content %}
<section>
<h1>Édition de commentaire</h1>
<h3>Commentaire actuel</h3>
<table class="thread">
<tr>
<td class="author">{{ widget_user.profile(comment.author) }}</td>
<td><div>{{ comment.text }}</div></td>
</tr>
</table>
<div class="form">
<h3>Nouveau commentaire</h3>
<form action="" method="post" enctype="multipart/form-data">
{{ form.hidden_tag() }}
{% if form.pseudo %}
{{ form.pseudo.label }}
{{ form.pseudo }}
{% for error in form.pseudo.errors %}
<span class="msgerror">{{ error }}</span>
{% endfor %}
{% endif %}
{{ widget_editor.text_editor(form.message, label=False) }}
<div>{{ form.preview(class_='bg-ok') }}</div>
<div>{{ form.submit(class_='bg-ok') }}</div>
</form>
</div>
</section>
{% endblock %}

View File

@ -29,7 +29,7 @@
Posté le {{ c.date_created|dyndate }}
{% endif %}
| <a href="{{ url_for('forum_topic', f=t.forum, page=(t,comments.page), _anchor=c.id) }}">#</a>
| <a href="#">Modifier</a>
| <a href="{{ url_for('edit_post', postid=c.id) }}">Modifier</a>
| <a href="#">Supprimer</a>
</div>
<!--<hr>-->