]> git.phdru.name Git - m_librarian.git/blob - m_librarian/web/app.py
Feat(web): Catch and report errors on downloading
[m_librarian.git] / m_librarian / web / app.py
1 # -*- coding: utf-8 -*-
2
3 import os
4 from bottle import cheetah_view, redirect, request, route, static_file
5
6 from ..config import get_config
7 from ..db import Book
8 from ..download import download
9 from ..search import search_authors_raw, books_by_author, search_books_raw
10
11
12 @route('/')
13 @cheetah_view('index.tmpl')
14 def index():
15     return {
16         'get_config': get_config,
17     }
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 @route('/search_authors/', method='POST')
32 @cheetah_view('list_authors.tmpl')
33 def search_authors_post():
34     value = request.forms.get('search_authors')
35     if not value:
36         return redirect('/search_authors/')
37     search_type = request.forms.get('search_type')
38     case_sensitive = request.forms.get('case_sensitive')
39     return search_authors_raw(value, search_type, case_sensitive)
40
41
42 @route('/books-by-author/<aid:int>/', method='GET')
43 @cheetah_view('list_books.tmpl')
44 def _books_by_author(aid):
45     return books_by_author(aid)
46
47
48 @route('/static/<filename:path>')
49 def send_static(filename):
50     return static_file(
51         filename, root=os.path.join(
52             os.path.dirname(__file__),
53             'static'
54         )
55     )
56
57
58 @route('/download/', method='POST')
59 @cheetah_view('download.tmpl')
60 def download_books():
61     books_ids = []
62     form = request.forms
63     for k in form:
64         if k.split('_')[-1] == 'books':
65             for bid in form.getall(k):
66                 books_ids.append(bid)
67     if books_ids:
68         download_path = get_config().getpath('download', 'path')
69         try:
70             for bid in books_ids:
71                 book = Book.get(int(bid))
72                 download(book, download_path)
73         except Exception as e:
74             return {
75                 'error': str(e),
76             }
77         else:
78             return {
79                 'message': u'Книги сохранены.',
80             }
81     else:
82         return {
83             'error': u'Не выбрано книг для сохранения.',
84         }
85
86
87 @route('/search_books', method='GET')
88 def _search_books():
89     return redirect('/search_books/')
90
91
92 @route('/search_books/', method='GET')
93 @cheetah_view('search_books.tmpl')
94 def search_books_get():
95     return {
96         'get_config': get_config,
97     }
98
99
100 @route('/search_books/', method='POST')
101 @cheetah_view('list_books.tmpl')
102 def search_books_post():
103     value = request.forms.get('search_books')
104     if not value:
105         return redirect('/search_books/')
106     search_type = request.forms.get('search_type')
107     case_sensitive = request.forms.get('case_sensitive')
108     use_filters = request.forms.get('use_filters')
109     return search_books_raw(value, search_type, case_sensitive, use_filters)