From 374e24efc0c625e90df27c4703f3b657d24e9169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?X=C3=A9fir=20Destiny?= Date: Sat, 7 Dec 2019 10:17:29 +0100 Subject: [PATCH] Page list finished --- app.py | 35 +++++++++++++++++++++-------------- config.py | 9 ++++++++- templates/list.html | 12 ++++++------ 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/app.py b/app.py index c83530f..74cc1a0 100644 --- a/app.py +++ b/app.py @@ -1,18 +1,15 @@ from operator import itemgetter -from os import environ from flask import redirect, render_template, url_for, request -from config import app, auth +from config import app, auth, ADMIN_USERNAME, ADMIN_PASSWORD, IS_DEBUG, APP_PORT, db from connectors import * from models import SearchForm, AnimeTitle @auth.verify_password def verify_password(username, password): - admin_username = environ.get('ADMIN_USERNAME', 'admin') - admin_password = environ.get('ADMIN_USERNAME', 'secret') - return username is admin_username and password is admin_password + return username is ADMIN_USERNAME and password is ADMIN_PASSWORD @app.template_filter('boldify') @@ -26,8 +23,8 @@ def boldify(name): @app.template_filter('shorten') -def shorten(str): - return str[:30] + '...' if len(str) > 30 else str +def shorten(str_to_replace): + return str_to_replace[:30] + '...' if len(str_to_replace) > 30 else str_to_replace @app.route('/') @@ -76,11 +73,23 @@ def latest(): @app.route('/list') def list_animes(): - filters = request.args.get('s', 'nyaa,yggtorrent').split(',') - titles = AnimeTitle.query.order_by(AnimeTitle.name).all() + filters = None + for i, to_filter in enumerate(request.args.get('s', 'nyaa,yggtorrent').split(',')): + if not i: + filters = AnimeLink.link.contains(to_filter) + else: + filters = filters | AnimeLink.link.contains(to_filter) - return render_template('list.html', form=SearchForm(), titles=titles, filters=filters, connector=Connector, - flags=ConnectorLang) + titles = db.session.query(AnimeTitle, AnimeLink).join(AnimeLink).filter(filters).order_by(AnimeTitle.name).all() + + results = {} + for title, link in titles: + if title.id not in results: + results[title.id] = [link] + else: + results[title.id].append(link) + + return render_template('list.html', form=SearchForm(), titles=results, connector=Connector, flags=ConnectorLang) @app.route('/admin') @@ -90,6 +99,4 @@ def admin(): if __name__ == '__main__': - app_debug = environ.get('FLASK_ENV', 'production') == 'development' - app_port = environ.get('FLASK_PORT', 5000) - app.run('0.0.0.0', app_port, app_debug) + app.run('0.0.0.0', APP_PORT, IS_DEBUG) diff --git a/config.py b/config.py index d04f32d..dae7d20 100644 --- a/config.py +++ b/config.py @@ -17,9 +17,16 @@ if not db_host or not db_user or not db_password or not db_name: print('Missing connection environment variables') exit() +# load app constants +IS_DEBUG = environ.get('FLASK_ENV', 'production') == 'development' +ADMIN_USERNAME = environ.get('ADMIN_USERNAME', 'admin') +ADMIN_PASSWORD = environ.get('ADMIN_PASSWORD', 'secret') +APP_PORT = environ.get('FLASK_PORT', 5000) + app = Flask(__name__) +app.secret_key = urandom(24).hex() app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://%s:%s@%s/%s' % (db_user, db_password, db_host, db_name) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True -app.secret_key = urandom(24).hex() +app.config['SQLALCHEMY_ECHO'] = IS_DEBUG auth = HTTPBasicAuth() db = SQLAlchemy(app) diff --git a/templates/list.html b/templates/list.html index 76978c5..fc4087d 100644 --- a/templates/list.html +++ b/templates/list.html @@ -12,12 +12,12 @@ - {% for title in titles %} - {% for link in title.links %} + {% for title in titles.values() %} + {% for link in title %} {% if not loop.index0 %} - - {{ title.name }} + + {{ link.title.name }} {% endif %} @@ -34,8 +34,8 @@ {% if not loop.index0 %} - - + +