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, search_books
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'Не выбрано книг для сохранения.',
108 @route('/search_books', method='GET')
110 return redirect('/search_books/')
113 @route('/search_books/', method='GET')
114 @cheetah_view('search_books.tmpl')
115 def search_books_get():
119 @route('/search_books/', method='POST')
120 @cheetah_view('list_books.tmpl')
121 def search_books_post():
122 value = request.forms.get('search_books')
124 return redirect('/search_books/')
125 value = decode(value)
126 search_type = request.forms.get('search_type')
128 search_type = 'start'
129 case_sensitive = request.forms.get('case_sensitive')
130 if case_sensitive is None:
131 case_sensitive = _guess_case_sensitivity(value)
132 books = search_books(search_type, case_sensitive, {'title': value}, None,
134 books_by_authors = {}
136 author = book.authors[0].fullname
137 if author in books_by_authors:
138 books_by_author = books_by_authors[author]
140 books_by_author = books_by_authors[author] = []
141 books_by_author.append(book)
143 'books_by_author': books_by_authors,
144 'search_books': value,
145 'search_type': search_type,
146 'case_sensitive': case_sensitive,