]> git.phdru.name Git - m_librarian.git/blob - m_librarian/web/app.py
Docs: Update TODO
[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         try:
69             for bid in books_ids:
70                 book = Book.get(int(bid))
71                 download(book)
72         except Exception as e:
73             return {
74                 'error': str(e),
75             }
76         else:
77             return {
78                 'message': u'Книги сохранены.',
79             }
80     else:
81         return {
82             'error': u'Не выбрано книг для сохранения.',
83         }
84
85
86 @route('/search_books', method='GET')
87 def _search_books():
88     return redirect('/search_books/')
89
90
91 @route('/search_books/', method='GET')
92 @cheetah_view('search_books.tmpl')
93 def search_books_get():
94     return {
95         'get_config': get_config,
96     }
97
98
99 @route('/search_books/', method='POST')
100 @cheetah_view('list_books.tmpl')
101 def search_books_post():
102     value = request.forms.get('search_books')
103     if not value:
104         return redirect('/search_books/')
105     search_type = request.forms.get('search_type')
106     case_sensitive = request.forms.get('case_sensitive')
107     use_filters = request.forms.get('use_filters')
108     return search_books_raw(value, search_type, case_sensitive, use_filters)