From cd508a983971cb2d185c7839e7238c44aa0d871e Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sat, 14 May 2016 19:18:23 +0300 Subject: [PATCH] Sort results --- m_librarian/search.py | 30 ++++++++++++++++++------------ scripts/ml-search.py | 12 ++++++++---- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/m_librarian/search.py b/m_librarian/search.py index ad36cd0..c4b5325 100644 --- a/m_librarian/search.py +++ b/m_librarian/search.py @@ -44,28 +44,34 @@ def _search_substring(table, case_sensitive, values, expressions): expressions) -def _search(table, search_type, case_sensitive, values, expressions): +def _search(table, search_type, case_sensitive, values, + expressions, orderBy=None): _search_f = globals()['_search_%s' % search_type] conditions = _search_f(table, case_sensitive, values, expressions) - return table.select(conditions) + return table.select(conditions, orderBy=orderBy) def search_authors(search_type, case_sensitive, values, - expressions): - return _search(Author, search_type, case_sensitive, values, expressions) + expressions, orderBy=None): + return _search(Author, search_type, case_sensitive, values, + expressions, orderBy) -def search_books(search_type, case_sensitive, values): - return _search(Book, search_type, case_sensitive, values, []) +def search_books(search_type, case_sensitive, values, orderBy=None): + return _search(Book, search_type, case_sensitive, values, + [], orderBy) -def search_extensions(search_type, case_sensitive, values): - return _search(Extension, search_type, case_sensitive, values, []) +def search_extensions(search_type, case_sensitive, values, orderBy=None): + return _search(Extension, search_type, case_sensitive, values, + [], orderBy) -def search_genres(search_type, case_sensitive, values): - return _search(Genre, search_type, case_sensitive, values, []) +def search_genres(search_type, case_sensitive, values, orderBy=None): + return _search(Genre, search_type, case_sensitive, values, + [], orderBy) -def search_languages(search_type, case_sensitive, values): - return _search(Language, search_type, case_sensitive, values, []) +def search_languages(search_type, case_sensitive, values, orderBy=None): + return _search(Language, search_type, case_sensitive, values, + [], orderBy) diff --git a/scripts/ml-search.py b/scripts/ml-search.py index 2915f51..04a8b53 100755 --- a/scripts/ml-search.py +++ b/scripts/ml-search.py @@ -43,7 +43,8 @@ def _search_authors(case_sensitive, args): if case_sensitive is None: case_sensitive = _guess_case_sensitivity(values) for author in search_authors(args.search_type, case_sensitive, values, - expressions): + expressions, + orderBy=('surname', 'name', 'misc_name')): names = filter(None, (author.surname, author.name, author.misc_name)) fullname = u' '.join(names) print fullname.encode(default_encoding), \ @@ -57,7 +58,8 @@ def _search_extensions(case_sensitive, args): case_sensitive = _guess_case_sensitivity(values) else: values = {} - for ext in search_extensions(args.search_type, case_sensitive, values): + for ext in search_extensions(args.search_type, case_sensitive, values, + orderBy='name'): print ext.name.encode(default_encoding), \ (u"(%s: %d)" % (_('books'), ext.count)).encode(default_encoding) @@ -71,7 +73,8 @@ def _search_genres(case_sensitive, args): 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): + for genre in search_genres(args.search_type, case_sensitive, values, + orderBy='name'): names = filter(None, (genre.name, genre.title)) fullname = u' '.join(names) print fullname.encode(default_encoding), \ @@ -85,7 +88,8 @@ def _search_languages(case_sensitive, args): case_sensitive = _guess_case_sensitivity(values) else: values = {} - for lang in search_languages(args.search_type, case_sensitive, values): + for lang in search_languages(args.search_type, case_sensitive, values, + orderBy='name'): print lang.name.encode(default_encoding), \ (u"(%s: %d)" % (_('books'), lang.count)).encode(default_encoding) -- 2.39.5