]> git.phdru.name Git - m_librarian.git/blob - m_librarian/web/app.py
Feat(web:books): Поиск по названиям книг
[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
19
20 @route('/search_authors', method='GET')
21 def _search_authors():
22     return redirect('/search_authors/')
23
24
25 @route('/search_authors/', method='GET')
26 @cheetah_view('search_authors.tmpl')
27 def search_authors_get():
28     return {}
29
30
31 def decode(value):
32     if isinstance(value, bytes):
33         return value.decode('utf-8')
34     return value
35
36
37 def _guess_case_sensitivity(value):
38     return not value.islower()
39
40
41 @route('/search_authors/', method='POST')
42 @cheetah_view('list_authors.tmpl')
43 def search_authors_post():
44     value = request.forms.get('search_authors')
45     if not value:
46         return redirect('/search_authors/')
47     value = decode(value)
48     search_type = request.forms.get('search_type')
49     if not search_type:
50         search_type = 'start'
51     case_sensitive = request.forms.get('case_sensitive')
52     if case_sensitive is None:
53         case_sensitive = _guess_case_sensitivity(value)
54     expressions = [(
55         CONCAT(Author.q.surname, ' ', Author.q.name, ' ', Author.q.misc_name),
56         decode(value)
57     )]
58     authors = search_authors(search_type, case_sensitive, {}, expressions,
59                              orderBy=('surname', 'name', 'misc_name'))
60     return {
61         'authors': list(authors),
62         'search_authors': value,
63         'search_type': search_type,
64         'case_sensitive': case_sensitive,
65     }
66
67
68 @route('/books-by-author/<id:int>/', method='GET')
69 @cheetah_view('books_by_author.tmpl')
70 def books_by_author(id):
71     return {
72         'author': Author.get(id),
73         'books': Book.select(
74             Book.j.authors & (Author.q.id == id),
75             orderBy=['series', 'ser_no', 'title'],
76         )
77     }
78
79
80 @route('/static/<filename:path>')
81 def send_static(filename):
82     return static_file(
83         filename, root=os.path.join(
84             os.path.dirname(__file__),
85             'static'
86         )
87     )
88
89
90 @route('/download/', method='POST')
91 @cheetah_view('download.tmpl')
92 def download_books():
93     books_ids = request.forms.getall('books')
94     download_path = get_config().get('download', 'path')
95     if books_ids:
96         for id in books_ids:
97             book = Book.get(int(id))
98             download(book, download_path)
99         return {
100             'message': u'Книги сохранены.',
101         }
102     else:
103         return {
104             'message': u'Не выбрано книг для сохранения.',
105         }
106
107
108 @route('/search_books', method='GET')
109 def _search_books():
110     return redirect('/search_books/')
111
112
113 @route('/search_books/', method='GET')
114 @cheetah_view('search_books.tmpl')
115 def search_books_get():
116     return {}
117
118
119 @route('/search_books/', method='POST')
120 @cheetah_view('list_books.tmpl')
121 def search_books_post():
122     value = request.forms.get('search_books')
123     if not value:
124         return redirect('/search_books/')
125     value = decode(value)
126     search_type = request.forms.get('search_type')
127     if not 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,
133                          orderBy=('title',))
134     return {
135         'books': list(books),
136         'search_books': value,
137         'search_type': search_type,
138         'case_sensitive': case_sensitive,
139     }