Move /view/<int:torrent_id> route into 'torrents' blueprint

and update templates.
This commit is contained in:
Kfir Hadas
2017-07-11 01:00:03 +03:00
parent 42535bbdab
commit 9acdd14e81
10 changed files with 91 additions and 82 deletions

View File

@@ -3,6 +3,7 @@ from nyaa.views import (
admin,
main,
site,
torrents,
users,
)
@@ -10,4 +11,5 @@ account_bp = account.bp
admin_bp = admin.bp
main_bp = main.bp
site_bp = site.bp
torrents_bp = torrents.bp
users_bp = users.bp

View File

@@ -44,19 +44,19 @@ def view_reports():
torrent.deleted = True
report.status = 1
log = log.format(report_id, 'Deleted', torrent_id,
flask.url_for('view_torrent', torrent_id=torrent_id),
flask.url_for('torrents.view', torrent_id=torrent_id),
report_user.username,
flask.url_for('users.view_user', user_name=report_user.username))
elif action == 'hide':
log = log.format(report_id, 'Hid', torrent_id,
flask.url_for('view_torrent', torrent_id=torrent_id),
flask.url_for('torrents.view', torrent_id=torrent_id),
report_user.username,
flask.url_for('users.view_user', user_name=report_user.username))
torrent.hidden = True
report.status = 1
else:
log = log.format(report_id, 'Closed', torrent_id,
flask.url_for('view_torrent', torrent_id=torrent_id),
flask.url_for('torrents.view', torrent_id=torrent_id),
report_user.username,
flask.url_for('users.view_user', user_name=report_user.username))
report.status = 2

View File

@@ -115,7 +115,7 @@ def home(rss):
flask.flash(flask.Markup('You were redirected here because '
'the given hash matched this torrent.'), 'info')
# Redirect user from search to the torrent if we found one with the specific info_hash
return flask.redirect(flask.url_for('view_torrent', torrent_id=infohash_torrent.id))
return flask.redirect(flask.url_for('torrents.view', torrent_id=infohash_torrent.id))
# If searching, we get results from elastic search
use_elastic = app.config.get('USE_ELASTIC_SEARCH')

70
nyaa/views/torrents.py Normal file
View File

@@ -0,0 +1,70 @@
import json
import flask
from sqlalchemy.orm import joinedload
from nyaa import db, forms, models
bp = flask.Blueprint('torrents', __name__)
@bp.route('/view/<int:torrent_id>', endpoint='view', methods=['GET', 'POST'])
def view_torrent(torrent_id):
if flask.request.method == 'POST':
torrent = models.Torrent.by_id(torrent_id)
else:
torrent = models.Torrent.query \
.options(joinedload('filelist'),
joinedload('comments')) \
.filter_by(id=torrent_id) \
.first()
if not torrent:
flask.abort(404)
# Only allow admins see deleted torrents
if torrent.deleted and not (flask.g.user and flask.g.user.is_moderator):
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.flush()
torrent_count = torrent.update_comment_count()
db.session.commit()
flask.flash('Comment successfully posted.', 'success')
return flask.redirect(flask.url_for('torrents.view',
torrent_id=torrent_id,
_anchor='com-' + str(torrent_count)))
# 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'))
report_form = forms.ReportForm()
return flask.render_template('view.html', torrent=torrent,
files=files,
comment_form=comment_form,
comments=torrent.comments,
can_edit=can_edit,
report_form=report_form)