]> git.phdru.name Git - m_librarian.git/blob - m_librarian/web/app.py
Feat(web): Configure columns for author and book tables
[m_librarian.git] / m_librarian / web / app.py
1 # -*- coding: utf-8 -*-
2
3 import os
4
5 from sqlobject.sqlbuilder import CONCAT
6 from bottle import cheetah_view, redirect, request, route, static_file
7
8 from m_librarian.config import get_config
9 from m_librarian.db import Author, Book
10 from m_librarian.download import download
11 from m_librarian.search import search_authors, search_books
12
13
14 @route('/')
15 @cheetah_view('index.tmpl')
16 def index():
17     return {
18         'get_config': get_config,
19     }
20
21
22 @route('/search_authors', method='GET')
23 def _search_authors():
24     return redirect('/search_authors/')
25
26
27 @route('/search_authors/', method='GET')
28 @cheetah_view('search_authors.tmpl')
29 def search_authors_get():
30     return {}
31
32
33 def decode(value):
34     if isinstance(value, bytes):
35         return value.decode('utf-8')
36     return value
37
38
39 def _guess_case_sensitivity(value):
40     return not value.islower()
41
42
43 @route('/search_authors/', method='POST')
44 @cheetah_view('list_authors.tmpl')
45 def search_authors_post():
46     value = request.forms.get('search_authors')
47     if not value:
48         return redirect('/search_authors/')
49     value = decode(value)
50     search_type = request.forms.get('search_type')
51     if not search_type:
52         search_type = 'start'
53     case_sensitive = request.forms.get('case_sensitive')
54     if case_sensitive is None:
55         case_sensitive = _guess_case_sensitivity(value)
56     expressions = [(
57         CONCAT(Author.q.surname, ' ', Author.q.name, ' ', Author.q.misc_name),
58         decode(value)
59     )]
60     authors = search_authors(search_type, case_sensitive, {}, expressions,
61                              orderBy=('surname', 'name', 'misc_name'))
62     columns = get_config().getlist('columns', 'author', ['fullname'])
63     return {
64         'authors': list(authors),
65         'search_authors': value,
66         'search_type': search_type,
67         'case_sensitive': case_sensitive,
68         'columns': columns,
69     }
70
71
72 @route('/books-by-author/<id:int>/', method='GET')
73 @cheetah_view('books_by_author.tmpl')
74 def books_by_author(id):
75     use_filters = get_config().getint('filters', 'use_in_books_list', 1)
76     columns = get_config().getlist('columns', 'book', ['title'])
77     if use_filters:
78         join_expressions = []
79         join_expressions.append(Book.j.authors)
80         join_expressions.append(Author.q.id == id)
81         books = search_books('full', None, {}, join_expressions,
82                              orderBy=('series', 'ser_no', 'title'),
83                              use_filters=use_filters)
84         return {
85             'author': Author.get(id),
86             'books': books,
87             'columns': columns,
88         }
89     else:
90         return {
91             'author': Author.get(id),
92             'books': Book.select(
93                 Book.j.authors & (Author.q.id == id),
94                 orderBy=['series', 'ser_no', 'title'],
95             ),
96             'columns': columns,
97         }
98
99
100 @route('/static/<filename:path>')
101 def send_static(filename):
102     return static_file(
103         filename, root=os.path.join(
104             os.path.dirname(__file__),
105             'static'
106         )
107     )
108
109
110 @route('/download/', method='POST')
111 @cheetah_view('download.tmpl')
112 def download_books():
113     books_ids = request.forms.getall('books')
114     download_path = get_config().getpath('download', 'path')
115     if books_ids:
116         for id in books_ids:
117             book = Book.get(int(id))
118             download(book, download_path)
119         return {
120             'message': u'Книги сохранены.',
121         }
122     else:
123         return {
124             'message': u'Не выбрано книг для сохранения.',
125         }
126
127
128 @route('/search_books', method='GET')
129 def _search_books():
130     return redirect('/search_books/')
131
132
133 @route('/search_books/', method='GET')
134 @cheetah_view('search_books.tmpl')
135 def search_books_get():
136     return {
137         'get_config': get_config,
138     }
139
140
141 @route('/search_books/', method='POST')
142 @cheetah_view('list_books.tmpl')
143 def search_books_post():
144     value = request.forms.get('search_books')
145     if not value:
146         return redirect('/search_books/')
147     value = decode(value)
148     search_type = request.forms.get('search_type')
149     if not search_type:
150         search_type = 'start'
151     case_sensitive = request.forms.get('case_sensitive')
152     if case_sensitive is None:
153         case_sensitive = _guess_case_sensitivity(value)
154     use_filters = request.forms.get('use_filters')
155     books = search_books(search_type, case_sensitive, {'title': value}, None,
156                          orderBy=('title',), use_filters=use_filters)
157     books_by_authors = {}
158     for book in books:
159         author = book.authors[0].fullname
160         if author in books_by_authors:
161             books_by_author = books_by_authors[author]
162         else:
163             books_by_author = books_by_authors[author] = []
164         books_by_author.append(book)
165     columns = get_config().getlist('columns', 'book', ['title'])
166     return {
167         'books_by_author': books_by_authors,
168         'search_books': value,
169         'search_type': search_type,
170         'case_sensitive': case_sensitive,
171         'columns': columns,
172     }