From: Oleg Broytman Date: Mon, 9 May 2016 21:06:35 +0000 (+0300) Subject: Search with expressions X-Git-Tag: 0.0.4~7 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=a85145dc47b87062148a2599180e229ab7c7af2b;p=m_librarian.git Search with expressions --- diff --git a/m_librarian/search.py b/m_librarian/search.py index a310757..f1d71b7 100644 --- a/m_librarian/search.py +++ b/m_librarian/search.py @@ -8,40 +8,51 @@ from sqlobject.sqlbuilder import AND, func from .db import Author, Book, Extension, Genre, Language -def _search_with_operator(table, case_sensitive, comparison_op, values): - expressions = [] +def _search_with_operator(table, case_sensitive, comparison_op, values, + expressions): + _expressions = [] if case_sensitive: for column, value in values.items(): - expressions.append( + _expressions.append( getattr(getattr(table.q, column), comparison_op)(value)) + for expr, value in expressions: + _expressions.append( + getattr(expr, comparison_op)(value)) else: for column, value in values.items(): - expressions.append( + _expressions.append( getattr(func.lower( getattr(table.q, column)), comparison_op)(value.lower())) - return AND(*expressions) + for expr, value in expressions: + _expressions.append( + getattr(func.lower(expr), comparison_op)(value.lower())) + return AND(*_expressions) -def _search_exact(table, case_sensitive, values): - return _search_with_operator(table, case_sensitive, '__eq__', values) +def _search_exact(table, case_sensitive, values, expressions): + return _search_with_operator(table, case_sensitive, '__eq__', values, + expressions) -def _search_start(table, case_sensitive, values): - return _search_with_operator(table, case_sensitive, 'startswith', values) +def _search_start(table, case_sensitive, values, expressions): + return _search_with_operator(table, case_sensitive, 'startswith', values, + expressions) -def _search_substring(table, case_sensitive, values): - return _search_with_operator(table, case_sensitive, 'contains', values) +def _search_substring(table, case_sensitive, values, expressions): + return _search_with_operator(table, case_sensitive, 'contains', values, + expressions) -def _search(table, search_type, case_sensitive, values): +def _search(table, search_type, case_sensitive, values, expressions): _search_f = globals()['_search_%s' % search_type] - conditions = _search_f(table, case_sensitive, values) + conditions = _search_f(table, case_sensitive, values, expressions) return table.select(conditions) -def search_authors(search_type, case_sensitive, values): - return _search(Author, search_type, case_sensitive, values) +def search_authors(search_type, case_sensitive, values, + expressions): + return _search(Author, search_type, case_sensitive, values, expressions) def search_books(search_type, case_sensitive, values):