]> git.phdru.name Git - m_librarian.git/commitdiff
Refactor search implementation to avoid code duplication
authorOleg Broytman <phd@phdru.name>
Tue, 29 Mar 2016 18:15:27 +0000 (21:15 +0300)
committerOleg Broytman <phd@phdru.name>
Tue, 29 Mar 2016 18:15:27 +0000 (21:15 +0300)
m_librarian/search.py

index 10cfc2880b629f1e1da081cd1cbfff26612a93da..a310757fba4069d659d0eb402db1e5d8222a3b32 100644 (file)
@@ -8,40 +8,30 @@ from sqlobject.sqlbuilder import AND, func
 from .db import Author, Book, Extension, Genre, Language
 
 
-def _search_exact(table, case_sensitive, values):
+def _search_with_operator(table, case_sensitive, comparison_op, values):
     expressions = []
     if case_sensitive:
         for column, value in values.items():
-            expressions.append(getattr(table.q, column) == value)
+            expressions.append(
+                getattr(getattr(table.q, column), comparison_op)(value))
     else:
         for column, value in values.items():
             expressions.append(
-                func.lower(getattr(table.q, column)) == value.lower())
+                getattr(func.lower(
+                    getattr(table.q, column)), 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_start(table, case_sensitive, values):
-    expressions = []
-    if case_sensitive:
-        for column, value in values.items():
-            expressions.append(getattr(table.q, column).startswith(value))
-    else:
-        for column, value in values.items():
-            expressions.append(
-                func.lower(getattr(table.q, column)).startswith(value.lower()))
-    return AND(*expressions)
+    return _search_with_operator(table, case_sensitive, 'startswith', values)
 
 
 def _search_substring(table, case_sensitive, values):
-    expressions = []
-    if case_sensitive:
-        for column, value in values.items():
-            expressions.append(getattr(table.q, column).contains(value))
-    else:
-        for column, value in values.items():
-            expressions.append(
-                func.lower(getattr(table.q, column)).contains(value.lower()))
-    return AND(*expressions)
+    return _search_with_operator(table, case_sensitive, 'contains', values)
 
 
 def _search(table, search_type, case_sensitive, values):