From a85145dc47b87062148a2599180e229ab7c7af2b Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Tue, 10 May 2016 00:06:35 +0300 Subject: [PATCH] Search with expressions --- m_librarian/search.py | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) 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): -- 2.39.5