]> git.phdru.name Git - m_librarian.git/commitdiff
Feat(web): Sort authors by columns
authorOleg Broytman <phd@phdru.name>
Mon, 8 Jul 2024 14:36:47 +0000 (17:36 +0300)
committerOleg Broytman <phd@phdru.name>
Tue, 9 Jul 2024 00:42:00 +0000 (03:42 +0300)
[skip ci]

TODO
m_librarian/search.py
m_librarian/web/app.py
m_librarian/web/views/list_authors.py
m_librarian/web/views/list_authors.tmpl

diff --git a/TODO b/TODO
index 5be94da5b5e6030c5570b9c8ed0c1e530fc7b98b..4e9904f569856f1a997bf21e15aa0ef4c2d1b03f 100644 (file)
--- 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.
index c190dc8aa3f2bcb858259671b4f0fbf6d847d274..d3b10083cfe4e0becd1349d86ba3d504abd646e8 100644 (file)
@@ -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,
     }
 
 
index 72f9104a425a3c86091da1d8890351c3b8cfb662..e19e355e516e4022e87922d9850cb0eb9b65acec 100644 (file)
@@ -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/<aid:int>/', method='GET')
index dca1c4090779344699278e1adcc27aed4a0e24d6..e1f00079d27d940a16383eabf058fbc2da7a3a28 100644 (file)
@@ -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'''</td>
+                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'''    <a href="/list_authors/?search_authors=''')
+                _v = VFFSL(SL,"search_authors",True) # u'$search_authors' on line 32, col 44
+                if _v is not None: write(_filter(_v, rawExpr=u'$search_authors')) # from line 32, col 44.
+                write(u'''&search_type=''')
+                _v = VFFSL(SL,"search_type",True) # u'$search_type' on line 32, col 72
+                if _v is not None: write(_filter(_v, rawExpr=u'$search_type')) # from line 32, col 72.
+                write(u'''&case_sensitive=''')
+                _v = VFFSL(SL,"case_param",True) # u'$case_param' on line 32, col 100
+                if _v is not None: write(_filter(_v, rawExpr=u'$case_param')) # from line 32, col 100.
+                write(u'''&sort=''')
+                _v = VFFSL(SL,"sort_param",True) # u'$sort_param' on line 32, col 117
+                if _v is not None: write(_filter(_v, rawExpr=u'$sort_param')) # from line 32, col 117.
+                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'''</a>
+    </td>
 ''')
             write(u'''  </tr>
 ''')
-            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'''  <tr>
 ''')
-                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'''    <td''')
-                    _v = VFFSL(SL,"style",True) # u'$style' on line 29, col 8
-                    if _v is not None: write(_filter(_v, rawExpr=u'$style')) # from line 29, col 8.
+                    _v = VFFSL(SL,"style",True) # u'$style' on line 44, col 8
+                    if _v is not None: write(_filter(_v, rawExpr=u'$style')) # from line 44, col 8.
                     write(u'''><a href="/books-by-author/''')
-                    _v = VFFSL(SL,"author.id",True) # u'$author.id' on line 29, col 41
-                    if _v is not None: write(_filter(_v, rawExpr=u'$author.id')) # from line 29, col 41.
+                    _v = VFFSL(SL,"author.id",True) # u'$author.id' on line 44, col 41
+                    if _v is not None: write(_filter(_v, rawExpr=u'$author.id')) # from line 44, col 41.
                     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'''
     </a></td>
 ''')
@@ -149,8 +178,8 @@ class list_authors(layout):
 ''')
             write(u'''  </table>
 ''')
-            _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'''  <p>\u041d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u0430\u0432\u0442\u043e\u0440\u0430!</p>
 ''')
         
index a364ebaa203b9d6f54f81f9dfff42026120cef1b..3848618e24df54bc7c5b118410c2eab7c5a16a11 100644 (file)
@@ -15,7 +15,22 @@ $search_authors_form(searchList=$searchList)
   <tr>
   #set $_ = $getattr($translations, 'ugettext', None) or $translations.gettext
   #for $column in $columns
-    <td style="text-align: center">$_($column)</td>
+    <td style="text-align: center">$_($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 ''
+    <a href="/list_authors/?search_authors=$search_authors&search_type=$search_type&case_sensitive=$case_param&sort=$sort_param">$sort_sign</a>
+    </td>
   #end for
   </tr>
   #for $author in $authors