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

Move delete_comment and submit_report into blueprint

This commit is contained in:
Kfir Hadas 2017-07-24 23:15:45 +03:00
parent ef56e54521
commit 93f94023d1
3 changed files with 56 additions and 60 deletions

View file

@ -1,6 +1,4 @@
import flask from nyaa import api_handler, app, template_utils, views
from nyaa import api_handler, app, db, forms, models, template_utils, views
from nyaa.backend import get_category_id_map from nyaa.backend import get_category_id_map
DEBUG_API = False DEBUG_API = False
@ -12,61 +10,6 @@ def category_name(cat_id):
return ' - '.join(get_category_id_map().get(cat_id, ['???'])) return ' - '.join(get_category_id_map().get(cat_id, ['???']))
# Routes start here #
@app.route('/view/<int:torrent_id>/comment/<int:comment_id>/delete', methods=['POST'])
def delete_comment(torrent_id, comment_id):
if not flask.g.user:
flask.abort(403)
torrent = models.Torrent.by_id(torrent_id)
if not torrent:
flask.abort(404)
comment = models.Comment.query.filter_by(id=comment_id).first()
if not comment:
flask.abort(404)
if not (comment.user.id == flask.g.user.id or flask.g.user.is_moderator):
flask.abort(403)
db.session.delete(comment)
db.session.flush()
torrent.update_comment_count()
url = flask.url_for('torrents.view', torrent_id=torrent.id)
if flask.g.user.is_moderator:
log = "Comment deleted on torrent [#{}]({})".format(torrent.id, url)
adminlog = models.AdminLog(log=log, admin_id=flask.g.user.id)
db.session.add(adminlog)
db.session.commit()
flask.flash('Comment successfully deleted.', 'success')
return flask.redirect(url)
@app.route('/view/<int:torrent_id>/submit_report', methods=['POST'])
def submit_report(torrent_id):
if not flask.g.user:
flask.abort(403)
form = forms.ReportForm(flask.request.form)
if flask.request.method == 'POST' and form.validate():
report_reason = form.reason.data
current_user_id = flask.g.user.id
report = models.Report(
torrent_id=torrent_id,
user_id=current_user_id,
reason=report_reason)
db.session.add(report)
db.session.commit()
flask.flash('Successfully reported torrent!', 'success')
return flask.redirect(flask.url_for('torrents.view', torrent_id=torrent_id))
# #################################### BLUEPRINTS #################################### # #################################### BLUEPRINTS ####################################
def register_blueprints(flask_app): def register_blueprints(flask_app):

View file

@ -156,7 +156,7 @@
<div class="row"> <div class="row">
<a href="#com-{{ loop.index }}"><small data-timestamp-swap data-timestamp="{{ comment.created_utc_timestamp|int }}">{{ comment.created_time.strftime('%Y-%m-%d %H:%M UTC') }}</small></a> <a href="#com-{{ loop.index }}"><small data-timestamp-swap data-timestamp="{{ comment.created_utc_timestamp|int }}">{{ comment.created_time.strftime('%Y-%m-%d %H:%M UTC') }}</small></a>
{% if g.user.is_moderator or g.user.id == comment.user_id %} {% if g.user.is_moderator or g.user.id == comment.user_id %}
<form class="delete-comment-form" action="{{ url_for('delete_comment', torrent_id=torrent.id, comment_id=comment.id) }}" method="POST"> <form class="delete-comment-form" action="{{ url_for('torrents.delete_comment', torrent_id=torrent.id, comment_id=comment.id) }}" method="POST">
<button name="submit" type="submit" class="btn btn-danger btn-xs" title="Delete">Delete</button> <button name="submit" type="submit" class="btn btn-danger btn-xs" title="Delete">Delete</button>
</form> </form>
{% endif %} {% endif %}
@ -195,7 +195,7 @@
rules</a>. Useless reports like "download is slow" or rules</a>. Useless reports like "download is slow" or
"thanks" can get you banned from the site. "thanks" can get you banned from the site.
</div> </div>
<form method="POST" action="{{ request.url }}/submit_report"> <form method="POST" action="{{ url_for('torrents.report', torrent_id=torrent.id) }}">
{{ report_form.csrf_token }} {{ report_form.csrf_token }}
{{ render_field(report_form.reason, class_='form-control', maxlength=255) }} {{ render_field(report_form.reason, class_='form-control', maxlength=255) }}
<div style="float: right;"> <div style="float: right;">

View file

@ -177,6 +177,59 @@ def download_torrent(torrent_id):
return resp return resp
@bp.route('/view/<int:torrent_id>/comment/<int:comment_id>/delete', methods=['POST'])
def delete_comment(torrent_id, comment_id):
if not flask.g.user:
flask.abort(403)
torrent = models.Torrent.by_id(torrent_id)
if not torrent:
flask.abort(404)
comment = models.Comment.query.filter_by(id=comment_id).first()
if not comment:
flask.abort(404)
if not (comment.user.id == flask.g.user.id or flask.g.user.is_moderator):
flask.abort(403)
db.session.delete(comment)
db.session.flush()
torrent.update_comment_count()
url = flask.url_for('torrents.view', torrent_id=torrent.id)
if flask.g.user.is_moderator:
log = "Comment deleted on torrent [#{}]({})".format(torrent.id, url)
adminlog = models.AdminLog(log=log, admin_id=flask.g.user.id)
db.session.add(adminlog)
db.session.commit()
flask.flash('Comment successfully deleted.', 'success')
return flask.redirect(url)
@bp.route('/view/<int:torrent_id>/submit_report', endpoint='report', methods=['POST'])
def submit_report(torrent_id):
if not flask.g.user:
flask.abort(403)
form = forms.ReportForm(flask.request.form)
if flask.request.method == 'POST' and form.validate():
report_reason = form.reason.data
current_user_id = flask.g.user.id
report = models.Report(
torrent_id=torrent_id,
user_id=current_user_id,
reason=report_reason)
db.session.add(report)
db.session.commit()
flask.flash('Successfully reported torrent!', 'success')
return flask.redirect(flask.url_for('torrents.view', torrent_id=torrent_id))
@bp.route('/upload', methods=['GET', 'POST']) @bp.route('/upload', methods=['GET', 'POST'])
def upload(): def upload():
upload_form = forms.UploadForm(CombinedMultiDict((flask.request.files, flask.request.form))) upload_form = forms.UploadForm(CombinedMultiDict((flask.request.files, flask.request.form)))