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

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

@ -109,7 +109,7 @@ def v2_api_upload():
# Create a response dict with relevant data # Create a response dict with relevant data
torrent_metadata = { torrent_metadata = {
'url': flask.url_for('view_torrent', torrent_id=torrent.id, _external=True), 'url': flask.url_for('torrents.view', torrent_id=torrent.id, _external=True),
'id': torrent.id, 'id': torrent.id,
'name': torrent.display_name, 'name': torrent.display_name,
'hash': torrent.info_hash.hex(), 'hash': torrent.info_hash.hex(),
@ -306,7 +306,7 @@ def v2_api_info(torrent_id_or_hash):
# Create a response dict with relevant data # Create a response dict with relevant data
torrent_metadata = { torrent_metadata = {
'submitter': submitter, 'submitter': submitter,
'url': flask.url_for('view_torrent', torrent_id=torrent.id, _external=True), 'url': flask.url_for('torrents.view', torrent_id=torrent.id, _external=True),
'id': torrent.id, 'id': torrent.id,
'name': torrent.display_name, 'name': torrent.display_name,

View file

@ -1,12 +1,9 @@
import json
import os.path import os.path
from urllib.parse import quote from urllib.parse import quote
import flask import flask
from werkzeug.datastructures import CombinedMultiDict from werkzeug.datastructures import CombinedMultiDict
from sqlalchemy.orm import joinedload
from nyaa import api_handler, app, backend, db, forms, models, template_utils, torrents, views from nyaa import api_handler, app, backend, db, forms, models, template_utils, torrents, views
from nyaa.utils import cached_function from nyaa.utils import cached_function
@ -58,74 +55,13 @@ def upload():
if flask.request.method == 'POST' and upload_form.validate(): if flask.request.method == 'POST' and upload_form.validate():
torrent = backend.handle_torrent_upload(upload_form, flask.g.user) torrent = backend.handle_torrent_upload(upload_form, flask.g.user)
return flask.redirect('/view/' + str(torrent.id)) return flask.redirect(flask.url_for('torrents.view', torrent_id=torrent.id))
else: else:
# If we get here with a POST, it means the form data was invalid: return a non-okay status # If we get here with a POST, it means the form data was invalid: return a non-okay status
status_code = 400 if flask.request.method == 'POST' else 200 status_code = 400 if flask.request.method == 'POST' else 200
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>', 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('view_torrent',
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)
@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:
@ -145,7 +81,7 @@ def delete_comment(torrent_id, comment_id):
db.session.flush() db.session.flush()
torrent.update_comment_count() torrent.update_comment_count()
url = flask.url_for('view_torrent', torrent_id=torrent.id) url = flask.url_for('torrents.view', torrent_id=torrent.id)
if flask.g.user.is_moderator: if flask.g.user.is_moderator:
log = "Comment deleted on torrent [#{}]({})".format(torrent.id, url) log = "Comment deleted on torrent [#{}]({})".format(torrent.id, url)
adminlog = models.AdminLog(log=log, admin_id=flask.g.user.id) adminlog = models.AdminLog(log=log, admin_id=flask.g.user.id)
@ -196,7 +132,7 @@ def edit_torrent(torrent_id):
if editor.is_moderator: if editor.is_moderator:
torrent.deleted = form.is_deleted.data torrent.deleted = form.is_deleted.data
url = flask.url_for('view_torrent', torrent_id=torrent.id) url = flask.url_for('torrents.view', torrent_id=torrent.id)
if deleted_changed and editor.is_moderator: if deleted_changed and editor.is_moderator:
log = "Torrent [#{0}]({1}) marked as {2}".format( log = "Torrent [#{0}]({1}) marked as {2}".format(
torrent.id, url, "deleted" if torrent.deleted else "undeleted") torrent.id, url, "deleted" if torrent.deleted else "undeleted")
@ -279,7 +215,7 @@ def submit_report(torrent_id):
db.session.commit() db.session.commit()
flask.flash('Successfully reported torrent!', 'success') flask.flash('Successfully reported torrent!', 'success')
return flask.redirect(flask.url_for('view_torrent', torrent_id=torrent_id)) return flask.redirect(flask.url_for('torrents.view', torrent_id=torrent_id))
def _get_cached_torrent_file(torrent): def _get_cached_torrent_file(torrent):
@ -307,6 +243,7 @@ def register_blueprints(flask_app):
flask_app.register_blueprint(views.admin_bp) flask_app.register_blueprint(views.admin_bp)
flask_app.register_blueprint(views.main_bp) flask_app.register_blueprint(views.main_bp)
flask_app.register_blueprint(views.site_bp) flask_app.register_blueprint(views.site_bp)
flask_app.register_blueprint(views.torrents_bp)
flask_app.register_blueprint(views.users_bp) flask_app.register_blueprint(views.users_bp)

View file

@ -4,7 +4,7 @@
{% from "_formhelpers.html" import render_field %} {% from "_formhelpers.html" import render_field %}
{% from "_formhelpers.html" import render_markdown_editor %} {% from "_formhelpers.html" import render_markdown_editor %}
{% set torrent_url = url_for('view_torrent', torrent_id=torrent.id) %} {% set torrent_url = url_for('torrents.view', torrent_id=torrent.id) %}
<h1> <h1>
Edit Torrent <a href="{{ torrent_url }}">#{{torrent.id}}</a> Edit Torrent <a href="{{ torrent_url }}">#{{torrent.id}}</a>
{% if (torrent.user != None) and (torrent.user != g.user) %} {% if (torrent.user != None) and (torrent.user != g.user) %}

View file

@ -25,7 +25,7 @@
{% endif %} {% endif %}
</td> </td>
<td> <td>
<a href="{{ url_for('view_torrent', torrent_id=report.torrent.id) }}">{{ report.torrent.display_name }}</a> <a href="{{ url_for('torrents.view', torrent_id=report.torrent.id) }}">{{ report.torrent.display_name }}</a>
by <a href="{{ url_for('users.view_user', user_name=report.torrent.user.username) }}"> by <a href="{{ url_for('users.view_user', user_name=report.torrent.user.username) }}">
{{ report.torrent.user.username }}</a> {{ report.torrent.user.username }}</a>
{% if g.user.is_superadmin and report.torrent.uploader_ip %} {% if g.user.is_superadmin and report.torrent.uploader_ip %}

View file

@ -14,7 +14,7 @@
{% else %} {% else %}
<link>{{ create_magnet_from_es_info(torrent.display_name, torrent.info_hash) }}</link> <link>{{ create_magnet_from_es_info(torrent.display_name, torrent.info_hash) }}</link>
{% endif %} {% endif %}
<guid isPermaLink="true">{{ url_for('view_torrent', torrent_id=torrent.meta.id, _external=True) }}</guid> <guid isPermaLink="true">{{ url_for('torrents.view', torrent_id=torrent.meta.id, _external=True) }}</guid>
<pubDate>{{ torrent.created_time|rfc822_es }}</pubDate> <pubDate>{{ torrent.created_time|rfc822_es }}</pubDate>
<nyaa:seeders> {{- torrent.seed_count }}</nyaa:seeders> <nyaa:seeders> {{- torrent.seed_count }}</nyaa:seeders>
@ -28,7 +28,7 @@
{% else %} {% else %}
<link>{{ torrent.magnet_uri }}</link> <link>{{ torrent.magnet_uri }}</link>
{% endif %} {% endif %}
<guid isPermaLink="true">{{ url_for('view_torrent', torrent_id=torrent.id, _external=True) }}</guid> <guid isPermaLink="true">{{ url_for('torrents.view', torrent_id=torrent.id, _external=True) }}</guid>
<pubDate>{{ torrent.created_time|rfc822 }}</pubDate> <pubDate>{{ torrent.created_time|rfc822 }}</pubDate>
<nyaa:seeders> {{- torrent.stats.seed_count }}</nyaa:seeders> <nyaa:seeders> {{- torrent.stats.seed_count }}</nyaa:seeders>
@ -41,7 +41,7 @@
<nyaa:category> {{- category_name(cat_id) }}</nyaa:category> <nyaa:category> {{- category_name(cat_id) }}</nyaa:category>
<nyaa:size> {{- torrent.filesize | filesizeformat(True) }}</nyaa:size> <nyaa:size> {{- torrent.filesize | filesizeformat(True) }}</nyaa:size>
{% set torrent_id = use_elastic and torrent.meta.id or torrent.id %} {% set torrent_id = use_elastic and torrent.meta.id or torrent.id %}
<description><![CDATA[<a href="{{ url_for('view_torrent', torrent_id=torrent_id, _external=True) }}">#{{ torrent_id }} | {{ torrent.display_name }}</a> | {{ torrent.filesize | filesizeformat(True) }} | {{ category_name(cat_id) }} | {{ use_elastic and torrent.info_hash or torrent.info_hash_as_hex | upper }}]]></description> <description><![CDATA[<a href="{{ url_for('torrents.view', torrent_id=torrent_id, _external=True) }}">#{{ torrent_id }} | {{ torrent.display_name }}</a> | {{ torrent.filesize | filesizeformat(True) }} | {{ category_name(cat_id) }} | {{ use_elastic and torrent.info_hash or torrent.info_hash_as_hex | upper }}]]></description>
</item> </item>
{% endfor %} {% endfor %}
</channel> </channel>

View file

@ -73,14 +73,14 @@
{% set torrent_id = torrent.meta.id if use_elastic else torrent.id %} {% set torrent_id = torrent.meta.id if use_elastic else torrent.id %}
{% set com_count = torrent.comment_count %} {% set com_count = torrent.comment_count %}
{% if com_count %} {% if com_count %}
<a href="{{ url_for('view_torrent', torrent_id=torrent_id, _anchor='comments') }}" class="comments" title="{{ '{c} comment{s}'.format(c=com_count, s='s' if com_count > 1 else '') }}"> <a href="{{ url_for('torrents.view', torrent_id=torrent_id, _anchor='comments') }}" class="comments" title="{{ '{c} comment{s}'.format(c=com_count, s='s' if com_count > 1 else '') }}">
<i class="fa fa-comments-o"></i>{{ com_count -}} <i class="fa fa-comments-o"></i>{{ com_count -}}
</a> </a>
{% endif %} {% endif %}
{% if use_elastic %} {% if use_elastic %}
<a href="{{ url_for('view_torrent', torrent_id=torrent_id) }}" title="{{ torrent.display_name | escape }}">{%if "highlight" in torrent.meta %}{{ torrent.meta.highlight.display_name[0] | safe }}{% else %}{{torrent.display_name}}{%endif%}</a> <a href="{{ url_for('torrents.view', torrent_id=torrent_id) }}" title="{{ torrent.display_name | escape }}">{%if "highlight" in torrent.meta %}{{ torrent.meta.highlight.display_name[0] | safe }}{% else %}{{torrent.display_name}}{%endif%}</a>
{% else %} {% else %}
<a href="{{ url_for('view_torrent', torrent_id=torrent_id) }}" title="{{ torrent.display_name | escape }}">{{ torrent.display_name | escape }}</a> <a href="{{ url_for('torrents.view', torrent_id=torrent_id) }}" title="{{ torrent.display_name | escape }}">{{ torrent.display_name | escape }}</a>
{% endif %} {% endif %}
</td> </td>
<td class="text-center" style="white-space: nowrap;"> <td class="text-center" style="white-space: nowrap;">

View file

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

View file

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

View file

@ -115,7 +115,7 @@ def home(rss):
flask.flash(flask.Markup('You were redirected here because ' flask.flash(flask.Markup('You were redirected here because '
'the given hash matched this torrent.'), 'info') 'the given hash matched this torrent.'), 'info')
# Redirect user from search to the torrent if we found one with the specific info_hash # 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 # If searching, we get results from elastic search
use_elastic = app.config.get('USE_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)