]> git.phdru.name Git - m_librarian.git/blob - m_librarian/web/app.py
95af5cb5d79e3060b0d7a923f02dc6c25a8013af
[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, AuthorBook, Book
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     authors = search_authors(search_type, case_sensitive, {}, expressions,
53                              orderBy=('surname', 'name', 'misc_name'))
54     return {
55         'authors': list(authors),
56         'search_authors': value,
57         'search_type': search_type,
58         'case_sensitive': case_sensitive,
59     }
60
61
62 @route('/books-by-author/<id:int>/', method='GET')
63 @cheetah_view('books_by_author.tmpl')
64 def books_by_author(id):
65     return {
66         'author': Author.get(id),
67         'books': Book.select(
68             Book.j.authors & (Author.q.id == id),
69             orderBy=['series', 'ser_no', 'title'],
70         )
71     }