1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-12-26 10:00:37 +00:00

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.
This commit is contained in:
Jonathan Rehm 2017-08-26 08:12:16 -07:00
parent 193605df4a
commit 3213bf50b3

View File

@ -501,24 +501,29 @@ def edit_required(f):
return inner return inner
# Fill indexpage with all requested data from database # Language and content filters
def fill_indexpage(page, database, db_filter, order): def common_filters():
if current_user.filter_language() != "all": if current_user.filter_language() != "all":
lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language()) lang_filter = db.Books.languages.any(db.Languages.lang_code == current_user.filter_language())
else: else:
lang_filter = True lang_filter = True
content_rating_filter = false() if current_user.mature_content else \ content_rating_filter = false() if current_user.mature_content else \
db.Books.tags.any(db.Tags.name.in_(config.mature_content_tags())) 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(): 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) .order_by(func.random()).limit(config.config_random_books)
else: else:
random = false random = false
off = int(int(config.config_books_per_page) * (page - 1)) off = int(int(config.config_books_per_page) * (page - 1))
pagination = Pagination(page, config.config_books_per_page, pagination = Pagination(page, config.config_books_per_page,
len(db.session.query(database) len(db.session.query(database)
.filter(db_filter).filter(lang_filter).filter(~content_rating_filter).all())) .filter(db_filter).filter(common_filters()).all()))
entries = db.session.query(database).filter(db_filter).filter(lang_filter).filter(~content_rating_filter)\ entries = db.session.query(database).filter(common_filters())\
.order_by(order).offset(off).limit(config.config_books_per_page) .order_by(order).offset(off).limit(config.config_books_per_page)
return entries, random, pagination return entries, random, pagination
@ -640,19 +645,13 @@ def feed_normal_search():
def feed_search(term): 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: if term:
entries = db.session.query(db.Books).filter(db.or_(db.Books.tags.any(db.Tags.name.like("%" + 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.series.any(db.Series.name.like("%" + term + "%")),
db.Books.authors.any(db.Authors.name.like("%" + term + "%")), db.Books.authors.any(db.Authors.name.like("%" + term + "%")),
db.Books.publishers.any(db.Publishers.name.like("%" + term + "%")), db.Books.publishers.any(db.Publishers.name.like("%" + term + "%")),
db.Books.title.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 entriescount = len(entries) if len(entries) > 0 else 1
pagination = Pagination(1, entriescount, entriescount) pagination = Pagination(1, entriescount, entriescount)
xml = render_title_template('feed.xml', searchterm=term, entries=entries, pagination=pagination) xml = render_title_template('feed.xml', searchterm=term, entries=entries, pagination=pagination)
@ -680,13 +679,7 @@ def feed_new():
@app.route("/opds/discover") @app.route("/opds/discover")
@requires_basic_auth_if_no_ano @requires_basic_auth_if_no_ano
def feed_discover(): def feed_discover():
if current_user.filter_language() != "all": entries = db.session.query(db.Books).filter(common_filters()).order_by(func.random())\
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())\
.limit(config.config_books_per_page) .limit(config.config_books_per_page)
pagination = Pagination(1, config.config_books_per_page, int(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) xml = render_title_template('feed.xml', entries=entries, pagination=pagination)
@ -715,12 +708,6 @@ def feed_hot():
off = request.args.get("offset") off = request.args.get("offset")
if not off: if not off:
off = 0 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( 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) 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) hot_books = all_books.offset(off).limit(config.config_books_per_page)
@ -729,7 +716,7 @@ def feed_hot():
downloadBook = db.session.query(db.Books).filter(db.Books.id == book.Downloads.book_id).first() downloadBook = db.session.query(db.Books).filter(db.Books.id == book.Downloads.book_id).first()
if downloadBook: if downloadBook:
entries.append( entries.append(
db.session.query(db.Books).filter(lang_filter).filter(~content_rating_filter) db.session.query(db.Books).filter(common_filters())
.filter(db.Books.id == book.Downloads.book_id).first() .filter(db.Books.id == book.Downloads.book_id).first()
) )
else: else:
@ -749,14 +736,7 @@ def feed_authorindex():
off = request.args.get("offset") off = request.args.get("offset")
if not off: if not off:
off = 0 off = 0
if current_user.filter_language() != "all": entries = db.session.query(db.Authors).join(db.books_authors_link).join(db.Books).filter(common_filters())\
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)\
.group_by('books_authors_link.author').order_by(db.Authors.sort).limit(config.config_books_per_page).offset(off) .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, pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
len(db.session.query(db.Authors).all())) len(db.session.query(db.Authors).all()))
@ -786,14 +766,7 @@ def feed_categoryindex():
off = request.args.get("offset") off = request.args.get("offset")
if not off: if not off:
off = 0 off = 0
if current_user.filter_language() != "all": entries = db.session.query(db.Tags).join(db.books_tags_link).join(db.Books).filter(common_filters())\
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)\
.group_by('books_tags_link.tag').order_by(db.Tags.name).offset(off).limit(config.config_books_per_page) .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, pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
len(db.session.query(db.Tags).all())) len(db.session.query(db.Tags).all()))
@ -823,14 +796,7 @@ def feed_seriesindex():
off = request.args.get("offset") off = request.args.get("offset")
if not off: if not off:
off = 0 off = 0
if current_user.filter_language() != "all": entries = db.session.query(db.Series).join(db.books_series_link).join(db.Books).filter(common_filters())\
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)\
.group_by('books_series_link.series').order_by(db.Series.sort).offset(off).all() .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, pagination = Pagination((int(off) / (int(config.config_books_per_page)) + 1), config.config_books_per_page,
len(db.session.query(db.Series).all())) len(db.session.query(db.Series).all()))
@ -1096,14 +1062,8 @@ def titles_descending(page):
@app.route('/hot/page/<int:page>') @app.route('/hot/page/<int:page>')
@login_required_if_no_ano @login_required_if_no_ano
def hot_books(page): 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(): 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) .order_by(func.random()).limit(config.config_random_books)
else: else:
random = false random = false
@ -1116,7 +1076,7 @@ def hot_books(page):
downloadBook = db.session.query(db.Books).filter(db.Books.id == book.Downloads.book_id).first() downloadBook = db.session.query(db.Books).filter(db.Books.id == book.Downloads.book_id).first()
if downloadBook: if downloadBook:
entries.append( entries.append(
db.session.query(db.Books).filter(lang_filter).filter(~content_rating_filter) db.session.query(db.Books).filter(common_filters())
.filter(db.Books.id == book.Downloads.book_id).first() .filter(db.Books.id == book.Downloads.book_id).first()
) )
else: else:
@ -1150,15 +1110,8 @@ def discover(page):
@app.route("/author") @app.route("/author")
@login_required_if_no_ano @login_required_if_no_ano
def author_list(): 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'))\ entries = db.session.query(db.Authors, func.count('books_authors_link.book').label('count'))\
.join(db.books_authors_link).join(db.Books)\ .join(db.books_authors_link).join(db.Books).filter(common_filters())\
.filter(lang_filter).filter(~content_rating_filter)\
.group_by('books_authors_link.author').order_by(db.Authors.sort).all() .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")) 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") @app.route("/series")
@login_required_if_no_ano @login_required_if_no_ano
def series_list(): 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'))\ entries = db.session.query(db.Series, func.count('books_series_link.book').label('count'))\
.join(db.books_series_link).join(db.Books)\ .join(db.books_series_link).join(db.Books).filter(common_filters())\
.filter(lang_filter).filter(~content_rating_filter)\
.group_by('books_series_link.series').order_by(db.Series.sort).all() .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")) 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") @app.route("/category")
@login_required_if_no_ano @login_required_if_no_ano
def category_list(): 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'))\ entries = db.session.query(db.Tags, func.count('books_tags_link.book').label('count'))\
.join(db.books_tags_link).join(db.Books)\ .join(db.books_tags_link).join(db.Books).filter(common_filters())\
.filter(lang_filter).filter(~content_rating_filter)\
.group_by('books_tags_link.tag').all() .group_by('books_tags_link.tag').all()
return render_title_template('list.html', entries=entries, folder='category', title=_(u"Category list")) 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/<int:book_id>") @app.route("/book/<int:book_id>")
@login_required_if_no_ano @login_required_if_no_ano
def show_book(book_id): def show_book(book_id):
if current_user.filter_language() != "all": entries = db.session.query(db.Books).filter(db.Books.id == book_id).filter(common_filters()).first()
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()
if entries: if entries:
for index in range(0, len(entries.languages)): for index in range(0, len(entries.languages)):
try: try:
@ -1589,18 +1521,12 @@ def update():
def search(): def search():
term = request.args.get("query").strip() term = request.args.get("query").strip()
if term: 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 + "%")), 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.series.any(db.Series.name.like("%" + term + "%")),
db.Books.authors.any(db.Authors.name.like("%" + term + "%")), db.Books.authors.any(db.Authors.name.like("%" + term + "%")),
db.Books.publishers.any(db.Publishers.name.like("%" + term + "%")), db.Books.publishers.any(db.Publishers.name.like("%" + term + "%")),
db.Books.title.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) return render_title_template('search.html', searchterm=term, entries=entries)
else: else:
return render_title_template('search.html', searchterm="") return render_title_template('search.html', searchterm="")
@ -2743,14 +2669,8 @@ def edit_book(book_id):
# create the function for sorting... # create the function for sorting...
db.session.connection().connection.connection.create_function("title_sort", 1, db.title_sort) 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() 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)\ 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 = [] author_names = []
# Book not found # Book not found