]> git.phdru.name Git - m_librarian.git/blob - m_librarian/web/app.py
38e5beacf2d0c9c72a5492fff7addaef5b881ea6
[m_librarian.git] / m_librarian / web / app.py
1 # -*- coding: utf-8 -*-
2
3 import os
4
5 from bottle import cheetah_view, redirect, request, route, static_file
6 from sqlobject.sqlbuilder import CONCAT
7
8 from ..config import get_config
9 from ..db import Author, Book
10 from ..download import download
11 from ..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/<aid:int>/', method='GET')
73 @cheetah_view('list_books.tmpl')
74 def books_by_author(aid):
75     use_filters = get_config().getint('filters', 'use_in_books_list', 1)
76     columns = get_config().getlist('columns', 'book', ['title'])
77     author = Author.get(aid)
78     if use_filters:
79         join_expressions = []
80         join_expressions.append(Book.j.authors)
81         join_expressions.append(Author.q.id == aid)
82         books = search_books('full', None, {}, join_expressions,
83                              orderBy=('series', 'ser_no', 'title', '-date'),
84                              use_filters=use_filters)
85     else:
86         books = Book.select(
87             Book.j.authors & (Author.q.id == aid),
88             orderBy=['series', 'ser_no', 'title'],
89         )
90
91     return {
92         'books_by_author': {author.fullname: list(books)},
93         'columns': columns,
94     }
95
96
97 @route('/static/<filename:path>')
98 def send_static(filename):
99     return static_file(
100         filename, root=os.path.join(
101             os.path.dirname(__file__),
102             'static'
103         )
104     )
105
106
107 @route('/download/', method='POST')
108 @cheetah_view('download.tmpl')
109 def download_books():
110     books_ids = []
111     form = request.forms
112     for k in form:
113         if k.split('_')[-1] == 'books':
114             for bid in form.getall(k):
115                 books_ids.append(bid)
116     download_path = get_config().getpath('download', 'path')
117     if books_ids:
118         for bid in books_ids:
119             book = Book.get(int(bid))
120             download(book, download_path)
121         return {
122             'message': u'Книги сохранены.',
123         }
124     else:
125         return {
126             'message': u'Не выбрано книг для сохранения.',
127         }
128
129
130 @route('/search_books', method='GET')
131 def _search_books():
132     return redirect('/search_books/')
133
134
135 @route('/search_books/', method='GET')
136 @cheetah_view('search_books.tmpl')
137 def search_books_get():
138     return {
139         'get_config': get_config,
140     }
141
142
143 @route('/search_books/', method='POST')
144 @cheetah_view('list_books.tmpl')
145 def search_books_post():
146     value = request.forms.get('search_books')
147     if not value:
148         return redirect('/search_books/')
149     value = decode(value)
150     search_type = request.forms.get('search_type')
151     if not search_type:
152         search_type = 'start'
153     case_sensitive = request.forms.get('case_sensitive')
154     if case_sensitive is None:
155         case_sensitive = _guess_case_sensitivity(value)
156     use_filters = request.forms.get('use_filters')
157     books = search_books(search_type, case_sensitive, {'title': value}, None,
158                          orderBy=('title',), use_filters=use_filters)
159     books_by_authors = {}
160     for book in books:
161         author = book.author1
162         if author in books_by_authors:
163             books_by_author = books_by_authors[author]
164         else:
165             books_by_author = books_by_authors[author] = []
166         books_by_author.append(book)
167     columns = get_config().getlist('columns', 'book', ['title'])
168     return {
169         'books_by_author': books_by_authors,
170         'search_books': value,
171         'search_type': search_type,
172         'case_sensitive': case_sensitive,
173         'columns': columns,
174     }