From 3913d8cea2bad0917eedc650e8ade4378c45a97f Mon Sep 17 00:00:00 2001
From: Kfir Hadas <sharkykh@gmail.com>
Date: Sat, 8 Jul 2017 00:48:28 +0300
Subject: [PATCH] Move admin routes into blueprint

and update templates

Routes:
* /reports
* /adminlog
---
 nyaa/routes.py             | 70 +-----------------------------------
 nyaa/templates/layout.html |  4 +--
 nyaa/views/__init__.py     |  2 ++
 nyaa/views/admin.py        | 74 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 79 insertions(+), 71 deletions(-)
 create mode 100644 nyaa/views/admin.py

diff --git a/nyaa/routes.py b/nyaa/routes.py
index 31c49d5..41699e7 100644
--- a/nyaa/routes.py
+++ b/nyaa/routes.py
@@ -707,75 +707,6 @@ def submit_report(torrent_id):
     return flask.redirect(flask.url_for('view_torrent', torrent_id=torrent_id))
 
 
-@app.route('/adminlog', methods=['GET'])
-def view_adminlog():
-    if not flask.g.user or not flask.g.user.is_moderator:
-        flask.abort(403)
-
-    page = flask.request.args.get('p', flask.request.args.get('offset', 1, int), int)
-    logs = models.AdminLog.all_logs() \
-        .order_by(models.AdminLog.created_time.desc()) \
-        .paginate(page=page, per_page=20)
-
-    return flask.render_template('adminlog.html',
-                                 adminlog=logs)
-
-
-@app.route('/reports', methods=['GET', 'POST'])
-def view_reports():
-    if not flask.g.user or not flask.g.user.is_moderator:
-        flask.abort(403)
-
-    page = flask.request.args.get('p', flask.request.args.get('offset', 1, int), int)
-    reports = models.Report.not_reviewed(page)
-    report_action = forms.ReportActionForm(flask.request.form)
-
-    if flask.request.method == 'POST' and report_action.validate():
-        action = report_action.action.data
-        torrent_id = report_action.torrent.data
-        report_id = report_action.report.data
-        torrent = models.Torrent.by_id(torrent_id)
-        report = models.Report.by_id(report_id)
-        report_user = models.User.by_id(report.user_id)
-
-        if not torrent or not report or report.status != 0:
-            flask.abort(404)
-        else:
-            log = "Report #{}: {} [#{}]({}), reported by [{}]({})"
-            if action == 'delete':
-                torrent.deleted = True
-                report.status = 1
-                log = log.format(report_id, 'Deleted', torrent_id,
-                                 flask.url_for('view_torrent', torrent_id=torrent_id),
-                                 report_user.username,
-                                 flask.url_for('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),
-                                 report_user.username,
-                                 flask.url_for('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),
-                                 report_user.username,
-                                 flask.url_for('view_user', user_name=report_user.username))
-                report.status = 2
-
-            adminlog = models.AdminLog(log=log, admin_id=flask.g.user.id)
-            db.session.add(adminlog)
-
-            models.Report.remove_reviewed(torrent_id)
-            db.session.commit()
-            flask.flash('Closed report #{}'.format(report.id), 'success')
-            return flask.redirect(flask.url_for('view_reports'))
-
-    return flask.render_template('reports.html',
-                                 reports=reports,
-                                 report_action=report_action)
-
-
 def _get_cached_torrent_file(torrent):
     # Note: obviously temporary
     cached_torrent = os.path.join(app.config['BASE_DIR'],
@@ -858,6 +789,7 @@ def register_blueprints(flask_app):
     flask_app.register_blueprint(api_handler.api_blueprint, url_prefix='/api')
     # Site routes
     flask_app.register_blueprint(views.account_bp)
+    flask_app.register_blueprint(views.admin_bp)
     flask_app.register_blueprint(views.site_bp)
 
 
diff --git a/nyaa/templates/layout.html b/nyaa/templates/layout.html
index 85742c2..76ae459 100644
--- a/nyaa/templates/layout.html
+++ b/nyaa/templates/layout.html
@@ -95,8 +95,8 @@
 								<span class="caret"></span>
 							</a>
 							<ul class="dropdown-menu">
-								<li {% if request.path == url_for('view_reports') %}class="active"{% endif %}><a href="{{ url_for('view_reports') }}">Reports</a></li>
-								<li {% if request.path == url_for('view_adminlog') %}class="active"{% endif %}><a href="{{ url_for('view_adminlog') }}">Log</a></li>
+								<li {% if request.path == url_for('admin.reports') %}class="active"{% endif %}><a href="{{ url_for('admin.reports') }}">Reports</a></li>
+								<li {% if request.path == url_for('admin.log') %}class="active"{% endif %}><a href="{{ url_for('admin.log') }}">Log</a></li>
 							</ul>
 						</li>
 						{% endif %}
diff --git a/nyaa/views/__init__.py b/nyaa/views/__init__.py
index 9e96c5c..d7922cc 100644
--- a/nyaa/views/__init__.py
+++ b/nyaa/views/__init__.py
@@ -1,7 +1,9 @@
 from nyaa.views import (
     account,
+    admin,
     site,
 )
 
 account_bp = account.bp
+admin_bp = admin.bp
 site_bp = site.bp
diff --git a/nyaa/views/admin.py b/nyaa/views/admin.py
new file mode 100644
index 0000000..2c3416a
--- /dev/null
+++ b/nyaa/views/admin.py
@@ -0,0 +1,74 @@
+import flask
+
+from nyaa import db, forms, models
+
+bp = flask.Blueprint('admin', __name__)
+
+
+@bp.route('/adminlog', endpoint='log', methods=['GET'])
+def view_adminlog():
+    if not flask.g.user or not flask.g.user.is_moderator:
+        flask.abort(403)
+
+    page = flask.request.args.get('p', flask.request.args.get('offset', 1, int), int)
+    logs = models.AdminLog.all_logs() \
+        .order_by(models.AdminLog.created_time.desc()) \
+        .paginate(page=page, per_page=20)
+
+    return flask.render_template('adminlog.html',
+                                 adminlog=logs)
+
+
+@bp.route('/reports', endpoint='reports', methods=['GET', 'POST'])
+def view_reports():
+    if not flask.g.user or not flask.g.user.is_moderator:
+        flask.abort(403)
+
+    page = flask.request.args.get('p', flask.request.args.get('offset', 1, int), int)
+    reports = models.Report.not_reviewed(page)
+    report_action = forms.ReportActionForm(flask.request.form)
+
+    if flask.request.method == 'POST' and report_action.validate():
+        action = report_action.action.data
+        torrent_id = report_action.torrent.data
+        report_id = report_action.report.data
+        torrent = models.Torrent.by_id(torrent_id)
+        report = models.Report.by_id(report_id)
+        report_user = models.User.by_id(report.user_id)
+
+        if not torrent or not report or report.status != 0:
+            flask.abort(404)
+        else:
+            log = "Report #{}: {} [#{}]({}), reported by [{}]({})"
+            if action == 'delete':
+                torrent.deleted = True
+                report.status = 1
+                log = log.format(report_id, 'Deleted', torrent_id,
+                                 flask.url_for('view_torrent', torrent_id=torrent_id),
+                                 report_user.username,
+                                 flask.url_for('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),
+                                 report_user.username,
+                                 flask.url_for('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),
+                                 report_user.username,
+                                 flask.url_for('view_user', user_name=report_user.username))
+                report.status = 2
+
+            adminlog = models.AdminLog(log=log, admin_id=flask.g.user.id)
+            db.session.add(adminlog)
+
+            models.Report.remove_reviewed(torrent_id)
+            db.session.commit()
+            flask.flash('Closed report #{}'.format(report.id), 'success')
+            return flask.redirect(flask.url_for('admin.reports'))
+
+    return flask.render_template('reports.html',
+                                 reports=reports,
+                                 report_action=report_action)