mirror of
https://gitlab.com/SIGBUS/nyaa.git
synced 2024-10-31 23:35:55 +00:00
Move delete_comment and submit_report into blueprint
This commit is contained in:
parent
ef56e54521
commit
93f94023d1
|
@ -1,6 +1,4 @@
|
|||
import flask
|
||||
|
||||
from nyaa import api_handler, app, db, forms, models, template_utils, views
|
||||
from nyaa import api_handler, app, template_utils, views
|
||||
from nyaa.backend import get_category_id_map
|
||||
|
||||
DEBUG_API = False
|
||||
|
@ -12,61 +10,6 @@ def category_name(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 ####################################
|
||||
|
||||
def register_blueprints(flask_app):
|
||||
|
|
|
@ -156,7 +156,7 @@
|
|||
<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>
|
||||
{% 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>
|
||||
</form>
|
||||
{% endif %}
|
||||
|
@ -195,7 +195,7 @@
|
|||
rules</a>. Useless reports like "download is slow" or
|
||||
"thanks" can get you banned from the site.
|
||||
</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 }}
|
||||
{{ render_field(report_form.reason, class_='form-control', maxlength=255) }}
|
||||
<div style="float: right;">
|
||||
|
|
|
@ -177,6 +177,59 @@ def download_torrent(torrent_id):
|
|||
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'])
|
||||
def upload():
|
||||
upload_form = forms.UploadForm(CombinedMultiDict((flask.request.files, flask.request.form)))
|
||||
|
|
Loading…
Reference in a new issue