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