From 7b957dd2c0a71a97896e1171aacd2e0dc98c85b0 Mon Sep 17 00:00:00 2001 From: James Turk Date: Sun, 5 Jan 2025 00:55:23 -0600 Subject: [PATCH] status sort order --- src/tt/controller.py | 19 +++++++++++++++++-- src/tt/db.py | 3 ++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/tt/controller.py b/src/tt/controller.py index e084449..f1bfb93 100644 --- a/src/tt/controller.py +++ b/src/tt/controller.py @@ -2,6 +2,7 @@ import json from datetime import datetime from peewee import fn from .db import db, Task, Category, TaskStatus, SavedSearch +from peewee import Case, Value def category_lookup(category): @@ -44,6 +45,15 @@ def update_task( return task +def _get_status_order(status_field): + # CASE statement that maps each status to its position in the order + order_case = Case( + status_field, [(status.value, Value(i)) for i, status in enumerate(TaskStatus)] + ) + + return order_case + + def _parse_sort_string(sort_string, model_class): """ Convert sort string like 'field1,-field2' to peewee order_by expressions. @@ -57,8 +67,13 @@ def _parse_sort_string(sort_string, model_class): is_desc = field.startswith("-") field_name = field[1:] if is_desc else field - # special handling for due_date with COALESCE - if field_name == "due_date": + if field == "status": + sort_expressions.append( + _get_status_order(Task.status).desc() + if is_desc + else _get_status_order(Task.status).asc() + ) + elif field_name == "due_date": expr = fn.COALESCE(getattr(model_class, field_name), datetime(3000, 12, 31)) sort_expressions.append(expr.desc() if is_desc else expr) else: diff --git a/src/tt/db.py b/src/tt/db.py index 1b27bcc..b43defe 100644 --- a/src/tt/db.py +++ b/src/tt/db.py @@ -21,9 +21,10 @@ db = SqliteDatabase( class TaskStatus(Enum): - ZERO = "zero" + # order is used for sorting WIP = "wip" BLOCKED = "blocked" + ZERO = "zero" DONE = "done"