213 lines
6.3 KiB
Python
213 lines
6.3 KiB
Python
from textual.app import App, ComposeResult
|
|
from textual.widgets import Header, Footer, DataTable, Placeholder
|
|
from textual.coordinate import Coordinate
|
|
from textual.screen import Screen
|
|
|
|
|
|
class ActivitiesScreen(Screen):
|
|
BINDINGS = [
|
|
("q", "quit", "Quit"),
|
|
("s", "sort", "Sort"),
|
|
("r", "refresh", "Refresh"),
|
|
("d", "delete", "Delete activity"),
|
|
]
|
|
|
|
def __init__(self, db_cursor, db_connection):
|
|
self.db_cursor = db_cursor
|
|
self.db_connection = db_connection
|
|
super().__init__()
|
|
|
|
def _refresh(self):
|
|
self.table.clear()
|
|
|
|
sql = '''
|
|
select
|
|
categories.id as category_id,
|
|
coalesce(categories.name, '') as category_name,
|
|
activities.id as activity_id,
|
|
activities.name as activity_name,
|
|
coalesce(facts_count, 0) as total_facts
|
|
from
|
|
activities
|
|
left join
|
|
categories
|
|
on
|
|
activities.category_id = categories.id
|
|
left join (
|
|
select
|
|
activity_id,
|
|
count(*) as facts_count
|
|
from
|
|
facts
|
|
group by
|
|
activity_id
|
|
) as facts_count_subquery
|
|
on
|
|
activities.id = facts_count_subquery.activity_id;
|
|
'''
|
|
|
|
results = self.db_cursor.execute(sql)
|
|
# results = [[cell or "" for cell in row] for row in self.db_cursor.fetchall()]
|
|
|
|
self.table.add_rows(results)
|
|
self.table.sort(self.columns[1], self.columns[3])
|
|
|
|
def compose(self) -> ComposeResult:
|
|
"""create child widgets for the app."""
|
|
yield Header()
|
|
yield DataTable()
|
|
yield Footer()
|
|
|
|
def on_mount(self) -> None:
|
|
self.table = self.query_one(DataTable)
|
|
self.table.cursor_type = "row"
|
|
self.columns = self.table.add_columns("category id","category","activity id","activity","entries")
|
|
self._refresh()
|
|
|
|
def action_refresh(self) -> None:
|
|
self._refresh()
|
|
|
|
def action_sort(self) -> None:
|
|
self.table.cursor_type = "column"
|
|
|
|
def action_delete(self) -> None:
|
|
# 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(
|
|
Coordinate(self.table.cursor_coordinate.row, 2),
|
|
)
|
|
|
|
sql = 'delete from activities where id = ?'
|
|
print(Coordinate(2, self.table.cursor_coordinate.row),)
|
|
print(activity_id)
|
|
|
|
self.db_cursor.execute(sql, (activity_id,))
|
|
self.db_connection.commit()
|
|
|
|
# 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):
|
|
event.data_table.sort(event.column_key)
|
|
event.data_table.cursor_type = "row"
|
|
|
|
|
|
class CategoriesScreen(Screen):
|
|
BINDINGS = [
|
|
("s", "sort", "Sort"),
|
|
("r", "refresh", "Refresh"),
|
|
("d", "delete", "Delete category"),
|
|
]
|
|
|
|
def __init__(self, db_cursor, db_connection):
|
|
self.db_cursor = db_cursor
|
|
self.db_connection = db_connection
|
|
super().__init__()
|
|
|
|
def _refresh(self):
|
|
self.table.clear()
|
|
|
|
sql = '''
|
|
select
|
|
categories.id as category_id,
|
|
coalesce(categories.name, '') as category_name,
|
|
coalesce(activities_count, 0) as total_activities
|
|
from
|
|
categories
|
|
left join (
|
|
select
|
|
category_id,
|
|
count(*) as activities_count
|
|
from
|
|
activities
|
|
group by
|
|
category_id
|
|
) as activities_count_subquery
|
|
on
|
|
categories.id = activities_count_subquery.category_id;
|
|
'''
|
|
|
|
results = self.db_cursor.execute(sql)
|
|
# results = [[cell or "" for cell in row] for row in self.db_cursor.fetchall()]
|
|
|
|
self.table.add_rows(results)
|
|
self.table.sort(self.columns[1])
|
|
|
|
def compose(self) -> ComposeResult:
|
|
"""create child widgets for the app."""
|
|
yield Header()
|
|
yield DataTable()
|
|
yield Footer()
|
|
|
|
def on_mount(self) -> None:
|
|
self.table = self.query_one(DataTable)
|
|
self.table.cursor_type = "row"
|
|
self.columns = self.table.add_columns("category id","category","activities")
|
|
self._refresh()
|
|
|
|
def action_refresh(self) -> None:
|
|
self._refresh()
|
|
|
|
def action_sort(self) -> None:
|
|
self.table.cursor_type = "column"
|
|
|
|
def action_delete(self) -> None:
|
|
# get the keys for the row and column under the cursor.
|
|
row_key, _ = self.table.coordinate_to_cell_key(self.table.cursor_coordinate)
|
|
|
|
category_id = self.table.get_cell_at(
|
|
Coordinate(self.table.cursor_coordinate.row, 0),
|
|
)
|
|
|
|
sql = 'delete from categories where id = ?'
|
|
print(Coordinate(2, self.table.cursor_coordinate.row),)
|
|
print(category_id)
|
|
|
|
self.db_cursor.execute(sql, (category_id,))
|
|
self.db_connection.commit()
|
|
|
|
# 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):
|
|
event.data_table.sort(event.column_key)
|
|
event.data_table.cursor_type = "row"
|
|
|
|
# class KimaiScreen(Screen):
|
|
# def compose(self) -> ComposeResult:
|
|
# yield Placeholder("Help Screen")
|
|
# yield Footer()
|
|
#
|
|
# def __init__(self, db_cursor, db_connection):
|
|
# self.db_cursor = db_cursor
|
|
# self.db_connection = db_connection
|
|
# super().__init__()
|
|
|
|
|
|
class HamsterToolsApp(App):
|
|
BINDINGS = [
|
|
("a", "switch_mode('activities')", "Activities"),
|
|
("c", "switch_mode('categories')", "Categories"),
|
|
# ("k", "switch_mode('kimai')", "Kimai"),
|
|
("q", "quit", "Quit"),
|
|
]
|
|
def __init__(self, db_cursor, db_connection):
|
|
self.db_cursor = db_cursor
|
|
self.db_connection = db_connection
|
|
|
|
self.MODES = {
|
|
"categories": CategoriesScreen(db_cursor, db_connection),
|
|
"activities": ActivitiesScreen(db_cursor, db_connection),
|
|
# "kimai": KimaiScreen,
|
|
}
|
|
|
|
super().__init__()
|
|
|
|
def on_mount(self) -> None:
|
|
self.switch_mode("activities")
|
|
|
|
def action_quit(self) -> None:
|
|
self.exit()
|
|
|