1
0
Fork 0
mirror of https://gitlab.com/SIGBUS/nyaa.git synced 2024-12-23 00:39:59 +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,8 +571,11 @@ 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):
if flask.request.method == 'POST':
torrent = models.Torrent.by_id(torrent_id)
else:
torrent = models.Torrent.query \ torrent = models.Torrent.query \
.options(joinedload('filelist'), .options(joinedload('filelist'),
joinedload('comments')) \ joinedload('comments')) \
@ -585,36 +588,16 @@ 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)
# 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)
files = None
if torrent.filelist:
files = json.loads(torrent.filelist.filelist_blob.decode('utf-8'))
comment_form = None comment_form = None
if flask.g.user: if flask.g.user:
comment_form = forms.CommentForm() comment_form = forms.CommentForm()
return flask.render_template('view.html', torrent=torrent, if flask.request.method == 'POST':
files=files,
comment_form=comment_form,
comments=torrent.comments,
can_edit=can_edit)
@app.route('/view/<int:torrent_id>/comment', methods=['POST'])
def submit_comment(torrent_id):
if not flask.g.user: if not flask.g.user:
flask.abort(403) flask.abort(403)
torrent = models.Torrent.by_id(torrent_id) if comment_form.validate():
if not torrent: comment_text = (comment_form.comment.data or '').strip()
flask.abort(404)
form = forms.CommentForm(flask.request.form)
if form.validate():
comment_text = (form.comment.data or '').strip()
comment = models.Comment( comment = models.Comment(
torrent_id=torrent_id, torrent_id=torrent_id,
@ -624,8 +607,23 @@ def submit_comment(torrent_id):
db.session.add(comment) db.session.add(comment)
db.session.commit() db.session.commit()
flask.flash('Comment successfully posted.', 'success')
return flask.redirect(flask.url_for('view_torrent', torrent_id=torrent_id)) return flask.redirect(flask.url_for('view_torrent', torrent_id=torrent_id))
# 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)
files = None
if torrent.filelist:
files = json.loads(torrent.filelist.filelist_blob.decode('utf-8'))
return flask.render_template('view.html', torrent=torrent,
files=files,
comment_form=comment_form,
comments=torrent.comments,
can_edit=can_edit)
@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):

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">