Reasonably-working Kimai API data fetch'n'display
This commit is contained in:
@ -5,7 +5,8 @@ from textual.containers import Horizontal, Vertical
|
||||
from textual.coordinate import Coordinate
|
||||
from textual.screen import Screen
|
||||
|
||||
from .db import DatabaseManager, Category, Activity
|
||||
from .db import DatabaseManager, Category, Activity, KimaiProject, KimaiCustomer
|
||||
from .kimai import KimaiAPI, Customer as KimaiAPICustomer, Project as KimaiAPIProject, Activity as KimaiAPIActivity
|
||||
|
||||
|
||||
class ListScreen(Screen):
|
||||
@ -55,7 +56,6 @@ class ListScreen(Screen):
|
||||
|
||||
class ActivitiesScreen(ListScreen):
|
||||
BINDINGS = [
|
||||
("q", "quit", "Quit"),
|
||||
("s", "sort", "Sort"),
|
||||
("r", "refresh", "Refresh"),
|
||||
("/", "filter", "Search"),
|
||||
@ -133,7 +133,6 @@ class ActivitiesScreen(ListScreen):
|
||||
|
||||
class CategoriesScreen(ListScreen):
|
||||
BINDINGS = [
|
||||
("q", "quit", "Quit"),
|
||||
("s", "sort", "Sort"),
|
||||
("r", "refresh", "Refresh"),
|
||||
("/", "filter", "Search"),
|
||||
@ -182,11 +181,66 @@ class CategoriesScreen(ListScreen):
|
||||
self.table.remove_row(row_key)
|
||||
|
||||
|
||||
class KimaiScreen(ListScreen):
|
||||
BINDINGS = [
|
||||
("s", "sort", "Sort"),
|
||||
("r", "refresh", "Refresh"),
|
||||
("g", "get", "Get data"),
|
||||
("/", "filter", "Search"),
|
||||
Binding(key="escape", action="cancelfilter", show=False),
|
||||
]
|
||||
|
||||
def _refresh(self, filter_query=None):
|
||||
self.table.clear()
|
||||
|
||||
projects = KimaiProject.list(
|
||||
self.db_manager
|
||||
)
|
||||
|
||||
self.table.add_rows(
|
||||
[
|
||||
[
|
||||
project.customer_id,
|
||||
project.customer_name,
|
||||
project.id,
|
||||
project.name,
|
||||
]
|
||||
for project in projects
|
||||
]
|
||||
)
|
||||
|
||||
self.table.sort(self.sort)
|
||||
|
||||
def action_get(self) -> None:
|
||||
api = KimaiAPI()
|
||||
|
||||
customers = KimaiAPICustomer.list(api)
|
||||
for customer in customers:
|
||||
KimaiCustomer(self.db_manager, id=customer.id, name=customer.name).save()
|
||||
|
||||
projects = KimaiAPIProject.list(api)
|
||||
for project in projects:
|
||||
KimaiProject(self.db_manager, id=project.id, name=project.name,
|
||||
customer_id=project.customer.id, customer_name="").save()
|
||||
|
||||
self._refresh()
|
||||
|
||||
def on_mount(self) -> None:
|
||||
self.table = self.query_one(DataTable)
|
||||
self.table.cursor_type = "row"
|
||||
self.columns = self.table.add_columns("customer id", "customer",
|
||||
"project id", "project")
|
||||
# self.sort = (self.columns[1], self.columns[3])
|
||||
self.sort = self.columns[1]
|
||||
self._refresh()
|
||||
|
||||
|
||||
class HamsterToolsApp(App):
|
||||
CSS_PATH = "app.tcss"
|
||||
BINDINGS = [
|
||||
("a", "switch_mode('activities')", "Activities"),
|
||||
("c", "switch_mode('categories')", "Categories"),
|
||||
("k", "switch_mode('kimai')", "Kimai"),
|
||||
("q", "quit", "Quit"),
|
||||
]
|
||||
|
||||
@ -195,7 +249,8 @@ class HamsterToolsApp(App):
|
||||
|
||||
self.MODES = {
|
||||
"categories": CategoriesScreen(self.db_manager),
|
||||
"activities": ActivitiesScreen(self.db_manager)
|
||||
"activities": ActivitiesScreen(self.db_manager),
|
||||
"kimai": KimaiScreen(self.db_manager)
|
||||
}
|
||||
|
||||
super().__init__()
|
||||
|
Reference in New Issue
Block a user