From b4908b9eb6f44e2cb5fab035f8580ffab7f1cec8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Thu, 1 Dec 2022 00:44:41 +0100 Subject: Allow selecting of sort key for search results. --- mu4web/main.py | 41 +++++++++++++++++++++++++++++++++++------ mu4web/static/style.css | 8 ++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/mu4web/main.py b/mu4web/main.py index 19fb279..0962236 100644 --- a/mu4web/main.py +++ b/mu4web/main.py @@ -13,7 +13,9 @@ from flask_login import ( logout_user, ) from typing import ( + Literal, Optional, + Union, cast, ) from mu import get_mail @@ -260,12 +262,16 @@ def search_field(q: str) -> HTML: ('input', {'type': 'Submit', 'value': 'Sök'})) -def search_result(q, by, reverse) -> HTML: +SortDirection = Union[Literal['rising'], + Literal['falling']] + + +def search_result(q: str, by: Optional[mu.Sortfield], direction: SortDirection) -> HTML: # keys = ['from', 'to', 'subject', 'date', 'size', 'maildir', 'msgid'] keys = ['from', 'to', 'subject', 'date'] - rows = mu.search(q, by, reverse) + rows = mu.search(q, by, direction == 'falling') body: list[tuple] = [] for row in rows: rowdata = [] @@ -280,20 +286,38 @@ def search_result(q, by, reverse) -> HTML: if len(rows) == 0: return "No results" else: + + heads: list[HTML] = [] + for m in keys: + link_body = m.title() + params = {'q': q, 'by': m} + if m == by: + link_body += ' ' + if direction == 'rising': + link_body += '▲' + params['direction'] = 'falling' + else: + link_body += '▼' + params['direction'] = 'rising' + heads.append(('th', ('a', {'href': '?' + urlencode(params)}, + link_body))) + return ('div', ('p', f"{len(rows)} träffar"), ('table', ('thead', ('tr', - [('th', m.title()) for m in keys])), + *heads + )), ('tbody', body))) -def search_page(q: str, by: Optional[mu.Sortfield] = None) -> str: +def search_page(q: str, by: Optional[mu.Sortfield], + direction: SortDirection) -> str: main_body = [search_field(q)] if q: - main_body.append(search_result(q, by, False)) + main_body.append(search_result(q, by, direction)) return render_document(page_base(title='Search', body=main_body)) @@ -378,8 +402,13 @@ def index(): @app.route('/search') @login_required def search_page_(): + DEFAULT_DIRECTION = 'falling' + direction = request.args.get('direction', DEFAULT_DIRECTION) + if direction not in ('rising', 'falling'): + direction = DEFAULT_DIRECTION return search_page(request.args.get('q'), - request.args.get('by', None)) + request.args.get('by', None), + direction) def multipart_page(msg_id: str, diff --git a/mu4web/static/style.css b/mu4web/static/style.css index 8297aab..8b88aa5 100644 --- a/mu4web/static/style.css +++ b/mu4web/static/style.css @@ -58,6 +58,14 @@ dd > * { margin: 0; } +th { + background-color: lightblue; +} + +th a { + color: initial; +} + tr:nth-child(2n) { background-color: lightblue; } -- cgit v1.2.3