]> git.phdru.name Git - m_librarian.git/blob - m_librarian/search.py
Docs: Update TODO
[m_librarian.git] / m_librarian / search.py
1 from sqlobject.sqlbuilder import AND, OR, func, CONCAT
2
3 from .config import get_config
4 from .db import Author, Book, Extension, Genre, Language
5
6 __all__ = [
7     'mk_search_conditions',
8     'search_authors', 'search_books', 'search_extensions',
9     'search_genres', 'search_languages',
10 ]
11
12
13 def _mk_search_conditions_with_operator(table, case_sensitive, comparison_op,
14                                         values, expressions):
15     if expressions is None:
16         expressions = []
17     _expressions = []
18     for column, value in values.items():
19         if column == 'id':
20             _expressions.append(table.q.id == value)
21             break
22     if case_sensitive:
23         for column, value in values.items():
24             if column == 'id':
25                 continue
26             _expressions.append(
27                 getattr(getattr(table.q, column), comparison_op)(value))
28         for expr, value in expressions:
29             _expressions.append(
30                 getattr(expr, comparison_op)(value))
31     else:
32         for column, value in values.items():
33             if column == 'id':
34                 continue
35             _expressions.append(
36                 getattr(func.lower(
37                     getattr(table.q, column)),
38                     comparison_op)(value.lower()))
39         for expr, value in expressions:
40             _expressions.append(
41                 getattr(func.lower(expr), comparison_op)(value.lower()))
42     return _expressions
43
44
45 _comparison_operators = {
46     'start': 'startswith',
47     'substring': 'contains',
48     'full': '__eq__',
49 }
50
51
52 def mk_search_conditions(table, search_type, case_sensitive, values,
53                          expressions=None, join_expressions=None):
54     if join_expressions is None:
55         join_expressions = []
56     return _mk_search_conditions_with_operator(
57         table, case_sensitive, _comparison_operators[search_type],
58         values, expressions) + join_expressions
59
60
61 def _search(table, search_type, case_sensitive, values,
62             expressions=None, join_expressions=None, orderBy=None):
63     conditions = mk_search_conditions(
64         table, search_type, case_sensitive, values, expressions=expressions,
65         join_expressions=join_expressions)
66     return table.select(AND(*conditions), orderBy=orderBy)
67
68
69 def search_authors(search_type, case_sensitive, values,
70                    expressions=None, orderBy=None):
71     return _search(Author, search_type, case_sensitive, values,
72                    expressions=expressions, orderBy=orderBy)
73
74
75 def search_books(search_type, case_sensitive, values, join_expressions=None,
76                  orderBy=None, use_filters=False):
77     if use_filters:
78         config = get_config()
79         lang_filter = config.getlist('filters', 'lang')
80         deleted_filter = config.getint('filters', 'deleted')
81         if lang_filter:
82             if join_expressions is None:
83                 join_expressions = []
84             lang_conditions = []
85             for lang in lang_filter:
86                 lvalues = {'name': lang}
87                 conditions = mk_search_conditions(
88                     Language, search_type, case_sensitive, lvalues)
89                 lang_conditions.append(conditions)
90             join_expressions.append(Book.j.language)
91             join_expressions.append(OR(*lang_conditions))
92     conditions = mk_search_conditions(
93         Book, search_type, case_sensitive, values,
94         join_expressions=join_expressions)
95     if use_filters and not deleted_filter:
96         conditions.append(Book.q.deleted == False)  # noqa: E712
97     return Book.select(AND(*conditions), orderBy=orderBy)
98
99
100 def search_extensions(search_type, case_sensitive, values, orderBy=None):
101     return _search(Extension, search_type, case_sensitive, values,
102                    orderBy=orderBy)
103
104
105 def search_genres(search_type, case_sensitive, values, orderBy=None):
106     return _search(Genre, search_type, case_sensitive, values,
107                    orderBy=orderBy)
108
109
110 def search_languages(search_type, case_sensitive, values, orderBy=None):
111     return _search(Language, search_type, case_sensitive, values,
112                    orderBy=orderBy)
113
114
115 def decode(value):
116     if isinstance(value, bytes):
117         return value.decode('utf-8')
118     return value
119
120
121 def _guess_case_sensitivity(value):
122     return not value.islower()
123
124
125 def search_authors_raw(value, search_type, case_sensitive):
126     value = decode(value)
127     if not search_type:
128         search_type = 'start'
129     if case_sensitive is None:
130         case_sensitive = _guess_case_sensitivity(value)
131     expressions = [(
132         CONCAT(Author.q.surname, ' ', Author.q.name, ' ', Author.q.misc_name),
133         decode(value)
134     )]
135     authors = search_authors(search_type, case_sensitive, {}, expressions,
136                              orderBy=('surname', 'name', 'misc_name'))
137     columns = get_config().getlist('columns', 'author', ['fullname'])
138     return {
139         'authors': list(authors),
140         'search_authors': value,
141         'search_type': search_type,
142         'case_sensitive': case_sensitive,
143         'columns': columns,
144     }
145
146
147 def books_by_author(aid):
148     use_filters = get_config().getint('filters', 'use_in_books_list', 1)
149     columns = get_config().getlist('columns', 'book', ['title'])
150     author = Author.get(aid)
151     if use_filters:
152         join_expressions = []
153         join_expressions.append(Book.j.authors)
154         join_expressions.append(Author.q.id == aid)
155         books = search_books('full', None, {}, join_expressions,
156                              orderBy=('series', 'ser_no', 'title', '-date'),
157                              use_filters=use_filters)
158     else:
159         books = Book.select(
160             Book.j.authors & (Author.q.id == aid),
161             orderBy=['series', 'ser_no', 'title'],
162         )
163     return {
164         'books_by_author': {author.fullname: list(books)},
165         'columns': columns,
166     }
167
168
169 def search_books_raw(value, search_type, case_sensitive, use_filters):
170     value = decode(value)
171     if not search_type:
172         search_type = 'start'
173     if case_sensitive is None:
174         case_sensitive = _guess_case_sensitivity(value)
175     books = search_books(search_type, case_sensitive, {'title': value}, None,
176                          orderBy=('title',), use_filters=use_filters)
177     books_by_authors = {}
178     for book in books:
179         author = book.author1
180         if author in books_by_authors:
181             books_by_author = books_by_authors[author]
182         else:
183             books_by_author = books_by_authors[author] = []
184         books_by_author.append(book)
185     columns = get_config().getlist('columns', 'book', ['title'])
186     return {
187         'books_by_author': books_by_authors,
188         'search_books': value,
189         'search_type': search_type,
190         'case_sensitive': case_sensitive,
191         'columns': columns,
192     }