From 9acdd14e81a643df36126d084a93262bff219b1a Mon Sep 17 00:00:00 2001 From: Kfir Hadas Date: Tue, 11 Jul 2017 01:00:03 +0300 Subject: [PATCH] Move /view/ route into 'torrents' blueprint and update templates. --- nyaa/api_handler.py | 4 +- nyaa/routes.py | 73 ++---------------------------- nyaa/templates/edit.html | 2 +- nyaa/templates/reports.html | 2 +- nyaa/templates/rss.xml | 6 +-- nyaa/templates/search_results.html | 6 +-- nyaa/views/__init__.py | 2 + nyaa/views/admin.py | 6 +-- nyaa/views/main.py | 2 +- nyaa/views/torrents.py | 70 ++++++++++++++++++++++++++++ 10 files changed, 91 insertions(+), 82 deletions(-) create mode 100644 nyaa/views/torrents.py diff --git a/nyaa/api_handler.py b/nyaa/api_handler.py index 7ec1fc6..1bde812 100644 --- a/nyaa/api_handler.py +++ b/nyaa/api_handler.py @@ -109,7 +109,7 @@ def v2_api_upload(): # Create a response dict with relevant data 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, 'name': torrent.display_name, 'hash': torrent.info_hash.hex(), @@ -306,7 +306,7 @@ def v2_api_info(torrent_id_or_hash): # Create a response dict with relevant data torrent_metadata = { '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, 'name': torrent.display_name, diff --git a/nyaa/routes.py b/nyaa/routes.py index 83cbb63..48e1c72 100644 --- a/nyaa/routes.py +++ b/nyaa/routes.py @@ -1,12 +1,9 @@ -import json import os.path from urllib.parse import quote import flask 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.utils import cached_function @@ -58,74 +55,13 @@ def upload(): if flask.request.method == 'POST' and upload_form.validate(): 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: # 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 return flask.render_template('upload.html', upload_form=upload_form), status_code -@app.route('/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('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//comment//delete', methods=['POST']) def delete_comment(torrent_id, comment_id): if not flask.g.user: @@ -145,7 +81,7 @@ def delete_comment(torrent_id, comment_id): db.session.flush() 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: log = "Comment deleted on torrent [#{}]({})".format(torrent.id, url) adminlog = models.AdminLog(log=log, admin_id=flask.g.user.id) @@ -196,7 +132,7 @@ def edit_torrent(torrent_id): if editor.is_moderator: 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: log = "Torrent [#{0}]({1}) marked as {2}".format( torrent.id, url, "deleted" if torrent.deleted else "undeleted") @@ -279,7 +215,7 @@ def submit_report(torrent_id): db.session.commit() 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): @@ -307,6 +243,7 @@ def register_blueprints(flask_app): flask_app.register_blueprint(views.admin_bp) flask_app.register_blueprint(views.main_bp) flask_app.register_blueprint(views.site_bp) + flask_app.register_blueprint(views.torrents_bp) flask_app.register_blueprint(views.users_bp) diff --git a/nyaa/templates/edit.html b/nyaa/templates/edit.html index a7ebf68..adce139 100644 --- a/nyaa/templates/edit.html +++ b/nyaa/templates/edit.html @@ -4,7 +4,7 @@ {% from "_formhelpers.html" import render_field %} {% 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) %}

Edit Torrent #{{torrent.id}} {% if (torrent.user != None) and (torrent.user != g.user) %} diff --git a/nyaa/templates/reports.html b/nyaa/templates/reports.html index 968aab2..fd013ed 100644 --- a/nyaa/templates/reports.html +++ b/nyaa/templates/reports.html @@ -25,7 +25,7 @@ {% endif %} - {{ report.torrent.display_name }} + {{ report.torrent.display_name }} by {{ report.torrent.user.username }} {% if g.user.is_superadmin and report.torrent.uploader_ip %} diff --git a/nyaa/templates/rss.xml b/nyaa/templates/rss.xml index 35aa8a8..9201c25 100644 --- a/nyaa/templates/rss.xml +++ b/nyaa/templates/rss.xml @@ -14,7 +14,7 @@ {% else %} {{ create_magnet_from_es_info(torrent.display_name, torrent.info_hash) }} {% endif %} - {{ url_for('view_torrent', torrent_id=torrent.meta.id, _external=True) }} + {{ url_for('torrents.view', torrent_id=torrent.meta.id, _external=True) }} {{ torrent.created_time|rfc822_es }} {{- torrent.seed_count }} @@ -28,7 +28,7 @@ {% else %} {{ torrent.magnet_uri }} {% endif %} - {{ url_for('view_torrent', torrent_id=torrent.id, _external=True) }} + {{ url_for('torrents.view', torrent_id=torrent.id, _external=True) }} {{ torrent.created_time|rfc822 }} {{- torrent.stats.seed_count }} @@ -41,7 +41,7 @@ {{- category_name(cat_id) }} {{- torrent.filesize | filesizeformat(True) }} {% set torrent_id = use_elastic and torrent.meta.id or torrent.id %} - #{{ torrent_id }} | {{ torrent.display_name }} | {{ torrent.filesize | filesizeformat(True) }} | {{ category_name(cat_id) }} | {{ use_elastic and torrent.info_hash or torrent.info_hash_as_hex | upper }}]]> + #{{ torrent_id }} | {{ torrent.display_name }} | {{ torrent.filesize | filesizeformat(True) }} | {{ category_name(cat_id) }} | {{ use_elastic and torrent.info_hash or torrent.info_hash_as_hex | upper }}]]> {% endfor %} diff --git a/nyaa/templates/search_results.html b/nyaa/templates/search_results.html index f83aab1..095d09f 100644 --- a/nyaa/templates/search_results.html +++ b/nyaa/templates/search_results.html @@ -73,14 +73,14 @@ {% set torrent_id = torrent.meta.id if use_elastic else torrent.id %} {% set com_count = torrent.comment_count %} {% if com_count %} - + {{ com_count -}} {% endif %} {% if use_elastic %} - {%if "highlight" in torrent.meta %}{{ torrent.meta.highlight.display_name[0] | safe }}{% else %}{{torrent.display_name}}{%endif%} + {%if "highlight" in torrent.meta %}{{ torrent.meta.highlight.display_name[0] | safe }}{% else %}{{torrent.display_name}}{%endif%} {% else %} - {{ torrent.display_name | escape }} + {{ torrent.display_name | escape }} {% endif %} diff --git a/nyaa/views/__init__.py b/nyaa/views/__init__.py index c6e7d4f..d2b940c 100644 --- a/nyaa/views/__init__.py +++ b/nyaa/views/__init__.py @@ -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 diff --git a/nyaa/views/admin.py b/nyaa/views/admin.py index b48b1d3..8766da5 100644 --- a/nyaa/views/admin.py +++ b/nyaa/views/admin.py @@ -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 diff --git a/nyaa/views/main.py b/nyaa/views/main.py index a361826..f687ef5 100644 --- a/nyaa/views/main.py +++ b/nyaa/views/main.py @@ -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') diff --git a/nyaa/views/torrents.py b/nyaa/views/torrents.py new file mode 100644 index 0000000..c4cad33 --- /dev/null +++ b/nyaa/views/torrents.py @@ -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/', 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)