]> git.phdru.name Git - m_librarian.git/blob - m_librarian/web/app.py
Feat(web): Add style.css
[m_librarian.git] / m_librarian / web / app.py
1 import os
2
3 from sqlobject.sqlbuilder import CONCAT
4 from bottle import cheetah_view, redirect, request, route, static_file
5
6 from m_librarian.db import Author, Book
7 from m_librarian.search import search_authors
8
9
10 @route('/')
11 @cheetah_view('index.tmpl')
12 def index():
13     return {}
14
15
16 @route('/search_authors', method='GET')
17 def _search_authors():
18     return redirect('/search_authors/')
19
20
21 @route('/search_authors/', method='GET')
22 @cheetah_view('search_authors.tmpl')
23 def search_authors_get():
24     return {}
25
26
27 def decode(value):
28     if isinstance(value, bytes):
29         return value.decode('utf-8')
30     return value
31
32
33 def _guess_case_sensitivity(value):
34     return not value.islower()
35
36
37 @route('/search_authors/', method='POST')
38 @cheetah_view('list_authors.tmpl')
39 def search_authors_post():
40     value = request.forms.get('search_authors')
41     if not value:
42         return redirect('/search_authors/')
43     value = decode(value)
44     search_type = request.forms.get('search_type')
45     if not search_type:
46         search_type = 'start'
47     case_sensitive = request.forms.get('case_sensitive')
48     if case_sensitive is None:
49         case_sensitive = _guess_case_sensitivity(value)
50     expressions = [(
51         CONCAT(Author.q.surname, ' ', Author.q.name, ' ', Author.q.misc_name),
52         decode(value)
53     )]
54     authors = search_authors(search_type, case_sensitive, {}, expressions,
55                              orderBy=('surname', 'name', 'misc_name'))
56     return {
57         'authors': list(authors),
58         'search_authors': value,
59         'search_type': search_type,
60         'case_sensitive': case_sensitive,
61     }
62
63
64 @route('/books-by-author/<id:int>/', method='GET')
65 @cheetah_view('books_by_author.tmpl')
66 def books_by_author(id):
67     return {
68         'author': Author.get(id),
69         'books': Book.select(
70             Book.j.authors & (Author.q.id == id),
71             orderBy=['series', 'ser_no', 'title'],
72         )
73     }
74
75
76 @route('/static/<filename:path>')
77 def send_static(filename):
78     return static_file(
79         filename, root=os.path.join(
80             os.path.dirname(__file__),
81             'static'
82         )
83     )