nmbug-status: add support for specifying sort order for each view

Let each view have a "sort" key, typically used with values
"oldest-first" or "newest-first" (although all values in Query.SORT
are accepted), and sort the results accordingly. Oldest first remains
the default.

The dynamic approach of mapping sort values is as suggested by
W. Trevor King <wking@tremily.us>.
This commit is contained in:
Jani Nikula 2015-09-26 14:37:51 +03:00 committed by David Bremner
parent 33c8777a96
commit 8b35b8f71a

View file

@ -156,11 +156,20 @@ class Page (object):
stream.write(self.footer) stream.write(self.footer)
def _write_view(self, database, view, stream): def _write_view(self, database, view, stream):
# sort order, default to oldest-first
sort_key = view.get('sort', 'oldest-first')
# dynamically accept all values in Query.SORT
sort_attribute = sort_key.upper().replace('-', '_')
try:
sort = getattr(notmuch.Query.SORT, sort_attribute)
except AttributeError:
raise ConfigError('Invalid sort setting for {}: {!r}'.format(
view['title'], sort_key))
if 'query-string' not in view: if 'query-string' not in view:
query = view['query'] query = view['query']
view['query-string'] = ' and '.join(query) view['query-string'] = ' and '.join(query)
q = notmuch.Query(database, view['query-string']) q = notmuch.Query(database, view['query-string'])
q.set_sort(notmuch.Query.SORT.OLDEST_FIRST) q.set_sort(sort)
threads = self._get_threads(messages=q.search_messages()) threads = self._get_threads(messages=q.search_messages())
self._write_view_header(view=view, stream=stream) self._write_view_header(view=view, stream=stream)
self._write_threads(threads=threads, stream=stream) self._write_threads(threads=threads, stream=stream)