Compare commits
2 Commits
35aaf17de2
...
fb23b5161f
Author | SHA1 | Date | |
---|---|---|---|
fb23b5161f | |||
7b957dd2c0 |
@ -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:
|
||||
|
@ -21,9 +21,10 @@ db = SqliteDatabase(
|
||||
|
||||
|
||||
class TaskStatus(Enum):
|
||||
ZERO = "zero"
|
||||
# order is used for sorting
|
||||
WIP = "wip"
|
||||
BLOCKED = "blocked"
|
||||
ZERO = "zero"
|
||||
DONE = "done"
|
||||
|
||||
|
||||
|
@ -29,6 +29,7 @@ from .utils import (
|
||||
get_colored_date,
|
||||
)
|
||||
|
||||
DEFAULT_CATEGORY = "main"
|
||||
COLUMNS = ("ID", "Task", "Status", "Type", "Due", "Category")
|
||||
column_to_field = {
|
||||
0: "ID",
|
||||
@ -231,11 +232,16 @@ class TT(App):
|
||||
self.refresh_tasks()
|
||||
|
||||
def action_add_task(self):
|
||||
# if filtering on type, status, or category
|
||||
# the new task should use the selected value
|
||||
category = self.filters.get("category", DEFAULT_CATEGORY)
|
||||
status = self.filters.get("status", TaskStatus.ZERO.value).split(",")[0]
|
||||
type_ = self.filters.get("type", "").split(",")[0]
|
||||
new_task = add_task(
|
||||
text="New Task",
|
||||
type="",
|
||||
status=TaskStatus.ZERO.value,
|
||||
category="main",
|
||||
type=type_,
|
||||
status=status,
|
||||
category=category,
|
||||
)
|
||||
self.refresh_tasks(restore_cursor=False)
|
||||
self.move_cursor_to_task(new_task.id)
|
||||
|
Loading…
Reference in New Issue
Block a user