From fe5be1115bf5bd54f47acf3546d4dd34453576c0 Mon Sep 17 00:00:00 2001 From: jpt Date: Sat, 3 May 2025 18:09:34 -0500 Subject: [PATCH] use THing.type field --- src/tt/config.py | 4 ++-- src/tt/controller/things.py | 6 +++++- src/tt/tui/editor.py | 22 ++++++++++++++++------ 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/tt/config.py b/src/tt/config.py index 96b5fb5..b30eb45 100644 --- a/src/tt/config.py +++ b/src/tt/config.py @@ -43,8 +43,8 @@ def get_view(name): raise ValueError(f"no such view! {name}") def get_column(name): - if name == "id": - return {"field_name": "id", "display_name": "ID", "read_only": True} + if name in ("id", "type"): + return {"field_name": name, "display_name": name, "read_only": True} for col in _load_config().get("columns", []): if col["field_name"] == name: diff --git a/src/tt/controller/things.py b/src/tt/controller/things.py index 792e899..c38354d 100644 --- a/src/tt/controller/things.py +++ b/src/tt/controller/things.py @@ -87,7 +87,11 @@ def get_things( # no _in query for JSON fields, so use OR condition = False for cond in val: - condition |= Thing.data[param] == cond + # handle type filtering the same way + if param == "type": + condition |= Thing.type == cond + else: + condition |= Thing.data[param] == cond query = query.where(condition) sort_expressions = _parse_sort_string(sort) diff --git a/src/tt/tui/editor.py b/src/tt/tui/editor.py index 8b8544d..ef11194 100644 --- a/src/tt/tui/editor.py +++ b/src/tt/tui/editor.py @@ -105,13 +105,13 @@ class TableEditor(App): - sort_string """ self.table_config = [] - view = config.get_view(name) + self.view = config.get_view(name) # FIXME: special case during things conversion # if recurring, different column definition # set up columns - for col_name in view["columns"]: + for col_name in self.view["columns"]: col = config.get_column(col_name) field_type = col.get("field_type", "text") field_cls = get_col_cls(field_type) @@ -129,8 +129,9 @@ class TableEditor(App): self.table_config.append(cc) # load default filters - self.filters = view["filters"] - self.sort_string = view["sort"] + self.filters = self.view["filters"] + self.sort_string = self.view["sort"] + self.defaults = self.view["defaults"] def _db_item_to_row(self, item): """ @@ -139,7 +140,8 @@ class TableEditor(App): row = [] for col in self.table_config: - # TODO: decide how this should work once things is done + # look up properties first (ID, type, etc.) + # & fall back to data JSON if not found try: val = getattr(item, col.field) except AttributeError: @@ -241,12 +243,20 @@ class TableEditor(App): for fc in self.table_config: if fc.read_only: continue - val = self.filters.get(fc.field, fc.default) + val = self.defaults.get(fc.field, fc.default) if val is not None: if "," in val: val = val.split(",")[0] # TODO: fix hack for enums prepopulated[fc.field] = val + # required fields + for required in ["type"]: + if required not in prepopulated: + try: + prepopulated[required] = self.defaults[required] + except KeyError: + raise Exception(f"must set a default for {required}") + new_item = self.add_item_callback(**prepopulated) self.refresh_data(restore_cursor=False) self.move_cursor_to_item(new_item.id) # TODO: check success here