From 86b80f87f868606e24fbf126ce2ee12a375e9bf7 Mon Sep 17 00:00:00 2001 From: James Turk Date: Sun, 5 Jan 2025 06:59:34 -0600 Subject: [PATCH] fix callbacks --- src/tt/controller.py | 10 +++++----- src/tt/tui/editor.py | 24 ++++++++++++++---------- src/tt/tui/tasks.py | 7 +++---- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/tt/controller.py b/src/tt/controller.py index cd3c004..3834a3e 100644 --- a/src/tt/controller.py +++ b/src/tt/controller.py @@ -11,8 +11,8 @@ def category_lookup(category): return category.id -def get_task(task_id: int) -> Task: - return Task.get_by_id(task_id) +def get_task(item_id: int) -> Task: + return Task.get_by_id(item_id) def add_task( @@ -35,15 +35,15 @@ def add_task( def update_task( - task_id: int, + item_id: int, **kwargs, ) -> Task: with db.atomic(): if category := kwargs.pop("category", None): kwargs["category_id"] = category_lookup(category) - query = Task.update(kwargs).where(Task.id == task_id) + query = Task.update(kwargs).where(Task.id == item_id) query.execute() - task = Task.get_by_id(task_id) + task = Task.get_by_id(item_id) return task diff --git a/src/tt/tui/editor.py b/src/tt/tui/editor.py index 1a99f3a..3020d86 100644 --- a/src/tt/tui/editor.py +++ b/src/tt/tui/editor.py @@ -113,7 +113,7 @@ class TableEditor(App): def __init__(self, default_view="default"): super().__init__() self.filters = {} - self.sort_string = "due,status" + self.sort_string = "" # TODO: default sort self._load_view(default_view) self.search_query = "" self.saved_cursor_pos = (1, 0) @@ -192,20 +192,23 @@ class TableEditor(App): if self.table.cursor_column == 0: cur_row = self.table.cursor_row item_id = int(self.table.get_cell_at((cur_row, 0))) + # TODO: deletable items need a delete self.update_item_callback(item_id, deleted=True) self._save_cursor() self.refresh_data() def action_add_item(self): - # if filtering on type, status, or category - # the new item should use the selected value - + # the new item should use the selected value if filtered # prepopulate with either the field default or the current filter - prepopulated = { - field.name: self.filters.get(field.name, field.default) - for field in self.TABLE_CONFIG - } - # TODO: need to split type_ and status + prepopulated = {} + for fc in self.TABLE_CONFIG: + val = self.filters.get(fc.field, fc.default) + if val is not None: + # enums use comma separated filters + if fc.enum: + prepopulated[fc.field] = val.split(",")[0] + else: + prepopulated[fc.field] = val new_item = self.add_item_callback(**prepopulated) self.refresh_data(restore_cursor=False) @@ -232,6 +235,7 @@ class TableEditor(App): # trigger item_id to be saved on the next cursor move # this avoids filtered columns disappearing right away # and tons of DB writes + # TODO: status hard coded here self._register_save_on_move(item_id, status=next_val) def _register_save_on_move(self, item_id, **kwargs): @@ -245,7 +249,6 @@ class TableEditor(App): self.update_item_callback(**self.save_on_move) self._save_cursor() self.refresh_data() - # reset status self.save_on_move = None def move_cursor_to_item(self, item_id): @@ -356,6 +359,7 @@ class TableEditor(App): # preprocess/validate the field being saved try: update_data[field] = cconf.preprocessor(new_value) + # raise Exception(update_data) self.update_item_callback(item_id, **update_data) self.refresh_data() except NotifyValidationError as e: diff --git a/src/tt/tui/tasks.py b/src/tt/tui/tasks.py index 4acca0c..86ba3ba 100644 --- a/src/tt/tui/tasks.py +++ b/src/tt/tui/tasks.py @@ -55,9 +55,6 @@ class TT(TableEditor): TableColumnConfig("due", "Due", default="", preprocessor=due_preprocessor), TableColumnConfig("category", "Category", default="main"), ) - update_item_callback = update_task - update_item_callback = add_task - get_item_callback = get_task BINDINGS = [ # saved views @@ -68,6 +65,9 @@ class TT(TableEditor): def __init__(self, default_view="default"): super().__init__() self._load_view(default_view) + self.update_item_callback = update_task + self.add_item_callback = add_task + self.get_item_callback = get_task def _load_view(self, name): try: @@ -122,7 +122,6 @@ class TT(TableEditor): def run(default_view): - initialize_db() app = TT(default_view) app.run()