1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-11-30 21:40:00 +00:00

Update search for cyrillic letters #877

This commit is contained in:
Ozzieisaacs 2019-04-19 06:54:12 +02:00
parent 67736fe187
commit 544405f5aa
2 changed files with 28 additions and 36 deletions

View File

@ -27,6 +27,7 @@ import ast
from ub import config from ub import config
import ub import ub
import sys import sys
import unidecode
session = None session = None
cc_exceptions = ['datetime', 'comments', 'float', 'composite', 'series'] cc_exceptions = ['datetime', 'comments', 'float', 'composite', 'series']
@ -46,7 +47,7 @@ def title_sort(title):
def lcase(s): def lcase(s):
return s.lower() return unidecode.unidecode(s.lower())
def ucase(s): def ucase(s):
@ -359,8 +360,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('lower', 1, lcase)
conn.connection.create_function('upper', 1, ucase) # 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")

View File

@ -668,25 +668,20 @@ def modify_database_object(input_elements, db_book_object, db_object, db_session
# read search results from calibre-database and return it (function is used for feed and simple search # read search results from calibre-database and return it (function is used for feed and simple search
def get_search_results(term): def get_search_results(term):
db.session.connection().connection.connection.create_function("lower", 1, db.lcase)
q = list() q = list()
authorterms = re.split("[, ]+", term) authorterms = re.split("[, ]+", term)
for authorterm in authorterms: for authorterm in authorterms:
q.append(db.Books.authors.any(db.or_(db.Authors.name.ilike("%" + authorterm + "%"), q.append(db.Books.authors.any(db.func.lower(db.Authors.name).ilike("%" + authorterm + "%")))
db.Authors.name.ilike("%" + unidecode.unidecode(authorterm) + "%"))))
db.session.connection().connection.connection.create_function("lower", 1, db.lcase) db.Books.authors.any(db.func.lower(db.Authors.name).ilike("%" + term + "%"))
db.Books.authors.any(db.or_(db.Authors.name.ilike("%" + term + "%"),
db.Authors.name.ilike("%" + unidecode.unidecode(term) + "%")))
return db.session.query(db.Books).filter(common_filters()).filter( return db.session.query(db.Books).filter(common_filters()).filter(
db.or_(db.Books.tags.any(db.Tags.name.ilike("%" + term + "%")), db.or_(db.Books.tags.any(db.func.lower(db.Tags.name).ilike("%" + term + "%")),
db.Books.series.any(db.Series.name.ilike("%" + term + "%")), db.Books.series.any(db.func.lower(db.Series.name).ilike("%" + term + "%")),
db.Books.authors.any(and_(*q)), db.Books.authors.any(and_(*q)),
db.Books.publishers.any(db.Publishers.name.ilike("%" + term + "%")), db.Books.publishers.any(db.func.lower(db.Publishers.name).ilike("%" + term + "%")),
db.Books.title.ilike("%" + term + "%"), db.func.lower(db.Books.title).ilike("%" + term + "%")
db.Books.tags.any(db.Tags.name.ilike("%" + unidecode.unidecode(term) + "%")),
db.Books.series.any(db.Series.name.ilike("%" + unidecode.unidecode(term) + "%")),
db.Books.publishers.any(db.Publishers.name.ilike("%" + unidecode.unidecode(term) + "%")),
db.Books.title.ilike("%" + unidecode.unidecode(term) + "%")
)).all() )).all()
@ -1097,7 +1092,8 @@ def get_comic_book(book_id, book_format, page):
def get_authors_json(): 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.query(db.Authors).filter(db.Authors.name.ilike("%" + query + "%")).all() db.session.connection().connection.connection.create_function("lower", 1, db.lcase)
entries = db.session.query(db.Authors).filter(db.func.lower(db.Authors.name).ilike("%" + query + "%")).all()
json_dumps = json.dumps([dict(name=r.name.replace('|',',')) for r in entries]) json_dumps = json.dumps([dict(name=r.name.replace('|',',')) for r in entries])
return json_dumps return json_dumps
@ -1107,7 +1103,8 @@ def get_authors_json():
def get_publishers_json(): def get_publishers_json():
if request.method == "GET": if request.method == "GET":
query = request.args.get('q') query = request.args.get('q')
entries = db.session.query(db.Publishers).filter(db.Publishers.name.ilike("%" + query + "%")).all() db.session.connection().connection.connection.create_function("lower", 1, db.lcase)
entries = db.session.query(db.Publishers).filter(db.func.lower(db.Publishers.name).ilike("%" + query + "%")).all()
json_dumps = json.dumps([dict(name=r.name.replace('|',',')) for r in entries]) json_dumps = json.dumps([dict(name=r.name.replace('|',',')) for r in entries])
return json_dumps return json_dumps
@ -1117,7 +1114,8 @@ def get_publishers_json():
def get_tags_json(): 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.query(db.Tags).filter(db.Tags.name.ilike("%" + query + "%")).all() db.session.connection().connection.connection.create_function("lower", 1, db.lcase)
entries = db.session.query(db.Tags).filter(db.func.lower(db.Tags.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
@ -1143,7 +1141,8 @@ 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.ilike("%" + query + "%")).all() db.session.connection().connection.connection.create_function("lower", 1, db.lcase)
entries = db.session.query(db.Series).filter(db.func.lower(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
@ -1155,12 +1154,13 @@ def get_matching_tags():
tag_dict = {'tags': []} tag_dict = {'tags': []}
if request.method == "GET": if request.method == "GET":
q = db.session.query(db.Books) q = db.session.query(db.Books)
db.session.connection().connection.connection.create_function("lower", 1, db.lcase)
author_input = request.args.get('author_name') author_input = request.args.get('author_name')
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.ilike("%" + author_input + "%")), q = q.filter(db.Books.authors.any(db.func.lower(db.Authors.name).ilike("%" + author_input + "%")),
db.Books.title.ilike("%" + title_input + "%")) db.func.lower(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))
@ -1916,7 +1916,6 @@ def advanced_search():
db.session.connection().connection.connection.create_function("lower", 1, db.lcase) db.session.connection().connection.connection.create_function("lower", 1, db.lcase)
q = db.session.query(db.Books) q = db.session.query(db.Books)
# postargs = request.form.to_dict()
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')
@ -1982,20 +1981,15 @@ def advanced_search():
searchterm = " + ".join(filter(None, searchterm)) searchterm = " + ".join(filter(None, searchterm))
q = q.filter() q = q.filter()
if author_name: if author_name:
q = q.filter(db.Books.authors.any(db.or_(db.Authors.name.ilike("%" + author_name + "%"), q = q.filter(db.Books.authors.any(db.func.lower(db.Authors.name).ilike("%" + author_name + "%")))
db.Authors.name.ilike("%" + unidecode.unidecode(author_name)
+ "%"))))
if book_title: if book_title:
q = q.filter(db.or_(db.Books.title.ilike("%" + book_title + "%"), q = q.filter(db.func.lower(db.Books.title).ilike("%" + book_title + "%"))
db.Books.title.ilike("%" + unidecode.unidecode(book_title) + "%")))
if pub_start: if pub_start:
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 publisher: if publisher:
q = q.filter(db.Books.publishers.any(db.or_(db.Publishers.name.ilike("%" + publisher + "%"), q = q.filter(db.Books.publishers.any(db.func.lower(db.Publishers.name).ilike("%" + publisher + "%")))
db.Publishers.name.ilike("%" + unidecode.unidecode(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:
@ -2018,9 +2012,7 @@ def advanced_search():
rating_low = int(rating_low) *2 rating_low = int(rating_low) *2
q = q.filter(db.Books.ratings.any(db.Ratings.rating >= rating_low)) q = q.filter(db.Books.ratings.any(db.Ratings.rating >= rating_low))
if description: if description:
q = q.filter(db.Books.comments.any(db.or_(db.Comments.text.ilike("%" + description + "%"), q = q.filter(db.Books.comments.any(db.func.lower(db.Comments.text).ilike("%" + description + "%")))
db.Comments.text.ilike("%" + unidecode.unidecode(description)
+ "%"))))
# search custom culumns # search custom culumns
for c in cc: for c in cc:
@ -2035,8 +2027,7 @@ def advanced_search():
db.cc_classes[c.id].value == custom_query )) db.cc_classes[c.id].value == custom_query ))
else: else:
q = q.filter(getattr(db.Books, 'custom_column_'+str(c.id)).any( q = q.filter(getattr(db.Books, 'custom_column_'+str(c.id)).any(
db.or_(db.cc_classes[c.id].value.ilike("%" + custom_query + "%"), db.func.lower(db.cc_classes[c.id].value).ilike("%" + custom_query + "%")))
db.cc_classes[c.id].value.ilike("%" + unidecode.unidecode(custom_query) + "%"))))
q = q.all() q = q.all()
ids = list() ids = list()
for element in q: for element in q: