Merge remote-tracking branch 'opds/master'

pull/394/head
OzzieIsaacs 7 years ago
commit 798cbc7505

@ -26,6 +26,11 @@ def title_sort(title):
title = title.replace(prep, '') + ', ' + prep title = title.replace(prep, '') + ', ' + prep
return title.strip() return title.strip()
def lcase(s):
return s.lower()
def ucase(s):
return s.upper()
Base = declarative_base() Base = declarative_base()
@ -319,6 +324,8 @@ def setup_db():
ub.session.commit() ub.session.commit()
config.loadSettings() config.loadSettings()
conn.connection.create_function('title_sort', 1, title_sort) conn.connection.create_function('title_sort', 1, title_sort)
conn.connection.create_function('lower', 1, lcase)
conn.connection.create_function('upper', 1, ucase)
if not cc_classes: if not cc_classes:
cc = conn.execute("SELECT id, datatype FROM custom_columns") cc = conn.execute("SELECT id, datatype FROM custom_columns")

@ -662,11 +662,13 @@ def feed_normal_search():
def feed_search(term): def feed_search(term):
if term: if term:
entries = db.session.query(db.Books).filter(db.or_(db.Books.tags.any(db.Tags.name.like("%" + term + "%")), term = term.strip().lower()
db.Books.series.any(db.Series.name.like("%" + term + "%")), db.session.connection().connection.connection.create_function("lower", 1, db.lcase)
db.Books.authors.any(db.Authors.name.like("%" + term + "%")), entries = db.session.query(db.Books).filter(db.or_(db.Books.tags.any(db.Tags.name.ilike("%" + term + "%")),
db.Books.publishers.any(db.Publishers.name.like("%" + term + "%")), db.Books.series.any(db.Series.name.ilike("%" + term + "%")),
db.Books.title.like("%" + term + "%")))\ db.Books.authors.any(db.Authors.name.ilike("%" + term + "%")),
db.Books.publishers.any(db.Publishers.name.ilike("%" + term + "%")),
db.Books.title.ilike("%" + term + "%")))\
.filter(common_filters()).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)
@ -912,7 +914,7 @@ def get_authors_json():
if request.method == "GET": if request.method == "GET":
query = request.args.get('q') query = request.args.get('q')
# entries = db.session.execute("select name from authors where name like '%" + query + "%'") # entries = db.session.execute("select name from authors where name like '%" + query + "%'")
entries = db.session.query(db.Authors).filter(db.Authors.name.like("%" + query + "%")).all() entries = db.session.query(db.Authors).filter(db.Authors.name.ilike("%" + query + "%")).all()
json_dumps = json.dumps([dict(name=r.name) for r in entries]) json_dumps = json.dumps([dict(name=r.name) for r in entries])
return json_dumps return json_dumps
@ -923,7 +925,7 @@ def get_tags_json():
if request.method == "GET": if request.method == "GET":
query = request.args.get('q') query = request.args.get('q')
# entries = db.session.execute("select name from tags where name like '%" + query + "%'") # entries = db.session.execute("select name from tags where name like '%" + query + "%'")
entries = db.session.query(db.Tags).filter(db.Tags.name.like("%" + query + "%")).all() entries = db.session.query(db.Tags).filter(db.Tags.iname.ilike("%" + query + "%")).all()
# for x in entries: # for x in entries:
# alfa = dict(name=x.name) # alfa = dict(name=x.name)
json_dumps = json.dumps([dict(name=r.name) for r in entries]) json_dumps = json.dumps([dict(name=r.name) for r in entries])
@ -1001,7 +1003,7 @@ def get_languages_json():
def get_series_json(): def get_series_json():
if request.method == "GET": if request.method == "GET":
query = request.args.get('q') query = request.args.get('q')
entries = db.session.query(db.Series).filter(db.Series.name.like("%" + query + "%")).all() entries = db.session.query(db.Series).filter(db.Series.name.ilike("%" + query + "%")).all()
# entries = db.session.execute("select name from series where name like '%" + query + "%'") # entries = db.session.execute("select name from series where name like '%" + query + "%'")
json_dumps = json.dumps([dict(name=r.name) for r in entries]) json_dumps = json.dumps([dict(name=r.name) for r in entries])
return json_dumps return json_dumps
@ -1017,8 +1019,8 @@ def get_matching_tags():
title_input = request.args.get('book_title') title_input = request.args.get('book_title')
include_tag_inputs = request.args.getlist('include_tag') include_tag_inputs = request.args.getlist('include_tag')
exclude_tag_inputs = request.args.getlist('exclude_tag') exclude_tag_inputs = request.args.getlist('exclude_tag')
q = q.filter(db.Books.authors.any(db.Authors.name.like("%" + author_input + "%")), q = q.filter(db.Books.authors.any(db.Authors.name.ilike("%" + author_input + "%")),
db.Books.title.like("%" + title_input + "%")) db.Books.title.ilike("%" + title_input + "%"))
if len(include_tag_inputs) > 0: if len(include_tag_inputs) > 0:
for tag in include_tag_inputs: for tag in include_tag_inputs:
q = q.filter(db.Books.tags.any(db.Tags.id == tag)) q = q.filter(db.Books.tags.any(db.Tags.id == tag))
@ -1607,14 +1609,20 @@ def update():
@app.route("/search", methods=["GET"]) @app.route("/search", methods=["GET"])
@login_required_if_no_ano @login_required_if_no_ano
def search(): def search():
term = request.args.get("query").strip() term = request.args.get("query").strip().lower()
if term: if term:
entries = db.session.query(db.Books).filter(db.or_(db.Books.tags.any(db.Tags.name.like("%" + term + "%")), db.session.connection().connection.connection.create_function("lower", 1, db.lcase)
db.Books.series.any(db.Series.name.like("%" + term + "%")), entries = db.session.query(db.Books).filter(db.or_(db.Books.tags.any(db.Tags.name.ilike("%" + term + "%")),
db.Books.authors.any(db.Authors.name.like("%" + term + "%")), db.Books.series.any(db.Series.name.ilike("%" + term + "%")),
db.Books.publishers.any(db.Publishers.name.like("%" + term + "%")), db.Books.authors.any(db.Authors.name.ilike("%" + term + "%")),
db.Books.title.like("%" + term + "%")))\ db.Books.publishers.any(db.Publishers.name.ilike("%" + term + "%")),
db.Books.title.ilike("%" + term + "%")))\
.filter(common_filters()).all() .filter(common_filters()).all()
# entries = db.session.query(db.Books).with_entities(db.Books.title).filter(db.Books.title.ilike("%" + term + "%")).all()
#result = db.session.execute("select name from authors where lower(name) like '%" + term.lower() + "%'")
#entries = result.fetchall()
#result.close()
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="")
@ -1624,6 +1632,7 @@ def search():
@login_required_if_no_ano @login_required_if_no_ano
def advanced_search(): def advanced_search():
if request.method == 'GET': if request.method == 'GET':
db.session.connection().connection.connection.create_function("lower", 1, db.lcase)
q = db.session.query(db.Books) q = db.session.query(db.Books)
include_tag_inputs = request.args.getlist('include_tag') include_tag_inputs = request.args.getlist('include_tag')
exclude_tag_inputs = request.args.getlist('exclude_tag') exclude_tag_inputs = request.args.getlist('exclude_tag')
@ -1635,9 +1644,9 @@ def advanced_search():
author_name = request.args.get("author_name") author_name = request.args.get("author_name")
book_title = request.args.get("book_title") book_title = request.args.get("book_title")
publisher = request.args.get("publisher") publisher = request.args.get("publisher")
if author_name: author_name = author_name.strip() if author_name: author_name = author_name.strip().lower()
if book_title: book_title = book_title.strip() if book_title: book_title = book_title.strip().lower()
if publisher: publisher = publisher.strip() if publisher: publisher = publisher.strip().lower()
if include_tag_inputs or exclude_tag_inputs or include_series_inputs or exclude_series_inputs or \ if include_tag_inputs or exclude_tag_inputs or include_series_inputs or exclude_series_inputs or \
include_languages_inputs or exclude_languages_inputs or author_name or book_title or publisher: include_languages_inputs or exclude_languages_inputs or author_name or book_title or publisher:
searchterm = [] searchterm = []
@ -1656,9 +1665,9 @@ def advanced_search():
lang.name = _(isoLanguages.get(part3=lang.lang_code).name) lang.name = _(isoLanguages.get(part3=lang.lang_code).name)
searchterm.extend(language.name for language in language_names) searchterm.extend(language.name for language in language_names)
searchterm = " + ".join(filter(None, searchterm)) searchterm = " + ".join(filter(None, searchterm))
q = q.filter(db.Books.authors.any(db.Authors.name.like("%" + author_name + "%")), q = q.filter(db.Books.authors.any(db.Authors.name.ilike("%" + author_name + "%")),
db.Books.title.like("%" + book_title + "%"), db.Books.title.ilike("%" + book_title + "%"),
db.Books.publishers.any(db.Publishers.name.like("%" + publisher + "%"))) db.Books.publishers.any(db.Publishers.name.ilike("%" + publisher + "%")))
for tag in include_tag_inputs: for tag in include_tag_inputs:
q = q.filter(db.Books.tags.any(db.Tags.id == tag)) q = q.filter(db.Books.tags.any(db.Tags.id == tag))
for tag in exclude_tag_inputs: for tag in exclude_tag_inputs:

Loading…
Cancel
Save