Improve filtering, add "move facts"

This commit is contained in:
3wc 2023-10-27 16:28:46 +01:00
parent 8e5e28ea67
commit cbbf952787
2 changed files with 57 additions and 9 deletions

View File

@ -12,6 +12,7 @@ class ActivitiesScreen(Screen):
BINDINGS = [
("q", "quit", "Quit"),
("s", "sort", "Sort"),
("f", "move_facts", "Move facts"),
("r", "refresh", "Refresh"),
("d", "delete", "Delete activity"),
("/", "filter", "Search"),
@ -60,19 +61,30 @@ class ActivitiesScreen(Screen):
def action_sort(self) -> None:
self.table.cursor_type = "column"
def on_data_table_column_selected(self, event):
self.sort = (event.column_key,)
event.data_table.sort(*self.sort)
event.data_table.cursor_type = "row"
def action_filter(self) -> None:
filter_input = self.query_one("#filter")
filter_input.display = True
self._refresh(filter_input.value)
filter_input.focus()
print(filter_input)
def on_input_submitted(self, event):
self.table.focus()
def action_cancelfilter(self) -> None:
filter_input = self.query_one("#filter")
filter_input.display = False
filter_input.clear()
self.table.focus()
self._refresh()
def action_delete(self) -> None:
# get the keys for the row and column under the cursor.
# get the keys for the row and column under the cursor.
row_key, _ = self.table.coordinate_to_cell_key(self.table.cursor_coordinate)
activity_id = self.table.get_cell_at(
@ -85,15 +97,30 @@ class ActivitiesScreen(Screen):
# supply the row key to `remove_row` to delete the row.
self.table.remove_row(row_key)
def on_data_table_column_selected(self, event):
self.sort = (event.column_key,)
event.data_table.sort(*self.sort)
event.data_table.cursor_type = "row"
def action_move_facts(self) -> None:
row_idx: int = self.table.cursor_row
row_cells = self.table.get_row_at(row_idx)
self.move_from_activity = Activity.get_by_id(self.db_manager, row_cells[2])
for col_idx, cell_value in enumerate(row_cells):
cell_coordinate = Coordinate(row_idx, col_idx)
self.table.update_cell_at(
cell_coordinate,
f"[red]{cell_value}[/red]",
)
self.table.move_cursor(row=self.table.cursor_coordinate[0]+1)
def on_data_table_row_selected(self, event):
if getattr(self, "move_from_activity", None) is not None:
move_to_activity = Activity.get_by_id(self.db_manager, self.table.get_cell_at(
Coordinate(event.cursor_row, 2)
))
self.move_from_activity.move_facts(move_to_activity)
self._refresh()
del self.move_from_activity
def on_input_changed(self, event):
self._refresh(event.value)
class CategoriesScreen(Screen):
BINDINGS = [
("s", "sort", "Sort"),
@ -149,12 +176,17 @@ class CategoriesScreen(Screen):
def action_filter(self) -> None:
filter_input = self.query_one("#filter")
filter_input.display = True
self._refresh(filter_input.value)
filter_input.focus()
print(filter_input)
def on_input_submitted(self, event):
self.table.focus()
def action_cancelfilter(self) -> None:
filter_input = self.query_one("#filter")
filter_input.display = False
filter_input.clear()
self.table.focus()
self._refresh()
def on_input_changed(self, event):

View File

@ -92,6 +92,22 @@ class Activity(BaseORM):
self.category_name = category_name
self.facts_count = facts_count
def move_facts(self, to_activity):
cursor = self.db_manager.get_cursor()
print(f"moving from {self.id} to {to_activity.id}")
cursor.execute("""
UPDATE
facts
SET
activity_id = ?
WHERE
activity_id = ?
""", (to_activity.id, self.id))
self.conn.commit()
@staticmethod
def list_activities(db_manager, filter_query=None):
cursor = db_manager.get_cursor()
@ -121,7 +137,7 @@ class Activity(BaseORM):
"""
if filter_query is not None:
cursor.execute(sql, ("%{}%".format(filter_query),) * 2 )
cursor.execute(sql, ("%{}%".format(filter_query),) * 2)
else:
cursor.execute(sql)