]> git.phdru.name Git - m_librarian.git/blob - m_librarian/web/app.py
Feat(web): Search authors
[m_librarian.git] / m_librarian / web / app.py
1 from sqlobject.sqlbuilder import CONCAT
2 from bottle import cheetah_view, redirect, request, route
3
4 from m_librarian.db import Author, open_db
5 from m_librarian.search import search_authors
6
7
8 @route('/')
9 @cheetah_view('index.tmpl')
10 def index():
11     return {}
12
13
14 @route('/search_authors', method='GET')
15 def _search_authors():
16     return redirect('/search_authors/')
17
18
19 @route('/search_authors/', method='GET')
20 @cheetah_view('search_authors.tmpl')
21 def search_authors_get():
22     return {}
23
24
25 def decode(value):
26     if isinstance(value, bytes):
27         return value.decode('utf-8')
28     return value
29
30
31 def _guess_case_sensitivity(value):
32     return not value.islower()
33
34
35 @route('/search_authors/', method='POST')
36 @cheetah_view('list_authors.tmpl')
37 def search_authors_post():
38     value = request.forms.get('search_authors')
39     if not value:
40         return redirect('/search_authors/')
41     value = decode(value)
42     search_type = request.forms.get('search_type')
43     if not search_type:
44         search_type = 'start'
45     case_sensitive = request.forms.get('case_sensitive')
46     if case_sensitive is None:
47         case_sensitive = _guess_case_sensitivity(value)
48     expressions = [(
49         CONCAT(Author.q.surname, ' ', Author.q.name, ' ', Author.q.misc_name),
50         decode(value)
51     )]
52     open_db()
53     authors = search_authors(search_type, case_sensitive, {}, expressions,
54                              orderBy=('surname', 'name', 'misc_name'))
55     return {'authors': list(authors)}