Beispiel #1
0
def books_by_author(id):
    use_filters = get_config().getint('filters', 'use_in_books_list', 1)
    columns = get_config().getlist('columns', 'book', ['title'])
    author = Author.get(id)
    if use_filters:
        join_expressions = []
        join_expressions.append(Book.j.authors)
        join_expressions.append(Author.q.id == id)
        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 == id),
            orderBy=['series', 'ser_no', 'title'],
        )

    return {
        'books_by_author': {
            author.fullname: list(books)
        },
        'columns': columns,
    }
Beispiel #2
0
def test_compile_format():
    config_path = os.path.join(
        os.path.dirname(__file__), 'test_config.conf')
    get_config(config_path)
    ml_conf = get_config()
    ml_conf.set('download', 'format', '%a/%s/%n %t')
    download._compile_format()
    assert download.compiled_format == \
        u'%(author1)s/%(series)s/%(ser_no)d %(title)s'
Beispiel #3
0
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,
    }
Beispiel #4
0
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,
    }
Beispiel #5
0
def download_books():
    books_ids = request.forms.getall('books')
    download_path = get_config().getpath('download', 'path')
    if books_ids:
        for id in books_ids:
            book = Book.get(int(id))
            download(book, download_path)
        return {
            'message': u'Книги сохранены.',
        }
    else:
        return {
            'message': u'Не выбрано книг для сохранения.',
        }
Beispiel #6
0
#! /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

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Init')
    parser.add_argument('-C', '--config', help='configuration file')
    parser.add_argument('-D', '--database', help='database URI')
    args = parser.parse_args()

    if args.config:
        get_config(args.config)  # Get and cache config file

    open_db(args.database)
    init_db()
    count_old, count_new = import_glst()
    if count_old:
        print("Imported %d genres (ignored %d existing)" %
              (count_new, count_old))
    else:
        print("Imported %d genres" % count_new)
Beispiel #7
0
def _search_books(case_sensitive, search_type, args):
    if args.get and args.get_many:
        sys.stderr.write(
            "Cannot get one book and many books at the same time\n")
        main_parser.print_help()
        sys.exit(1)
    join_expressions = []
    values = _get_values(args, 'title', 'series', 'archive', 'file', 'id')
    if case_sensitive is None:
        test_values = values.copy()
        test_values.update(
            _get_values(args, 'surname', 'name', 'misc_name', 'fullname',
                        'ext', 'gname', 'gtitle', 'lang'))
        case_sensitive = _guess_case_sensitivity(test_values)
    avalues = _get_values(args, 'surname', 'name', 'misc_name', 'fullname')
    if args.aid:
        avalues['id'] = args.aid
    if avalues:
        if (args.surname or args.name or args.misc_name) and args.fullname:
            sys.stderr.write(
                "Cannot search by names and full name at the same time\n")
            main_parser.print_help()
            sys.exit(1)
        expressions = []
        join_expressions.append(Book.j.authors)
        value = args.fullname
        if value:
            expressions.append(
                (CONCAT(Author.q.surname, ' ', Author.q.name, ' ',
                        Author.q.misc_name), decode(value)))
        conditions = mk_search_conditions(Author, search_type, case_sensitive,
                                          avalues, expressions)
        join_expressions.extend(conditions)
    evalues = {}
    if args.ext:
        evalues['name'] = args.ext
    if args.eid:
        evalues['id'] = args.eid
    if evalues:
        join_expressions.append(Book.j.extension)
        conditions = mk_search_conditions(Extension, search_type,
                                          case_sensitive, evalues)
        join_expressions.extend(conditions)
    gvalues = {}
    for column in 'name', 'title':
        value = getattr(args, 'g' + column)
        if value:
            gvalues[column] = decode(value)
    if args.gid:
        gvalues['id'] = args.gid
    if gvalues:
        join_expressions.append(Book.j.genres)
        conditions = mk_search_conditions(Genre, search_type, case_sensitive,
                                          gvalues)
        join_expressions.extend(conditions)
    lvalues = {}
    if args.lang:
        lvalues['name'] = args.lang
    if args.lid:
        lvalues['id'] = args.lid
    if lvalues:
        join_expressions.append(Book.j.language)
        conditions = mk_search_conditions(Language, search_type,
                                          case_sensitive, lvalues)
        join_expressions.extend(conditions)
    books = search_books(search_type,
                         case_sensitive,
                         values,
                         join_expressions,
                         orderBy=('series', 'ser_no', 'title', '-date'),
                         use_filters=args.filters)
    if args.count:
        print_count(books.count())
        return
    if args.get_many:
        books = books[:args.get_many]
    elif args.get:
        count = books.count()
        if count != 1:
            sys.stderr.write("There must be exactly 1 book for --get; "
                             "(found %d).\n" % count)
            sys.stderr.write("Use --get-many N to download more than one "
                             "book.\n")
            sys.exit(1)
    count = 0
    for book in books:
        print(encode(book.title), end=' ')
        if args.verbose >= 1:
            print("(id=%d)" % book.id, end=' ')
        print()
        if args.verbose >= 1:
            print(" ", encode(_("Author(s)")), ":", end=' ')
            for author in book.authors:
                print(encode(author.fullname), end=' ')
            print()
            print(" ", encode(_("Genre(s)")), ":", end=' ')
            for genre in book.genres:
                print((encode(genre.title or genre.name)), end=' ')
            print()
            if book.series:
                print(" ", encode(_("Series")), ":", end=' ')
                print(encode(book.series), "(%d)" % book.ser_no)

        if args.verbose >= 2:
            print(" ", encode(_("Date")), ":", book.date)
            print(" ", encode(_("Language")), ":", encode(book.language.name))

        if args.verbose >= 3:
            print(" ", encode(_("Archive")), ":", book.archive)
            print(" ", encode(_("File")), ":", book.file)
            print(" ", encode(_("Extension")), ":",
                  encode(book.extension.name))
            print(" ", encode(_("Size")), ":", book.size, encode(_("bytes")))
            print(" ", encode(_("Deleted")), ":", encode(_(str(book.deleted))))
        if args.get or args.get_many:
            download_to = args.download_to
            if download_to is None:
                download_to = get_config().getpath('download', 'path')
            download(book, download_to, args.path, args.format)
        count += 1
    print_count(count)
Beispiel #8
0
def test_config():
    config_path = os.path.join(os.path.dirname(__file__), 'test_config.conf')
    get_config(config_path)
    ml_conf = get_config()
    assert ml_conf.get('library', 'path') == '/home/test-config'