1
0
mirror of https://github.com/janeczku/calibre-web synced 2025-01-23 23:46:56 +00:00

Some code cosmetics

This commit is contained in:
Ozzie Isaacs 2021-03-14 14:40:04 +01:00
parent f52fa41439
commit 4df443e007

View File

@ -977,6 +977,19 @@ def search():
title=_(u"Search"), title=_(u"Search"),
page="search") page="search")
@web.route("/advsearch", methods=['POST'])
@login_required_if_no_ano
def advanced_search():
values = dict(request.form)
params = ['include_tag', 'exclude_tag', 'include_serie', 'exclude_serie', 'include_language',
'exclude_language', 'include_extension', 'exclude_extension']
for param in params:
values[param] = list(request.form.getlist(param))
flask_session['query'] = json.dumps(values)
return redirect(url_for('web.books_list', data="advsearch", sort_param='stored', query=""))
def adv_search_custom_columns(cc, term, q): def adv_search_custom_columns(cc, term, q):
for c in cc: for c in cc:
custom_query = term.get('custom_column_' + str(c.id)) custom_query = term.get('custom_column_' + str(c.id))
@ -996,6 +1009,72 @@ def adv_search_custom_columns(cc, term, q):
return q return q
def adv_search_language(q, include_languages_inputs, exclude_languages_inputs):
if current_user.filter_language() != "all":
q = q.filter(db.Books.languages.any(db.Languages.lang_code == current_user.filter_language()))
else:
for language in include_languages_inputs:
q = q.filter(db.Books.languages.any(db.Languages.id == language))
for language in exclude_languages_inputs:
q = q.filter(not_(db.Books.series.any(db.Languages.id == language)))
return q
def adv_search_ratings(q, rating_high, rating_low):
if rating_high:
rating_high = int(rating_high) * 2
q = q.filter(db.Books.ratings.any(db.Ratings.rating <= rating_high))
if rating_low:
rating_low = int(rating_low) * 2
q = q.filter(db.Books.ratings.any(db.Ratings.rating >= rating_low))
return q
def adv_search_read_status(q, read_status):
if read_status:
if config.config_read_column:
if read_status == "True":
q = q.join(db.cc_classes[config.config_read_column], isouter=True) \
.filter(db.cc_classes[config.config_read_column].value == True)
else:
q = q.join(db.cc_classes[config.config_read_column], isouter=True) \
.filter(coalesce(db.cc_classes[config.config_read_column].value, False) != True)
else:
if read_status == "True":
q = q.join(ub.ReadBook, db.Books.id == ub.ReadBook.book_id, isouter=True) \
.filter(ub.ReadBook.user_id == int(current_user.id),
ub.ReadBook.read_status == ub.ReadBook.STATUS_FINISHED)
else:
q = q.join(ub.ReadBook, db.Books.id == ub.ReadBook.book_id, isouter=True) \
.filter(ub.ReadBook.user_id == int(current_user.id),
coalesce(ub.ReadBook.read_status, 0) != ub.ReadBook.STATUS_FINISHED)
return q
def adv_search_extension(q, include_extension_inputs, exclude_extension_inputs):
for extension in include_extension_inputs:
q = q.filter(db.Books.data.any(db.Data.format == extension))
for extension in exclude_extension_inputs:
q = q.filter(not_(db.Books.data.any(db.Data.format == extension)))
return q
def adv_search_tag(q, include_tag_inputs, exclude_tag_inputs):
for tag in include_tag_inputs:
q = q.filter(db.Books.tags.any(db.Tags.id == tag))
for tag in exclude_tag_inputs:
q = q.filter(not_(db.Books.tags.any(db.Tags.id == tag)))
return q
def adv_search_serie(q, include_series_inputs, exclude_series_inputs):
for serie in include_series_inputs:
q = q.filter(db.Books.series.any(db.Series.id == serie))
for serie in exclude_series_inputs:
q = q.filter(not_(db.Books.series.any(db.Series.id == serie)))
return q
def extend_search_term(searchterm, def extend_search_term(searchterm,
author_name, author_name,
book_title, book_title,
@ -1051,19 +1130,7 @@ def extend_search_term(searchterm,
searchterm.extend(ext for ext in exclude_extension_inputs) searchterm.extend(ext for ext in exclude_extension_inputs)
# handle custom columns # handle custom columns
searchterm = " + ".join(filter(None, searchterm)) searchterm = " + ".join(filter(None, searchterm))
return searchterm return searchterm, pub_start, pub_end
@web.route("/advsearch", methods=['POST'])
@login_required_if_no_ano
def advanced_search():
values = dict(request.form)
params = ['include_tag', 'exclude_tag', 'include_serie', 'exclude_serie', 'include_language',
'exclude_language', 'include_extension', 'exclude_extension']
for param in params:
values[param] = list(request.form.getlist(param))
flask_session['query'] = json.dumps(values)
return redirect(url_for('web.books_list', data="advsearch", sort_param='stored', query=""))
def render_adv_search_results(term, offset=None, order=None, limit=None): def render_adv_search_results(term, offset=None, order=None, limit=None):
@ -1110,7 +1177,7 @@ def render_adv_search_results(term, offset=None, order=None, limit=None):
include_languages_inputs or exclude_languages_inputs or author_name or book_title or \ include_languages_inputs or exclude_languages_inputs or author_name or book_title or \
publisher or pub_start or pub_end or rating_low or rating_high or description or cc_present or \ publisher or pub_start or pub_end or rating_low or rating_high or description or cc_present or \
include_extension_inputs or exclude_extension_inputs or read_status: include_extension_inputs or exclude_extension_inputs or read_status:
searchterm = extend_search_term(searchterm, searchterm, pub_start, pub_end = extend_search_term(searchterm,
author_name, author_name,
book_title, book_title,
publisher, publisher,
@ -1135,50 +1202,15 @@ def render_adv_search_results(term, offset=None, order=None, limit=None):
q = q.filter(db.Books.pubdate >= pub_start) q = q.filter(db.Books.pubdate >= pub_start)
if pub_end: if pub_end:
q = q.filter(db.Books.pubdate <= pub_end) q = q.filter(db.Books.pubdate <= pub_end)
if read_status: q = adv_search_read_status(q, read_status)
if config.config_read_column:
if read_status=="True":
q = q.join(db.cc_classes[config.config_read_column], isouter=True) \
.filter(db.cc_classes[config.config_read_column].value == True)
else:
q = q.join(db.cc_classes[config.config_read_column], isouter=True) \
.filter(coalesce(db.cc_classes[config.config_read_column].value, False) != True)
else:
if read_status == "True":
q = q.join(ub.ReadBook, db.Books.id==ub.ReadBook.book_id, isouter=True)\
.filter(ub.ReadBook.user_id == int(current_user.id),
ub.ReadBook.read_status == ub.ReadBook.STATUS_FINISHED)
else:
q = q.join(ub.ReadBook, db.Books.id == ub.ReadBook.book_id, isouter=True) \
.filter(ub.ReadBook.user_id == int(current_user.id),
coalesce(ub.ReadBook.read_status, 0) != ub.ReadBook.STATUS_FINISHED)
if publisher: if publisher:
q = q.filter(db.Books.publishers.any(func.lower(db.Publishers.name).ilike("%" + publisher + "%"))) q = q.filter(db.Books.publishers.any(func.lower(db.Publishers.name).ilike("%" + publisher + "%")))
for tag in include_tag_inputs: q = adv_search_tag(q, include_tag_inputs, exclude_tag_inputs)
q = q.filter(db.Books.tags.any(db.Tags.id == tag)) q = adv_search_serie(q, include_series_inputs, exclude_series_inputs)
for tag in exclude_tag_inputs: q = adv_search_extension(q, include_extension_inputs, exclude_extension_inputs)
q = q.filter(not_(db.Books.tags.any(db.Tags.id == tag))) q = adv_search_language(q, include_languages_inputs, exclude_languages_inputs)
for serie in include_series_inputs: q = adv_search_ratings(q, rating_high, rating_low)
q = q.filter(db.Books.series.any(db.Series.id == serie))
for serie in exclude_series_inputs:
q = q.filter(not_(db.Books.series.any(db.Series.id == serie)))
for extension in include_extension_inputs:
q = q.filter(db.Books.data.any(db.Data.format == extension))
for extension in exclude_extension_inputs:
q = q.filter(not_(db.Books.data.any(db.Data.format == extension)))
if current_user.filter_language() != "all":
q = q.filter(db.Books.languages.any(db.Languages.lang_code == current_user.filter_language()))
else:
for language in include_languages_inputs:
q = q.filter(db.Books.languages.any(db.Languages.id == language))
for language in exclude_languages_inputs:
q = q.filter(not_(db.Books.series.any(db.Languages.id == language)))
if rating_high:
rating_high = int(rating_high) * 2
q = q.filter(db.Books.ratings.any(db.Ratings.rating <= rating_high))
if rating_low:
rating_low = int(rating_low) * 2
q = q.filter(db.Books.ratings.any(db.Ratings.rating >= rating_low))
if description: if description:
q = q.filter(db.Books.comments.any(func.lower(db.Comments.text).ilike("%" + description + "%"))) q = q.filter(db.Books.comments.any(func.lower(db.Comments.text).ilike("%" + description + "%")))
@ -1188,7 +1220,6 @@ def render_adv_search_results(term, offset=None, order=None, limit=None):
q = q.order_by(*order).all() q = q.order_by(*order).all()
flask_session['query'] = json.dumps(term) flask_session['query'] = json.dumps(term)
ub.store_ids(q) ub.store_ids(q)
# entries, result_count, pagination = calibre_db.get_search_results(term, offset, order, limit)
result_count = len(q) result_count = len(q)
if offset != None and limit != None: if offset != None and limit != None:
offset = int(offset) offset = int(offset)