]> git.phdru.name Git - m_librarian.git/commitdiff
Search with expressions
authorOleg Broytman <phd@phdru.name>
Mon, 9 May 2016 21:06:35 +0000 (00:06 +0300)
committerOleg Broytman <phd@phdru.name>
Mon, 9 May 2016 21:06:35 +0000 (00:06 +0300)
m_librarian/search.py

index a310757fba4069d659d0eb402db1e5d8222a3b32..f1d71b71a9e9bf90deb060af373f22ff742267f6 100644 (file)
@@ -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):