1 # -*- coding: utf-8 -*-
5 from sqlobject.sqlbuilder import CONCAT
6 from bottle import cheetah_view, redirect, request, route, static_file
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
15 @cheetah_view('index.tmpl')
20 @route('/search_authors', method='GET')
21 def _search_authors():
22 return redirect('/search_authors/')
25 @route('/search_authors/', method='GET')
26 @cheetah_view('search_authors.tmpl')
27 def search_authors_get():
32 if isinstance(value, bytes):
33 return value.decode('utf-8')
37 def _guess_case_sensitivity(value):
38 return not value.islower()
41 @route('/search_authors/', method='POST')
42 @cheetah_view('list_authors.tmpl')
43 def search_authors_post():
44 value = request.forms.get('search_authors')
46 return redirect('/search_authors/')
48 search_type = request.forms.get('search_type')
51 case_sensitive = request.forms.get('case_sensitive')
52 if case_sensitive is None:
53 case_sensitive = _guess_case_sensitivity(value)
55 CONCAT(Author.q.surname, ' ', Author.q.name, ' ', Author.q.misc_name),
58 authors = search_authors(search_type, case_sensitive, {}, expressions,
59 orderBy=('surname', 'name', 'misc_name'))
61 'authors': list(authors),
62 'search_authors': value,
63 'search_type': search_type,
64 'case_sensitive': case_sensitive,
68 @route('/books-by-author/<id:int>/', method='GET')
69 @cheetah_view('books_by_author.tmpl')
70 def books_by_author(id):
72 'author': Author.get(id),
74 Book.j.authors & (Author.q.id == id),
75 orderBy=['series', 'ser_no', 'title'],
80 @route('/static/<filename:path>')
81 def send_static(filename):
83 filename, root=os.path.join(
84 os.path.dirname(__file__),
90 @route('/download/', method='POST')
91 @cheetah_view('download.tmpl')
93 books_ids = request.forms.getall('books')
94 download_path = get_config().get('download', 'path')
97 book = Book.get(int(id))
98 download(book, download_path)
100 'message': u'Книги сохранены.',
104 'message': u'Не выбрано книг для сохранения.',