From 9fef343c1bec31449441116ebac95c37b91f6795 Mon Sep 17 00:00:00 2001 From: Kfir Hadas Date: Mon, 24 Jul 2017 23:10:36 +0300 Subject: [PATCH] Move torrent edit and upload into 'torrents' blueprint Move supporting functions and variables into other files * nyaa.views.torrents: - _create_upload_category_choices * nyaa.backend: - get_category_id_map --- nyaa/api_handler.py | 5 +- nyaa/backend.py | 13 ++++ nyaa/routes.py | 123 +------------------------------------ nyaa/templates/layout.html | 2 +- nyaa/templates/view.html | 2 +- nyaa/views/torrents.py | 109 +++++++++++++++++++++++++++++++- 6 files changed, 127 insertions(+), 127 deletions(-) diff --git a/nyaa/api_handler.py b/nyaa/api_handler.py index 1bde812..66e4a90 100644 --- a/nyaa/api_handler.py +++ b/nyaa/api_handler.py @@ -6,8 +6,7 @@ from nyaa import models, forms from nyaa import bencode, backend, utils from nyaa import torrents -# For _create_upload_category_choices -from nyaa import routes +from nyaa.views.torrents import _create_upload_category_choices import functools import json @@ -102,7 +101,7 @@ def v2_api_upload(): # Flask-WTF (very helpfully!!) automatically grabs the request form, so force a None formdata upload_form = forms.UploadForm(None, data=mapped_dict, meta={'csrf': False}) - upload_form.category.choices = routes._create_upload_category_choices() + upload_form.category.choices = _create_upload_category_choices() if upload_form.validate(): torrent = backend.handle_torrent_upload(upload_form, flask.g.user) diff --git a/nyaa/backend.py b/nyaa/backend.py index c2b1fe1..93d43f8 100644 --- a/nyaa/backend.py +++ b/nyaa/backend.py @@ -12,6 +12,19 @@ from orderedset import OrderedSet from ipaddress import ip_address +@utils.cached_function +def get_category_id_map(): + ''' Reads database for categories and turns them into a dict with + ids as keys and name list as the value, ala + {'1_0': ['Anime'], '1_2': ['Anime', 'English-translated'], ...} ''' + cat_id_map = {} + for main_cat in models.MainCategory.query: + cat_id_map[main_cat.id_as_string] = [main_cat.name] + for sub_cat in main_cat.sub_categories: + cat_id_map[sub_cat.id_as_string] = [main_cat.name, sub_cat.name] + return cat_id_map + + def _replace_utf8_values(dict_or_list): ''' Will replace 'property' with 'property.utf-8' and remove latter if it exists. Thanks, bitcomet! :/ ''' diff --git a/nyaa/routes.py b/nyaa/routes.py index 48e1c72..1227d7b 100644 --- a/nyaa/routes.py +++ b/nyaa/routes.py @@ -2,10 +2,9 @@ import os.path from urllib.parse import quote import flask -from werkzeug.datastructures import CombinedMultiDict -from nyaa import api_handler, app, backend, db, forms, models, template_utils, torrents, views -from nyaa.utils import cached_function +from nyaa import api_handler, app, db, forms, models, template_utils, torrents, views +from nyaa.backend import get_category_id_map DEBUG_API = False @@ -16,52 +15,8 @@ def category_name(cat_id): return ' - '.join(get_category_id_map().get(cat_id, ['???'])) -@cached_function -def get_category_id_map(): - ''' Reads database for categories and turns them into a dict with - ids as keys and name list as the value, ala - {'1_0': ['Anime'], '1_2': ['Anime', 'English-translated'], ...} ''' - cat_id_map = {} - for main_cat in models.MainCategory.query: - cat_id_map[main_cat.id_as_string] = [main_cat.name] - for sub_cat in main_cat.sub_categories: - cat_id_map[sub_cat.id_as_string] = [main_cat.name, sub_cat.name] - return cat_id_map - - # Routes start here # -@cached_function -def _create_upload_category_choices(): - ''' Turns categories in the database into a list of (id, name)s ''' - choices = [('', '[Select a category]')] - id_map = get_category_id_map() - - for key in sorted(id_map.keys()): - cat_names = id_map[key] - is_main_cat = key.endswith('_0') - - # cat_name = is_main_cat and cat_names[0] or (' - ' + cat_names[1]) - cat_name = ' - '.join(cat_names) - choices.append((key, cat_name, is_main_cat)) - return choices - - -@app.route('/upload', methods=['GET', 'POST']) -def upload(): - upload_form = forms.UploadForm(CombinedMultiDict((flask.request.files, flask.request.form))) - upload_form.category.choices = _create_upload_category_choices() - - if flask.request.method == 'POST' and upload_form.validate(): - torrent = backend.handle_torrent_upload(upload_form, flask.g.user) - - 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//comment//delete', methods=['POST']) def delete_comment(torrent_id, comment_id): if not flask.g.user: @@ -93,80 +48,6 @@ def delete_comment(torrent_id, comment_id): return flask.redirect(url) -@app.route('/view//edit', methods=['GET', 'POST']) -def edit_torrent(torrent_id): - torrent = models.Torrent.by_id(torrent_id) - form = forms.EditForm(flask.request.form) - form.category.choices = _create_upload_category_choices() - - editor = flask.g.user - - if not torrent: - flask.abort(404) - - # Only allow admins edit deleted torrents - if torrent.deleted and not (editor and editor.is_moderator): - flask.abort(404) - - # Only allow torrent owners or admins edit torrents - if not editor or not (editor is torrent.user or editor.is_moderator): - flask.abort(403) - - if flask.request.method == 'POST' and form.validate(): - # Form has been sent, edit torrent with data. - torrent.main_category_id, torrent.sub_category_id = \ - form.category.parsed_data.get_category_ids() - torrent.display_name = (form.display_name.data or '').strip() - torrent.information = (form.information.data or '').strip() - torrent.description = (form.description.data or '').strip() - - torrent.hidden = form.is_hidden.data - torrent.remake = form.is_remake.data - torrent.complete = form.is_complete.data - torrent.anonymous = form.is_anonymous.data - - if editor.is_trusted: - torrent.trusted = form.is_trusted.data - - deleted_changed = torrent.deleted != form.is_deleted.data - if editor.is_moderator: - torrent.deleted = form.is_deleted.data - - 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") - adminlog = models.AdminLog(log=log, admin_id=editor.id) - db.session.add(adminlog) - - db.session.commit() - - flask.flash(flask.Markup( - 'Torrent has been successfully edited! Changes might take a few minutes to show up.'), - 'info') - - return flask.redirect(url) - else: - if flask.request.method != 'POST': - # Fill form data only if the POST didn't fail - form.category.data = torrent.sub_category.id_as_string - form.display_name.data = torrent.display_name - form.information.data = torrent.information - form.description.data = torrent.description - - form.is_hidden.data = torrent.hidden - form.is_remake.data = torrent.remake - form.is_complete.data = torrent.complete - form.is_anonymous.data = torrent.anonymous - - form.is_trusted.data = torrent.trusted - form.is_deleted.data = torrent.deleted - - return flask.render_template('edit.html', - form=form, - torrent=torrent) - - @app.route('/view//magnet') def redirect_magnet(torrent_id): torrent = models.Torrent.by_id(torrent_id) diff --git a/nyaa/templates/layout.html b/nyaa/templates/layout.html index 76ccbda..43da9cb 100644 --- a/nyaa/templates/layout.html +++ b/nyaa/templates/layout.html @@ -71,7 +71,7 @@ {% set search_placeholder = 'Search {} torrents...'.format(search_username) if user_page else 'Search...' %}