]> git.phdru.name Git - m_librarian.git/commitdiff
Merge branch 'master' into wx
authorOleg Broytman <phd@phdru.name>
Fri, 29 Dec 2023 08:44:38 +0000 (11:44 +0300)
committerOleg Broytman <phd@phdru.name>
Fri, 29 Dec 2023 08:44:38 +0000 (11:44 +0300)
* master:
  Refactor: Separate web forms processing from search
  Perf(web/app): Get `download_path` only if there're books to download
  Style: Re-group and reorder imports

m_librarian/db.py
m_librarian/glst.py
m_librarian/inp.py
m_librarian/search.py
m_librarian/web/app.py
scripts/ml-initdb.py
scripts/ml-search.py
scripts/ml-web.py
tests/dbutils.py

index d91068844148755f9964e59407f8468770cfc6ba..064ca9a0d8a75cd89667feaddfecc9be450ff358 100755 (executable)
@@ -2,9 +2,11 @@
 
 from __future__ import print_function
 import os
+
 from sqlobject import SQLObject, StringCol, UnicodeCol, IntCol, BoolCol, \
     ForeignKey, DateCol, DatabaseIndex, RelatedJoin, \
     connectionForURI, sqlhub, SQLObjectNotFound, dberrors
+
 from .compat import string_type
 from .config import get_config
 
index fedb27b2ed6647fe38b2d8569f7a0483fcad355e..206dcc40f858a306524ef07e1d2ec68b02269cc7 100755 (executable)
@@ -1,9 +1,10 @@
 #! /usr/bin/env python
 
 from __future__ import print_function
-import codecs
 from glob import glob
+import codecs
 import os
+
 from sqlobject import sqlhub, SQLObjectNotFound
 from .db import Genre
 
index 3789dd2e2d986c5969a7688d9651b8cfd2219095..f910b10eefe03e6deb6b66c4e54d9015a33646bc 100644 (file)
@@ -1,8 +1,10 @@
 
 import os
 from zipfile import ZipFile
+
 from sqlobject import sqlhub
 from sqlobject.sqlbuilder import Select
+
 from .db import Author, Book, Extension, Genre, Language, \
     insert_name, insert_author
 
index 82889a8d518e720108d3dc297316a4409c080a69..c190dc8aa3f2bcb858259671b4f0fbf6d847d274 100644 (file)
@@ -1,4 +1,5 @@
-from sqlobject.sqlbuilder import AND, OR, func
+from sqlobject.sqlbuilder import AND, OR, func, CONCAT
+
 from .config import get_config
 from .db import Author, Book, Extension, Genre, Language
 
@@ -109,3 +110,83 @@ def search_genres(search_type, case_sensitive, values, orderBy=None):
 def search_languages(search_type, case_sensitive, values, orderBy=None):
     return _search(Language, search_type, case_sensitive, values,
                    orderBy=orderBy)
+
+
+def decode(value):
+    if isinstance(value, bytes):
+        return value.decode('utf-8')
+    return value
+
+
+def _guess_case_sensitivity(value):
+    return not value.islower()
+
+
+def search_authors_raw(value, search_type, case_sensitive):
+    value = decode(value)
+    if not search_type:
+        search_type = 'start'
+    if case_sensitive is None:
+        case_sensitive = _guess_case_sensitivity(value)
+    expressions = [(
+        CONCAT(Author.q.surname, ' ', Author.q.name, ' ', Author.q.misc_name),
+        decode(value)
+    )]
+    authors = search_authors(search_type, case_sensitive, {}, expressions,
+                             orderBy=('surname', 'name', 'misc_name'))
+    columns = get_config().getlist('columns', 'author', ['fullname'])
+    return {
+        'authors': list(authors),
+        'search_authors': value,
+        'search_type': search_type,
+        'case_sensitive': case_sensitive,
+        'columns': columns,
+    }
+
+
+def books_by_author(aid):
+    use_filters = get_config().getint('filters', 'use_in_books_list', 1)
+    columns = get_config().getlist('columns', 'book', ['title'])
+    author = Author.get(aid)
+    if use_filters:
+        join_expressions = []
+        join_expressions.append(Book.j.authors)
+        join_expressions.append(Author.q.id == aid)
+        books = search_books('full', None, {}, join_expressions,
+                             orderBy=('series', 'ser_no', 'title', '-date'),
+                             use_filters=use_filters)
+    else:
+        books = Book.select(
+            Book.j.authors & (Author.q.id == aid),
+            orderBy=['series', 'ser_no', 'title'],
+        )
+    return {
+        'books_by_author': {author.fullname: list(books)},
+        'columns': columns,
+    }
+
+
+def search_books_raw(value, search_type, case_sensitive, use_filters):
+    value = decode(value)
+    if not search_type:
+        search_type = 'start'
+    if case_sensitive is None:
+        case_sensitive = _guess_case_sensitivity(value)
+    books = search_books(search_type, case_sensitive, {'title': value}, None,
+                         orderBy=('title',), use_filters=use_filters)
+    books_by_authors = {}
+    for book in books:
+        author = book.author1
+        if author in books_by_authors:
+            books_by_author = books_by_authors[author]
+        else:
+            books_by_author = books_by_authors[author] = []
+        books_by_author.append(book)
+    columns = get_config().getlist('columns', 'book', ['title'])
+    return {
+        'books_by_author': books_by_authors,
+        'search_books': value,
+        'search_type': search_type,
+        'case_sensitive': case_sensitive,
+        'columns': columns,
+    }
index a5ea5b2f52004364ef0c23396af9ee4571ed6a35..283ddb715b8b819fb0039c57bf375337eff84a1e 100644 (file)
@@ -1,14 +1,12 @@
 # -*- coding: utf-8 -*-
 
 import os
-
-from sqlobject.sqlbuilder import CONCAT
 from bottle import cheetah_view, redirect, request, route, static_file
 
 from ..config import get_config
-from ..db import Author, Book
+from ..db import Book
 from ..download import download
-from ..search import search_authors, search_books
+from ..search import search_authors_raw, books_by_author, search_books_raw
 
 
 @route('/')
@@ -30,68 +28,21 @@ def search_authors_get():
     return {}
 
 
-def decode(value):
-    if isinstance(value, bytes):
-        return value.decode('utf-8')
-    return value
-
-
-def _guess_case_sensitivity(value):
-    return not value.islower()
-
-
 @route('/search_authors/', method='POST')
 @cheetah_view('list_authors.tmpl')
 def search_authors_post():
     value = request.forms.get('search_authors')
     if not value:
         return redirect('/search_authors/')
-    value = decode(value)
     search_type = request.forms.get('search_type')
-    if not search_type:
-        search_type = 'start'
     case_sensitive = request.forms.get('case_sensitive')
-    if case_sensitive is None:
-        case_sensitive = _guess_case_sensitivity(value)
-    expressions = [(
-        CONCAT(Author.q.surname, ' ', Author.q.name, ' ', Author.q.misc_name),
-        decode(value)
-    )]
-    authors = search_authors(search_type, case_sensitive, {}, expressions,
-                             orderBy=('surname', 'name', 'misc_name'))
-    columns = get_config().getlist('columns', 'author', ['fullname'])
-    return {
-        'authors': list(authors),
-        'search_authors': value,
-        'search_type': search_type,
-        'case_sensitive': case_sensitive,
-        'columns': columns,
-    }
+    return search_authors_raw(value, search_type, case_sensitive)
 
 
 @route('/books-by-author/<aid:int>/', method='GET')
 @cheetah_view('list_books.tmpl')
-def books_by_author(aid):
-    use_filters = get_config().getint('filters', 'use_in_books_list', 1)
-    columns = get_config().getlist('columns', 'book', ['title'])
-    author = Author.get(aid)
-    if use_filters:
-        join_expressions = []
-        join_expressions.append(Book.j.authors)
-        join_expressions.append(Author.q.id == aid)
-        books = search_books('full', None, {}, join_expressions,
-                             orderBy=('series', 'ser_no', 'title', '-date'),
-                             use_filters=use_filters)
-    else:
-        books = Book.select(
-            Book.j.authors & (Author.q.id == aid),
-            orderBy=['series', 'ser_no', 'title'],
-        )
-
-    return {
-        'books_by_author': {author.fullname: list(books)},
-        'columns': columns,
-    }
+def _books_by_author(aid):
+    return books_by_author(aid)
 
 
 @route('/static/<filename:path>')
@@ -113,8 +64,8 @@ def download_books():
         if k.split('_')[-1] == 'books':
             for bid in form.getall(k):
                 books_ids.append(bid)
-    download_path = get_config().getpath('download', 'path')
     if books_ids:
+        download_path = get_config().getpath('download', 'path')
         for bid in books_ids:
             book = Book.get(int(bid))
             download(book, download_path)
@@ -146,29 +97,7 @@ def search_books_post():
     value = request.forms.get('search_books')
     if not value:
         return redirect('/search_books/')
-    value = decode(value)
     search_type = request.forms.get('search_type')
-    if not search_type:
-        search_type = 'start'
     case_sensitive = request.forms.get('case_sensitive')
-    if case_sensitive is None:
-        case_sensitive = _guess_case_sensitivity(value)
     use_filters = request.forms.get('use_filters')
-    books = search_books(search_type, case_sensitive, {'title': value}, None,
-                         orderBy=('title',), use_filters=use_filters)
-    books_by_authors = {}
-    for book in books:
-        author = book.author1
-        if author in books_by_authors:
-            books_by_author = books_by_authors[author]
-        else:
-            books_by_author = books_by_authors[author] = []
-        books_by_author.append(book)
-    columns = get_config().getlist('columns', 'book', ['title'])
-    return {
-        'books_by_author': books_by_authors,
-        'search_books': value,
-        'search_type': search_type,
-        'case_sensitive': case_sensitive,
-        'columns': columns,
-    }
+    return search_books_raw(value, search_type, case_sensitive, use_filters)
index c229c5ea63bba64d1b006f56014f9a0a2b244c6a..4c21b1aaf0b3b0bb83816c754cd18fac54ed5c57 100755 (executable)
@@ -1,6 +1,7 @@
 #! /usr/bin/env python
 
 import argparse
+
 from m_librarian.config import get_config
 from m_librarian.db import open_db, init_db
 from m_librarian.glst import import_glst
index 28daa0b288226ab9f1d34fe4252f0f82104f1805..7e6c9da3b793c02df82ea5d8570635258427a31a 100755 (executable)
@@ -4,6 +4,7 @@ from __future__ import print_function
 import argparse
 import os
 import sys
+
 from sqlobject.sqlbuilder import CONCAT
 
 from m_lib.defenc import default_encoding
index 773be00e1cbefd9f007b090c7582087c0a286861..c93b5fd88c2243a4d923d2a795065592e605b417 100755 (executable)
@@ -7,9 +7,9 @@ import webbrowser
 from bottle import thread  # portable import
 
 from m_librarian.db import open_db
-import m_librarian.web.app  # noqa: F401 imported but unused
 from m_librarian.web.server import run_server
 from m_librarian.web.utils import get_lock, close_lock, get_open_port
+import m_librarian.web.app  # noqa: F401 imported but unused
 
 
 def start_browser(port):
index bb5a362a18e0b576065b76bfd80c3a247945b64e..06a9e9c6ab6afc0f3b4df51f85f67741a400e3bf 100644 (file)
@@ -1,6 +1,8 @@
 
 import os
+
 from sqlobject.tests.dbtest import getConnection
+
 from m_librarian.db import open_db, init_db
 from m_librarian.inp import import_inpx