From fce1fcce340a02e02f0282266901215d0d561b31 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Mon, 8 Jul 2024 17:36:47 +0300 Subject: [PATCH] Feat(web): Sort authors by columns [skip ci] --- TODO | 2 +- m_librarian/search.py | 38 +++++++++++++-- m_librarian/web/app.py | 6 ++- m_librarian/web/views/list_authors.py | 63 ++++++++++++++++++------- m_librarian/web/views/list_authors.tmpl | 17 ++++++- 5 files changed, 102 insertions(+), 24 deletions(-) diff --git a/TODO b/TODO index 5be94da..4e9904f 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,7 @@ Next release ------------ -(wx) List of authors - sort by clicking on column titles. +List of authors - sort by clicking on column titles. Installers. diff --git a/m_librarian/search.py b/m_librarian/search.py index c190dc8..d3b1008 100644 --- a/m_librarian/search.py +++ b/m_librarian/search.py @@ -1,3 +1,8 @@ +try: + from urllib.parse import quote +except ImportError: + from urllib import quote + from sqlobject.sqlbuilder import AND, OR, func, CONCAT from .config import get_config @@ -122,7 +127,7 @@ def _guess_case_sensitivity(value): return not value.islower() -def search_authors_raw(value, search_type, case_sensitive): +def search_authors_raw(value, search_type, case_sensitive, sort=None): value = decode(value) if not search_type: search_type = 'start' @@ -132,15 +137,42 @@ def search_authors_raw(value, search_type, case_sensitive): 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']) + orderby_columns = columns[:] + try: + idx = orderby_columns.index('fullname') + except ValueError: + pass + else: + orderby_columns[idx:idx+1] = ('surname', 'name', 'misc_name') + if sort: + if sort[0] in '+-': + sort_order = sort[0] + sort_key = sort[1:] + else: + sort_order = '+' + sort_key = sort + sort = sort_order+sort_key + if sort_key == 'fullname': + orderby_columns = (sort_order+'surname', + sort_order+'name', sort_order+'misc_name') + elif sort_key == 'count': + orderby_columns = (sort_order+'count', sort_order+'surname', + sort_order+'name', sort_order+'misc_name') + else: + orderby_columns = [sort_order+sort_key] + else: + sort = '+' + columns[0] + authors = search_authors(search_type, case_sensitive, {}, expressions, + orderBy=orderby_columns) return { 'authors': list(authors), 'search_authors': value, 'search_type': search_type, 'case_sensitive': case_sensitive, 'columns': columns, + 'sort': sort, + 'quote': quote, } diff --git a/m_librarian/web/app.py b/m_librarian/web/app.py index 72f9104..e19e355 100644 --- a/m_librarian/web/app.py +++ b/m_librarian/web/app.py @@ -41,7 +41,8 @@ def list_authors_get(): return redirect('/search_authors/') search_type = request.query.get('search_type') case_sensitive = request.query.get('case_sensitive') - return search_authors_raw(value, search_type, case_sensitive) + sort = request.query.get('sort') + return search_authors_raw(value, search_type, case_sensitive, sort) @route('/list_authors/', method='POST') @@ -52,7 +53,8 @@ def list_authors_post(): return redirect('/search_authors/') search_type = request.forms.get('search_type') case_sensitive = request.forms.get('case_sensitive') - return search_authors_raw(value, search_type, case_sensitive) + sort = request.forms.get('sort') + return search_authors_raw(value, search_type, case_sensitive, sort) @route('/books-by-author//', method='GET') diff --git a/m_librarian/web/views/list_authors.py b/m_librarian/web/views/list_authors.py index dca1c40..e1f0007 100644 --- a/m_librarian/web/views/list_authors.py +++ b/m_librarian/web/views/list_authors.py @@ -37,10 +37,10 @@ VFN=valueForName currentTime=time.time __CHEETAH_version__ = '3.3.1' __CHEETAH_versionTuple__ = (3, 3, 1, 'final', 0) -__CHEETAH_genTime__ = 1718725815.054143 -__CHEETAH_genTimestamp__ = 'Tue Jun 18 18:50:15 2024' +__CHEETAH_genTime__ = 1720485596.472826 +__CHEETAH_genTimestamp__ = 'Tue Jul 9 03:39:56 2024' __CHEETAH_src__ = 'list_authors.tmpl' -__CHEETAH_srcLastModified__ = 'Tue Jun 18 18:50:12 2024' +__CHEETAH_srcLastModified__ = 'Tue Jul 9 03:39:54 2024' __CHEETAH_docstring__ = 'Autogenerated by Cheetah: The Python-Powered Template Engine' if __CHEETAH_versionTuple__ < RequiredCheetahVersionTuple: @@ -101,7 +101,7 @@ class list_authors(layout): ''') if VFFSL(SL,"authors",True): # generated from line 12, col 1 - _orig_filter_63533534 = _filter + _orig_filter_20064572 = _filter filterName = u'WebSafe' if "WebSafe" in self._CHEETAH__filters: _filter = self._CHEETAH__currentFilter = self._CHEETAH__filters[filterName] @@ -120,28 +120,57 @@ class list_authors(layout): _(VFFSL(SL,"column",True)) _v = VFFSL(SL,"_",False)(VFFSL(SL,"column",True)) # u'$_($column)' on line 18, col 36 if _v is not None: write(_filter(_v, rawExpr=u'$_($column)')) # from line 18, col 36. - write(u''' + write(u''' +''') + if VFFSL(SL,"sort",True) in (VFFSL(SL,"column",True), '+'+VFFSL(SL,"column",True), '-'+VFFSL(SL,"column",True)): # generated from line 19, col 5 + if VFFSL(SL,"sort",True) in (VFFSL(SL,"column",True), '+'+VFFSL(SL,"column",True)): # generated from line 20, col 7 + sort_param = '-'+VFFSL(SL,"column",True) + sort_sign = '↓' + elif VFFSL(SL,"sort",True) == '-'+VFFSL(SL,"column",True): # generated from line 23, col 7 + sort_param = VFFSL(SL,"quote",False)('+')+VFFSL(SL,"column",True) + sort_sign = '↑' + else: # generated from line 27, col 5 + sort_param = VFFSL(SL,"quote",False)('+')+VFFSL(SL,"column",True) + sort_sign = '↓' + case_param = '1' if VFFSL(SL,"case_sensitive",True) else '' + write(u''' ''') + _v = VFFSL(SL,"sort_sign",True) # u'$sort_sign' on line 32, col 130 + if _v is not None: write(_filter(_v, rawExpr=u'$sort_sign')) # from line 32, col 130. + write(u''' + ''') write(u''' ''') - for author in VFFSL(SL,"authors",True): # generated from line 21, col 3 + for author in VFFSL(SL,"authors",True): # generated from line 36, col 3 write(u''' ''') - for column in VFFSL(SL,"columns",True): # generated from line 23, col 3 - if VFFSL(SL,"column",True) == 'count': # generated from line 24, col 5 + for column in VFFSL(SL,"columns",True): # generated from line 38, col 3 + if VFFSL(SL,"column",True) == 'count': # generated from line 39, col 5 style = ' style="text-align: right; width: 5ex"' - else: # generated from line 26, col 5 + else: # generated from line 41, col 5 style = '' write(u''' ''') - _v = VFFSL(SL,"unicode",False)(VFFSL(SL,"getattr",False)(VFFSL(SL,"author",True), VFFSL(SL,"column",True)) or '') # u"$unicode($getattr($author, $column) or '')" on line 30, col 5 - if _v is not None: write(_filter(_v, rawExpr=u"$unicode($getattr($author, $column) or '')")) # from line 30, col 5. + _v = VFFSL(SL,"unicode",False)(VFFSL(SL,"getattr",False)(VFFSL(SL,"author",True), VFFSL(SL,"column",True)) or '') # u"$unicode($getattr($author, $column) or '')" on line 45, col 5 + if _v is not None: write(_filter(_v, rawExpr=u"$unicode($getattr($author, $column) or '')")) # from line 45, col 5. write(u''' ''') @@ -149,8 +178,8 @@ class list_authors(layout): ''') write(u''' ''') - _filter = self._CHEETAH__currentFilter = _orig_filter_63533534 - else: # generated from line 37, col 1 + _filter = self._CHEETAH__currentFilter = _orig_filter_20064572 + else: # generated from line 52, col 1 write(u'''

\u041d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u0430\u0432\u0442\u043e\u0440\u0430!

''') diff --git a/m_librarian/web/views/list_authors.tmpl b/m_librarian/web/views/list_authors.tmpl index a364eba..3848618 100644 --- a/m_librarian/web/views/list_authors.tmpl +++ b/m_librarian/web/views/list_authors.tmpl @@ -15,7 +15,22 @@ $search_authors_form(searchList=$searchList) #set $_ = $getattr($translations, 'ugettext', None) or $translations.gettext #for $column in $columns - $_($column) + $_($column) + #if $sort in ($column, '+'+$column, '-'+$column) + #if $sort in ($column, '+'+$column) + #set $sort_param='-'+$column + #set $sort_sign='↓' + #elif $sort == '-'+$column + #set $sort_param=$quote('+')+$column + #set $sort_sign='↑' + #end if + #else + #set $sort_param=$quote('+')+$column + #set $sort_sign='↓' + #end if + #set $case_param='1' if $case_sensitive else '' + $sort_sign + #end for #for $author in $authors -- 2.39.5