From 3213bf50b3fb138fd8083a7580209659273b1ba8 Mon Sep 17 00:00:00 2001 From: Jonathan Rehm Date: Sat, 26 Aug 2017 08:12:16 -0700 Subject: [PATCH] Move common filters into function Instead of duplicating functionality across lots of functions, combine it into one common function. It makes reading and modifying it much easier. --- cps/web.py | 132 +++++++++++------------------------------------------ 1 file changed, 26 insertions(+), 106 deletions(-) diff --git a/cps/web.py b/cps/web.py index 19673453..b4549304 100755 --- a/cps/web.py +++ b/cps/web.py @@ -501,24 +501,29 @@ def edit_required(f): return inner -# Fill indexpage with all requested data from database -def fill_indexpage(page, database, db_filter, order): +# Language and content filters +def common_filters(): if current_user.filter_language() != "all": lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language()) else: lang_filter = True content_rating_filter = false() if current_user.mature_content else \ db.Books.tags.any(db.Tags.name.in_(config.mature_content_tags())) + return and_(lang_filter, ~content_rating_filter) + + +# Fill indexpage with all requested data from database +def fill_indexpage(page, database, db_filter, order): if current_user.show_detail_random(): - random = db.session.query(db.Books).filter(lang_filter).filter(~content_rating_filter)\ + random = db.session.query(db.Books).filter(common_filters())\ .order_by(func.random()).limit(config.config_random_books) else: random = false off = int(int(config.config_books_per_page) * (page - 1)) pagination = Pagination(page, config.config_books_per_page, len(db.session.query(database) - .filter(db_filter).filter(lang_filter).filter(~content_rating_filter).all())) - entries = db.session.query(database).filter(db_filter).filter(lang_filter).filter(~content_rating_filter)\ + .filter(db_filter).filter(common_filters()).all())) + entries = db.session.query(database).filter(common_filters())\ .order_by(order).offset(off).limit(config.config_books_per_page) return entries, random, pagination @@ -640,19 +645,13 @@ def feed_normal_search(): def feed_search(term): - if current_user.filter_language() != "all": - lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language()) - else: - lang_filter = True - content_rating_filter = false() if current_user.mature_content else \ - db.Books.tags.any(db.Tags.name.in_(config.mature_content_tags())) if term: entries = db.session.query(db.Books).filter(db.or_(db.Books.tags.any(db.Tags.name.like("%" + term + "%")), db.Books.series.any(db.Series.name.like("%" + term + "%")), db.Books.authors.any(db.Authors.name.like("%" + term + "%")), db.Books.publishers.any(db.Publishers.name.like("%" + term + "%")), db.Books.title.like("%" + term + "%")))\ - .filter(lang_filter).filter(~content_rating_filter).all() + .filter(common_filters()).all() entriescount = len(entries) if len(entries) > 0 else 1 pagination = Pagination(1, entriescount, entriescount) xml = render_title_template('feed.xml', searchterm=term, entries=entries, pagination=pagination) @@ -680,13 +679,7 @@ def feed_new(): @app.route("/opds/discover") @requires_basic_auth_if_no_ano def feed_discover(): - if current_user.filter_language() != "all": - lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language()) - else: - lang_filter = True - content_rating_filter = false() if current_user.mature_content else \ - db.Books.tags.any(db.Tags.name.in_(config.mature_content_tags())) - entries = db.session.query(db.Books).filter(lang_filter).filter(~content_rating_filter).order_by(func.random())\ + entries = db.session.query(db.Books).filter(common_filters()).order_by(func.random())\ .limit(config.config_books_per_page) pagination = Pagination(1, config.config_books_per_page, int(config.config_books_per_page)) xml = render_title_template('feed.xml', entries=entries, pagination=pagination) @@ -715,12 +708,6 @@ def feed_hot(): off = request.args.get("offset") if not off: off = 0 - if current_user.filter_language() != "all": - lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language()) - else: - lang_filter = True - content_rating_filter = false() if current_user.mature_content else \ - db.Books.tags.any(db.Tags.name.in_(config.mature_content_tags())) all_books = ub.session.query(ub.Downloads, ub.func.count(ub.Downloads.book_id)).order_by( ub.func.count(ub.Downloads.book_id).desc()).group_by(ub.Downloads.book_id) hot_books = all_books.offset(off).limit(config.config_books_per_page) @@ -729,8 +716,8 @@ def feed_hot(): downloadBook = db.session.query(db.Books).filter(db.Books.id == book.Downloads.book_id).first() if downloadBook: entries.append( - db.session.query(db.Books).filter(lang_filter).filter(~content_rating_filter) - .filter(db.Books.id == book.Downloads.book_id).first() + db.session.query(db.Books).filter(common_filters()) + .filter(db.Books.id == book.Downloads.book_id).first() ) else: ub.session.query(ub.Downloads).filter(book.Downloads.book_id == ub.Downloads.book_id).delete() @@ -749,14 +736,7 @@ def feed_authorindex(): off = request.args.get("offset") if not off: off = 0 - if current_user.filter_language() != "all": - lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language()) - else: - lang_filter = True - content_rating_filter = false() if current_user.mature_content else \ - db.Books.tags.any(db.Tags.name.in_(config.mature_content_tags())) - entries = db.session.query(db.Authors).join(db.books_authors_link).join(db.Books)\ - .filter(lang_filter).filter(~content_rating_filter)\ + entries = db.session.query(db.Authors).join(db.books_authors_link).join(db.Books).filter(common_filters())\ .group_by('books_authors_link.author').order_by(db.Authors.sort).limit(config.config_books_per_page).offset(off) pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page, len(db.session.query(db.Authors).all())) @@ -786,14 +766,7 @@ def feed_categoryindex(): off = request.args.get("offset") if not off: off = 0 - if current_user.filter_language() != "all": - lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language()) - else: - lang_filter = True - content_rating_filter = false() if current_user.mature_content else \ - db.Books.tags.any(db.Tags.name.in_(config.mature_content_tags())) - entries = db.session.query(db.Tags).join(db.books_tags_link).join(db.Books)\ - .filter(lang_filter).filter(~content_rating_filter)\ + entries = db.session.query(db.Tags).join(db.books_tags_link).join(db.Books).filter(common_filters())\ .group_by('books_tags_link.tag').order_by(db.Tags.name).offset(off).limit(config.config_books_per_page) pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page, len(db.session.query(db.Tags).all())) @@ -823,14 +796,7 @@ def feed_seriesindex(): off = request.args.get("offset") if not off: off = 0 - if current_user.filter_language() != "all": - lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language()) - else: - lang_filter = True - content_rating_filter = false() if current_user.mature_content else \ - db.Books.tags.any(db.Tags.name.in_(config.mature_content_tags())) - entries = db.session.query(db.Series).join(db.books_series_link).join(db.Books)\ - .filter(lang_filter).filter(~content_rating_filter)\ + entries = db.session.query(db.Series).join(db.books_series_link).join(db.Books).filter(common_filters())\ .group_by('books_series_link.series').order_by(db.Series.sort).offset(off).all() pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page, len(db.session.query(db.Series).all())) @@ -1096,14 +1062,8 @@ def titles_descending(page): @app.route('/hot/page/') @login_required_if_no_ano def hot_books(page): - if current_user.filter_language() != "all": - lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language()) - else: - lang_filter = True - content_rating_filter = false() if current_user.mature_content else \ - db.Books.tags.any(db.Tags.name.in_(config.mature_content_tags())) if current_user.show_detail_random(): - random = db.session.query(db.Books).filter(lang_filter).filter(~content_rating_filter)\ + random = db.session.query(db.Books).filter(common_filters())\ .order_by(func.random()).limit(config.config_random_books) else: random = false @@ -1116,8 +1076,8 @@ def hot_books(page): downloadBook = db.session.query(db.Books).filter(db.Books.id == book.Downloads.book_id).first() if downloadBook: entries.append( - db.session.query(db.Books).filter(lang_filter).filter(~content_rating_filter) - .filter(db.Books.id == book.Downloads.book_id).first() + db.session.query(db.Books).filter(common_filters()) + .filter(db.Books.id == book.Downloads.book_id).first() ) else: ub.session.query(ub.Downloads).filter(book.Downloads.book_id == ub.Downloads.book_id).delete() @@ -1150,15 +1110,8 @@ def discover(page): @app.route("/author") @login_required_if_no_ano def author_list(): - if current_user.filter_language() != "all": - lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language()) - else: - lang_filter = True - content_rating_filter = false() if current_user.mature_content else \ - db.Books.tags.any(db.Tags.name.in_(config.mature_content_tags())) entries = db.session.query(db.Authors, func.count('books_authors_link.book').label('count'))\ - .join(db.books_authors_link).join(db.Books)\ - .filter(lang_filter).filter(~content_rating_filter)\ + .join(db.books_authors_link).join(db.Books).filter(common_filters())\ .group_by('books_authors_link.author').order_by(db.Authors.sort).all() return render_title_template('list.html', entries=entries, folder='author', title=_(u"Author list")) @@ -1187,15 +1140,8 @@ def author(book_id, page): @app.route("/series") @login_required_if_no_ano def series_list(): - if current_user.filter_language() != "all": - lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language()) - else: - lang_filter = True - content_rating_filter = false() if current_user.mature_content else \ - db.Books.tags.any(db.Tags.name.in_(config.mature_content_tags())) entries = db.session.query(db.Series, func.count('books_series_link.book').label('count'))\ - .join(db.books_series_link).join(db.Books)\ - .filter(lang_filter).filter(~content_rating_filter)\ + .join(db.books_series_link).join(db.Books).filter(common_filters())\ .group_by('books_series_link.series').order_by(db.Series.sort).all() return render_title_template('list.html', entries=entries, folder='series', title=_(u"Series list")) @@ -1263,15 +1209,8 @@ def language(name, page): @app.route("/category") @login_required_if_no_ano def category_list(): - if current_user.filter_language() != "all": - lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language()) - else: - lang_filter = True - content_rating_filter = false() if current_user.mature_content else \ - db.Books.tags.any(db.Tags.name.in_(config.mature_content_tags())) entries = db.session.query(db.Tags, func.count('books_tags_link.book').label('count'))\ - .join(db.books_tags_link).join(db.Books)\ - .filter(lang_filter).filter(~content_rating_filter)\ + .join(db.books_tags_link).join(db.Books).filter(common_filters())\ .group_by('books_tags_link.tag').all() return render_title_template('list.html', entries=entries, folder='category', title=_(u"Category list")) @@ -1309,14 +1248,7 @@ def toggle_read(book_id): @app.route("/book/") @login_required_if_no_ano def show_book(book_id): - if current_user.filter_language() != "all": - lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language()) - else: - lang_filter = True - content_rating_filter = false() if current_user.mature_content else \ - db.Books.tags.any(db.Tags.name.in_(config.mature_content_tags())) - entries = db.session.query(db.Books)\ - .filter(db.Books.id == book_id).filter(lang_filter).filter(~content_rating_filter).first() + entries = db.session.query(db.Books).filter(db.Books.id == book_id).filter(common_filters()).first() if entries: for index in range(0, len(entries.languages)): try: @@ -1589,18 +1521,12 @@ def update(): def search(): term = request.args.get("query").strip() if term: - if current_user.filter_language() != "all": - lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language()) - else: - lang_filter = True - content_rating_filter = false() if current_user.mature_content else \ - db.Books.tags.any(db.Tags.name.in_(config.mature_content_tags())) entries = db.session.query(db.Books).filter(db.or_(db.Books.tags.any(db.Tags.name.like("%" + term + "%")), db.Books.series.any(db.Series.name.like("%" + term + "%")), db.Books.authors.any(db.Authors.name.like("%" + term + "%")), db.Books.publishers.any(db.Publishers.name.like("%" + term + "%")), db.Books.title.like("%" + term + "%")))\ - .filter(lang_filter).filter(~content_rating_filter).all() + .filter(common_filters()).all() return render_title_template('search.html', searchterm=term, entries=entries) else: return render_title_template('search.html', searchterm="") @@ -2743,14 +2669,8 @@ def edit_book(book_id): # create the function for sorting... db.session.connection().connection.connection.create_function("title_sort", 1, db.title_sort) cc = db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all() - if current_user.filter_language() != "all": - lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language()) - else: - lang_filter = True - content_rating_filter = false() if current_user.mature_content else \ - db.Books.tags.any(db.Tags.name.in_(config.mature_content_tags())) book = db.session.query(db.Books)\ - .filter(db.Books.id == book_id).filter(lang_filter).filter(~content_rating_filter).first() + .filter(db.Books.id == book_id).filter(common_filters()).first() author_names = [] # Book not found