diff --git a/cps/db.py b/cps/db.py index f337ea4c..7428eba1 100755 --- a/cps/db.py +++ b/cps/db.py @@ -26,6 +26,11 @@ def title_sort(title): title = title.replace(prep, '') + ', ' + prep return title.strip() +def lcase(s): + return s.lower() + +def ucase(s): + return s.upper() Base = declarative_base() @@ -319,6 +324,8 @@ def setup_db(): ub.session.commit() config.loadSettings() 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: cc = conn.execute("SELECT id, datatype FROM custom_columns") diff --git a/cps/web.py b/cps/web.py index 8a00ee7c..1f4a5136 100755 --- a/cps/web.py +++ b/cps/web.py @@ -662,11 +662,13 @@ def feed_normal_search(): def feed_search(term): 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 + "%")))\ + term = term.strip().lower() + db.session.connection().connection.connection.create_function("lower", 1, db.lcase) + entries = db.session.query(db.Books).filter(db.or_(db.Books.tags.any(db.Tags.name.ilike("%" + term + "%")), + db.Books.series.any(db.Series.name.ilike("%" + 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() entriescount = len(entries) if len(entries) > 0 else 1 pagination = Pagination(1, entriescount, entriescount) @@ -912,7 +914,7 @@ def get_authors_json(): if request.method == "GET": query = request.args.get('q') # 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]) return json_dumps @@ -923,7 +925,7 @@ def get_tags_json(): if request.method == "GET": query = request.args.get('q') # 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: # alfa = dict(name=x.name) json_dumps = json.dumps([dict(name=r.name) for r in entries]) @@ -1001,7 +1003,7 @@ def get_languages_json(): def get_series_json(): if request.method == "GET": 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 + "%'") json_dumps = json.dumps([dict(name=r.name) for r in entries]) return json_dumps @@ -1017,8 +1019,8 @@ def get_matching_tags(): title_input = request.args.get('book_title') include_tag_inputs = request.args.getlist('include_tag') exclude_tag_inputs = request.args.getlist('exclude_tag') - q = q.filter(db.Books.authors.any(db.Authors.name.like("%" + author_input + "%")), - db.Books.title.like("%" + title_input + "%")) + q = q.filter(db.Books.authors.any(db.Authors.name.ilike("%" + author_input + "%")), + db.Books.title.ilike("%" + title_input + "%")) if len(include_tag_inputs) > 0: for tag in include_tag_inputs: q = q.filter(db.Books.tags.any(db.Tags.id == tag)) @@ -1607,14 +1609,20 @@ def update(): @app.route("/search", methods=["GET"]) @login_required_if_no_ano def search(): - term = request.args.get("query").strip() + term = request.args.get("query").strip().lower() + 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 + "%")))\ + db.session.connection().connection.connection.create_function("lower", 1, db.lcase) + entries = db.session.query(db.Books).filter(db.or_(db.Books.tags.any(db.Tags.name.ilike("%" + term + "%")), + db.Books.series.any(db.Series.name.ilike("%" + 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() +# 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) else: return render_title_template('search.html', searchterm="") @@ -1624,6 +1632,7 @@ def search(): @login_required_if_no_ano def advanced_search(): if request.method == 'GET': + db.session.connection().connection.connection.create_function("lower", 1, db.lcase) q = db.session.query(db.Books) include_tag_inputs = request.args.getlist('include_tag') exclude_tag_inputs = request.args.getlist('exclude_tag') @@ -1635,9 +1644,9 @@ def advanced_search(): author_name = request.args.get("author_name") book_title = request.args.get("book_title") publisher = request.args.get("publisher") - if author_name: author_name = author_name.strip() - if book_title: book_title = book_title.strip() - if publisher: publisher = publisher.strip() + if author_name: author_name = author_name.strip().lower() + if book_title: book_title = book_title.strip().lower() + if publisher: publisher = publisher.strip().lower() 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: searchterm = [] @@ -1656,9 +1665,9 @@ def advanced_search(): lang.name = _(isoLanguages.get(part3=lang.lang_code).name) searchterm.extend(language.name for language in language_names) searchterm = " + ".join(filter(None, searchterm)) - q = q.filter(db.Books.authors.any(db.Authors.name.like("%" + author_name + "%")), - db.Books.title.like("%" + book_title + "%"), - db.Books.publishers.any(db.Publishers.name.like("%" + publisher + "%"))) + q = q.filter(db.Books.authors.any(db.Authors.name.ilike("%" + author_name + "%")), + db.Books.title.ilike("%" + book_title + "%"), + db.Books.publishers.any(db.Publishers.name.ilike("%" + publisher + "%"))) for tag in include_tag_inputs: q = q.filter(db.Books.tags.any(db.Tags.id == tag)) for tag in exclude_tag_inputs: