From a10f5b70948be1bd082fde61ac746c8ca7ec20fa Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Fri, 13 May 2016 22:10:08 +0300 Subject: [PATCH] Search genres by name and title --- docs-ru/command_line.rst | 14 ++++++++++++++ docs/command_line.rst | 14 ++++++++++++++ m_librarian/db.py | 2 +- scripts/ml-search.py | 24 +++++++++++++++++++++++- 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/docs-ru/command_line.rst b/docs-ru/command_line.rst index 140e330..6fc5f87 100644 --- a/docs-ru/command_line.rst +++ b/docs-ru/command_line.rst @@ -102,4 +102,18 @@ ml-search.py Искать и печатать список расширений имён файлов по имени. +Поиск жанров +^^^^^^^^^^^^ + +Использование:: + + ml-search.py [-i] [-I] [-t ...] genres [-n name] [-t title] + +Искать и печатать список жанров по имени и заголовку. + +Опции:: + + -n, --name имя Искать по названию + -t, --title заголовок Искать по заголовку + .. vim: set tw=72 : diff --git a/docs/command_line.rst b/docs/command_line.rst index bb3b8a2..2697d50 100644 --- a/docs/command_line.rst +++ b/docs/command_line.rst @@ -100,4 +100,18 @@ Usage:: Search and print a list of extensions by name. +Genres search +^^^^^^^^^^^^^ + +Usage:: + + ml-search.py [-i] [-I] [-t ...] genres [-n name] [-t title] + +Search and print a list of genres by name and title. + +Options:: + + -n, --name name Search by name + -t, --title title Search by title + .. vim: set tw=72 : diff --git a/m_librarian/db.py b/m_librarian/db.py index c06fab8..771fd57 100755 --- a/m_librarian/db.py +++ b/m_librarian/db.py @@ -82,7 +82,7 @@ class Extension(SQLObject): class Genre(SQLObject): - name = StringCol(notNull=True, unique=True) + name = UnicodeCol(notNull=True, unique=True) title = UnicodeCol(notNull=True) count = IntCol(notNull=True) books = RelatedJoin('Book', otherColumn='book_id', diff --git a/scripts/ml-search.py b/scripts/ml-search.py index 47b1a8c..ef396fd 100755 --- a/scripts/ml-search.py +++ b/scripts/ml-search.py @@ -6,7 +6,8 @@ from sqlobject.sqlbuilder import CONCAT from m_lib.defenc import default_encoding from m_librarian.db import Author, open_db -from m_librarian.search import search_authors, search_extensions +from m_librarian.search import search_authors, search_extensions, \ + search_genres from m_librarian.translations import translations _ = translations.ugettext @@ -58,6 +59,22 @@ def _search_extensions(case_sensitive, args): (u"(%s: %d)" % (_('books'), ext.count)).encode(default_encoding) +def _search_genres(case_sensitive, args): + values = {} + expressions = [] + for column in 'name', 'title': + value = getattr(args, column) + if value: + values[column] = unicode(value, default_encoding) + if case_sensitive is None: + case_sensitive = _guess_case_sensitivity(values) + for genre in search_genres(args.search_type, case_sensitive, values): + names = filter(None, (genre.name, genre.title)) + fullname = u' '.join(names) + print fullname.encode(default_encoding), \ + (u"(%s: %d)" % (_('books'), genre.count)).encode(default_encoding) + + if __name__ == '__main__': main_parser = argparse.ArgumentParser(description='Search') main_parser.add_argument('-i', '--ignore-case', @@ -86,6 +103,11 @@ if __name__ == '__main__': parser.add_argument('name', help='search by name') parser.set_defaults(func=_search_extensions) + parser = subparsers.add_parser('genres', help='Search genres') + parser.add_argument('-n', '--name', help='search by name') + parser.add_argument('-t', '--title', help='search by title') + parser.set_defaults(func=_search_genres) + args = main_parser.parse_args() if args.case_sensitive: if args.ignore_case: -- 2.39.5