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')
18 'get_config': get_config,
22 @route('/search_authors', method='GET')
23 def _search_authors():
24 return redirect('/search_authors/')
27 @route('/search_authors/', method='GET')
28 @cheetah_view('search_authors.tmpl')
29 def search_authors_get():
34 if isinstance(value, bytes):
35 return value.decode('utf-8')
39 def _guess_case_sensitivity(value):
40 return not value.islower()
43 @route('/search_authors/', method='POST')
44 @cheetah_view('list_authors.tmpl')
45 def search_authors_post():
46 value = request.forms.get('search_authors')
48 return redirect('/search_authors/')
50 search_type = request.forms.get('search_type')
53 case_sensitive = request.forms.get('case_sensitive')
54 if case_sensitive is None:
55 case_sensitive = _guess_case_sensitivity(value)
57 CONCAT(Author.q.surname, ' ', Author.q.name, ' ', Author.q.misc_name),
60 authors = search_authors(search_type, case_sensitive, {}, expressions,
61 orderBy=('surname', 'name', 'misc_name'))
63 'authors': list(authors),
64 'search_authors': value,
65 'search_type': search_type,
66 'case_sensitive': case_sensitive,
70 @route('/books-by-author/<id:int>/', method='GET')
71 @cheetah_view('books_by_author.tmpl')
72 def books_by_author(id):
73 use_filters = get_config().getint('filters', 'use_in_books_list', 1)
76 join_expressions.append(Book.j.authors)
77 join_expressions.append(Author.q.id == id)
78 books = search_books('full', None, {}, join_expressions,
79 orderBy=('series', 'ser_no', 'title'),
80 use_filters=use_filters)
82 'author': Author.get(id),
87 'author': Author.get(id),
89 Book.j.authors & (Author.q.id == id),
90 orderBy=['series', 'ser_no', 'title'],
95 @route('/static/<filename:path>')
96 def send_static(filename):
98 filename, root=os.path.join(
99 os.path.dirname(__file__),
105 @route('/download/', method='POST')
106 @cheetah_view('download.tmpl')
107 def download_books():
108 books_ids = request.forms.getall('books')
109 download_path = get_config().get('download', 'path') or '.'
112 book = Book.get(int(id))
113 download(book, download_path)
115 'message': u'Книги сохранены.',
119 'message': u'Не выбрано книг для сохранения.',
123 @route('/search_books', method='GET')
125 return redirect('/search_books/')
128 @route('/search_books/', method='GET')
129 @cheetah_view('search_books.tmpl')
130 def search_books_get():
132 'get_config': get_config,
136 @route('/search_books/', method='POST')
137 @cheetah_view('list_books.tmpl')
138 def search_books_post():
139 value = request.forms.get('search_books')
141 return redirect('/search_books/')
142 value = decode(value)
143 search_type = request.forms.get('search_type')
145 search_type = 'start'
146 case_sensitive = request.forms.get('case_sensitive')
147 if case_sensitive is None:
148 case_sensitive = _guess_case_sensitivity(value)
149 use_filters = request.forms.get('use_filters')
150 books = search_books(search_type, case_sensitive, {'title': value}, None,
151 orderBy=('title',), use_filters=use_filters)
152 books_by_authors = {}
154 author = book.authors[0].fullname
155 if author in books_by_authors:
156 books_by_author = books_by_authors[author]
158 books_by_author = books_by_authors[author] = []
159 books_by_author.append(book)
161 'books_by_author': books_by_authors,
162 'search_books': value,
163 'search_type': search_type,
164 'case_sensitive': case_sensitive,