Switch to testing db, working deletion 🎉
This commit is contained in:
parent
b5e486020e
commit
a0cdf945bf
@ -9,8 +9,9 @@ import click
|
||||
import requests
|
||||
import sqlite3
|
||||
|
||||
HAMSTER_DIR = Path.home() / '.local/share/hamster'
|
||||
HAMSTER_FILE = HAMSTER_DIR / 'hamster.db'
|
||||
# HAMSTER_DIR = Path.home() / '.local/share/hamster'
|
||||
# HAMSTER_FILE = HAMSTER_DIR / 'hamster.db'
|
||||
HAMSTER_FILE = 'hamster-testing.db'
|
||||
|
||||
conn = sqlite3.connect(HAMSTER_FILE)
|
||||
c = conn.cursor()
|
||||
@ -703,7 +704,7 @@ def _import(username, mapping_path=None, output=None, category_search=None, afte
|
||||
@cli.command()
|
||||
def app():
|
||||
from .app import HamsterToolsApp
|
||||
app = HamsterToolsApp(db_cursor=c)
|
||||
app = HamsterToolsApp(db_cursor=c, db_connection=conn)
|
||||
app.run()
|
||||
|
||||
|
||||
|
@ -1,32 +1,58 @@
|
||||
from textual.app import App, ComposeResult
|
||||
from textual.widgets import Header, Footer, DataTable
|
||||
from textual.coordinate import Coordinate
|
||||
|
||||
|
||||
ROWS = [
|
||||
("lane", "swimmer", "country", "time"),
|
||||
(4, "Joseph Schooling", "Singapore", 50.39),
|
||||
(2, "Michael Phelps", "United States", 51.14),
|
||||
(5, "Chad le Clos", "South Africa", 51.14),
|
||||
(6, "László Cseh", "Hungary", 51.14),
|
||||
(3, "Li Zhuhao", "China", 51.26),
|
||||
(8, "Mehdy Metella", "France", 51.58),
|
||||
(7, "Tom Shields", "United States", 51.73),
|
||||
(1, "Aleksandr Sadovnikov", "Russia", 51.84),
|
||||
(10, "Darren Burns", "Scotland", 51.84),
|
||||
]
|
||||
|
||||
class HamsterToolsApp(App):
|
||||
"""A Textual app to manage stopwatches."""
|
||||
|
||||
BINDINGS = [
|
||||
("q", "quit", "Quit"),
|
||||
("m", "mark", "Mark"),
|
||||
("s", "sort", "Sort"),
|
||||
("r", "refresh", "Refresh"),
|
||||
("d", "delete_activity", "Delete activity"),
|
||||
]
|
||||
|
||||
def __init__(self, db_cursor):
|
||||
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()
|
||||
@ -34,26 +60,38 @@ class HamsterToolsApp(App):
|
||||
yield Footer()
|
||||
|
||||
def on_mount(self) -> None:
|
||||
table = self.query_one(DataTable)
|
||||
|
||||
sql = '''
|
||||
SELECT
|
||||
categories.id, categories.name, activities.id, activities.name
|
||||
FROM
|
||||
activities
|
||||
LEFT JOIN
|
||||
categories
|
||||
ON
|
||||
activities.category_id = categories.id '''
|
||||
|
||||
results = self.db_cursor.execute(sql)
|
||||
results = [[cell or "" for cell in row] for row in self.db_cursor.fetchall()]
|
||||
|
||||
columns = table.add_columns("category id","category","activity ID","activity")
|
||||
|
||||
table.add_rows(results)
|
||||
table.sort(columns[1], columns[3])
|
||||
table.cursor_type = "row"
|
||||
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_quit(self) -> None:
|
||||
self.exit()
|
||||
|
||||
def action_refresh(self) -> None:
|
||||
self._refresh()
|
||||
|
||||
def action_sort(self) -> None:
|
||||
self.table.cursor_type = "column"
|
||||
|
||||
def action_delete_activity(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"
|
||||
|
Loading…
Reference in New Issue
Block a user