status sort order

This commit is contained in:
James Turk 2025-01-05 00:55:23 -06:00
parent 35aaf17de2
commit 7b957dd2c0
2 changed files with 19 additions and 3 deletions

View File

@ -2,6 +2,7 @@ import json
from datetime import datetime from datetime import datetime
from peewee import fn from peewee import fn
from .db import db, Task, Category, TaskStatus, SavedSearch from .db import db, Task, Category, TaskStatus, SavedSearch
from peewee import Case, Value
def category_lookup(category): def category_lookup(category):
@ -44,6 +45,15 @@ def update_task(
return 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): def _parse_sort_string(sort_string, model_class):
""" """
Convert sort string like 'field1,-field2' to peewee order_by expressions. 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("-") is_desc = field.startswith("-")
field_name = field[1:] if is_desc else field field_name = field[1:] if is_desc else field
# special handling for due_date with COALESCE if field == "status":
if field_name == "due_date": 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)) expr = fn.COALESCE(getattr(model_class, field_name), datetime(3000, 12, 31))
sort_expressions.append(expr.desc() if is_desc else expr) sort_expressions.append(expr.desc() if is_desc else expr)
else: else:

View File

@ -21,9 +21,10 @@ db = SqliteDatabase(
class TaskStatus(Enum): class TaskStatus(Enum):
ZERO = "zero" # order is used for sorting
WIP = "wip" WIP = "wip"
BLOCKED = "blocked" BLOCKED = "blocked"
ZERO = "zero"
DONE = "done" DONE = "done"