1
0
Fork 0
mirror of https://gitlab.com/SIGBUS/nyaa.git synced 2024-12-22 19:30:00 +00:00

Move posting comments to view_torrent to fix displaying form errors.

This commit is contained in:
nyaadev 2017-05-23 00:04:27 +02:00
parent b7144f80f9
commit 3fc347d049
2 changed files with 33 additions and 35 deletions

View file

@ -571,13 +571,16 @@ def upload():
return flask.render_template('upload.html', upload_form=upload_form), status_code return flask.render_template('upload.html', upload_form=upload_form), status_code
@app.route('/view/<int:torrent_id>') @app.route('/view/<int:torrent_id>', methods=['GET', 'POST'])
def view_torrent(torrent_id): def view_torrent(torrent_id):
torrent = models.Torrent.query \ if flask.request.method == 'POST':
.options(joinedload('filelist'), torrent = models.Torrent.by_id(torrent_id)
joinedload('comments')) \ else:
.filter_by(id=torrent_id) \ torrent = models.Torrent.query \
.first() .options(joinedload('filelist'),
joinedload('comments')) \
.filter_by(id=torrent_id) \
.first()
if not torrent: if not torrent:
flask.abort(404) flask.abort(404)
@ -585,6 +588,29 @@ def view_torrent(torrent_id):
if torrent.deleted and not (flask.g.user and flask.g.user.is_moderator): if torrent.deleted and not (flask.g.user and flask.g.user.is_moderator):
flask.abort(404) flask.abort(404)
comment_form = None
if flask.g.user:
comment_form = forms.CommentForm()
if flask.request.method == 'POST':
if not flask.g.user:
flask.abort(403)
if comment_form.validate():
comment_text = (comment_form.comment.data or '').strip()
comment = models.Comment(
torrent_id=torrent_id,
user_id=flask.g.user.id,
text=comment_text)
db.session.add(comment)
db.session.commit()
flask.flash('Comment successfully posted.', 'success')
return flask.redirect(flask.url_for('view_torrent', torrent_id=torrent_id))
# Only allow owners and admins to edit torrents # Only allow owners and admins to edit torrents
can_edit = flask.g.user and (flask.g.user is torrent.user or flask.g.user.is_moderator) can_edit = flask.g.user and (flask.g.user is torrent.user or flask.g.user.is_moderator)
@ -592,10 +618,6 @@ def view_torrent(torrent_id):
if torrent.filelist: if torrent.filelist:
files = json.loads(torrent.filelist.filelist_blob.decode('utf-8')) files = json.loads(torrent.filelist.filelist_blob.decode('utf-8'))
comment_form = None
if flask.g.user:
comment_form = forms.CommentForm()
return flask.render_template('view.html', torrent=torrent, return flask.render_template('view.html', torrent=torrent,
files=files, files=files,
comment_form=comment_form, comment_form=comment_form,
@ -603,30 +625,6 @@ def view_torrent(torrent_id):
can_edit=can_edit) can_edit=can_edit)
@app.route('/view/<int:torrent_id>/comment', methods=['POST'])
def submit_comment(torrent_id):
if not flask.g.user:
flask.abort(403)
torrent = models.Torrent.by_id(torrent_id)
if not torrent:
flask.abort(404)
form = forms.CommentForm(flask.request.form)
if form.validate():
comment_text = (form.comment.data or '').strip()
comment = models.Comment(
torrent_id=torrent_id,
user_id=flask.g.user.id,
text=comment_text)
db.session.add(comment)
db.session.commit()
return flask.redirect(flask.url_for('view_torrent', torrent_id=torrent_id))
@app.route('/view/<int:torrent_id>/comment/<int:comment_id>/delete', methods=['POST']) @app.route('/view/<int:torrent_id>/comment/<int:comment_id>/delete', methods=['POST'])
def delete_comment(torrent_id, comment_id): def delete_comment(torrent_id, comment_id):
if not flask.g.user: if not flask.g.user:

View file

@ -176,7 +176,7 @@
</script> </script>
{% endfor %} {% endfor %}
{% if comment_form %} {% if comment_form %}
<form action="{{ url_for('submit_comment', torrent_id=torrent.id) }}" method="POST" class="comment-box"> <form class="comment-box" method="POST">
{{ comment_form.csrf_token }} {{ comment_form.csrf_token }}
{{ render_field(comment_form.comment, class_='form-control') }} {{ render_field(comment_form.comment, class_='form-control') }}
<input type="submit" value="Submit" class="btn btn-success btn-sm"> <input type="submit" value="Submit" class="btn btn-success btn-sm">