Hot damn working date filtering

This commit is contained in:
3wc 2023-10-29 23:12:39 +00:00
parent 8e2eead540
commit 0c58555b43
2 changed files with 59 additions and 24 deletions

View File

@ -1,3 +1,5 @@
from datetime import datetime
from textual import on from textual import on
from textual.app import App, ComposeResult from textual.app import App, ComposeResult
from textual.binding import Binding from textual.binding import Binding
@ -33,12 +35,13 @@ from .kimai import (
class ListScreen(Screen): class ListScreen(Screen):
def compose(self) -> ComposeResult: def compose(self) -> ComposeResult:
"""create child widgets for the app."""
yield Header() yield Header()
with Vertical(): with Vertical():
yield DataTable() yield DataTable()
with Horizontal(): with Horizontal(id="filter"):
yield Input(id="filter") yield Input(id="search", placeholder="Category/activity name contains text")
yield Input(id="date",
placeholder='After date, in {0} format'.format(datetime.now().strftime('%Y-%m-%d')))
yield Footer() yield Footer()
def action_refresh(self) -> None: def action_refresh(self) -> None:
@ -53,23 +56,24 @@ class ListScreen(Screen):
event.data_table.cursor_type = "row" event.data_table.cursor_type = "row"
def action_filter(self) -> None: def action_filter(self) -> None:
filter_input = self.query_one("#filter") self.query_one("#filter").display = True
filter_input.display = True self._refresh()
self._refresh(filter_input.value) self.query_one("#filter #search").focus()
filter_input.focus()
def on_input_submitted(self, event): def on_input_submitted(self, event):
self.table.focus() self.table.focus()
def action_cancelfilter(self) -> None: def action_cancelfilter(self) -> None:
filter_input = self.query_one("#filter") self.query_one("#filter").display = False
filter_input.display = False self.query_one("#filter #search").clear()
filter_input.clear() self.query_one("#filter #date").clear()
self.table.focus() self.table.focus()
self._refresh() self._refresh()
def on_input_changed(self, event): @on(Input.Changed, '#filter Input')
self._refresh(event.value) def filter(self, event):
self._refresh()
class ActivityEditScreen(ModalScreen): class ActivityEditScreen(ModalScreen):
@ -244,7 +248,7 @@ class ActivityListScreen(ListScreen):
Binding(key="escape", action="cancelfilter", show=False), Binding(key="escape", action="cancelfilter", show=False),
] ]
def _refresh(self, filter_query=None): def _refresh(self):
self.table.clear() self.table.clear()
facts_count_query = ( facts_count_query = (
@ -268,6 +272,7 @@ class ActivityListScreen(ListScreen):
HamsterActivity.select( HamsterActivity.select(
HamsterActivity, HamsterActivity,
HamsterCategory.id, HamsterCategory.id,
HamsterFact.start_time,
fn.COALESCE(HamsterCategory.name, "None").alias("category_name"), fn.COALESCE(HamsterCategory.name, "None").alias("category_name"),
fn.COALESCE(facts_count_query.c.facts_count, 0).alias("facts_count"), fn.COALESCE(facts_count_query.c.facts_count, 0).alias("facts_count"),
fn.COALESCE(mappings_count_query.c.mappings_count, 0).alias( fn.COALESCE(mappings_count_query.c.mappings_count, 0).alias(
@ -276,6 +281,8 @@ class ActivityListScreen(ListScreen):
) )
.join(HamsterCategory, JOIN.LEFT_OUTER) .join(HamsterCategory, JOIN.LEFT_OUTER)
.switch(HamsterActivity) .switch(HamsterActivity)
.join(HamsterFact, JOIN.LEFT_OUTER)
.switch(HamsterActivity)
.join( .join(
facts_count_query, facts_count_query,
JOIN.LEFT_OUTER, JOIN.LEFT_OUTER,
@ -290,12 +297,22 @@ class ActivityListScreen(ListScreen):
.group_by(HamsterActivity) .group_by(HamsterActivity)
) )
if filter_query: filter_search = self.query_one('#filter #search').value
if filter_search is not None:
activities = activities.where( activities = activities.where(
HamsterActivity.name.contains(filter_query) HamsterActivity.name.contains(filter_search)
| HamsterCategory.name.contains(filter_query) | HamsterCategory.name.contains(filter_search)
) )
filter_date = self.query_one('#filter #date').value
if filter_date is not None:
try:
activities = activities.where(
HamsterFact.start_time > datetime.strptime(filter_date, '%Y-%m-%d')
)
except ValueError:
pass
self.table.add_rows( self.table.add_rows(
[ [
[ [
@ -355,8 +372,7 @@ class ActivityListScreen(ListScreen):
HamsterFact.update({HamsterFact.activity: move_to_activity}).where( HamsterFact.update({HamsterFact.activity: move_to_activity}).where(
HamsterFact.activity == self.move_from_activity HamsterFact.activity == self.move_from_activity
).execute() ).execute()
filter_input = self.query_one("#filter") self._refresh()
self._refresh(filter_input.value)
del self.move_from_activity del self.move_from_activity
def action_edit(self): def action_edit(self):
@ -388,8 +404,8 @@ class ActivityListScreen(ListScreen):
hamster_activity=selected_activity, **mapping hamster_activity=selected_activity, **mapping
) )
m.save() m.save()
filter_input = self.query_one("#filter") filter_search = self.query_one("#search")
self._refresh(filter_input.value) self._refresh()
self.app.push_screen( self.app.push_screen(
ActivityMappingScreen( ActivityMappingScreen(
@ -409,19 +425,34 @@ class CategoryListScreen(ListScreen):
Binding(key="escape", action="cancelfilter", show=False), Binding(key="escape", action="cancelfilter", show=False),
] ]
def _refresh(self, filter_query=None): def _refresh(self):
self.table.clear() self.table.clear()
categories = ( categories = (
HamsterCategory.select( HamsterCategory.select(
HamsterCategory, fn.Count(HamsterActivity.id).alias("activities_count") HamsterCategory,
fn.Count(HamsterActivity.id).alias("activities_count"),
HamsterFact.start_time
) )
.join(HamsterActivity, JOIN.LEFT_OUTER) .join(HamsterActivity, JOIN.LEFT_OUTER)
.join(HamsterFact, JOIN.LEFT_OUTER)
.group_by(HamsterCategory) .group_by(HamsterCategory)
) )
if filter_query: filter_search = self.query_one('#filter #search').value
categories = categories.where(HamsterCategory.name.contains(filter_query)) if filter_search is not None:
categories = categories.where(
HamsterCategory.name.contains(filter_search)
)
filter_date = self.query_one('#filter #date').value
if filter_date is not None:
try:
categories = categories.where(
HamsterFact.start_time > datetime.strptime(filter_date, '%Y-%m-%d')
)
except ValueError:
pass
self.table.add_rows( self.table.add_rows(
[ [

View File

@ -14,6 +14,10 @@ DataTable:focus .datatable--cursor {
display: none; display: none;
} }
#filter Input {
width: 50%;
}
ActivityEditScreen, ActivityMappingScreen { ActivityEditScreen, ActivityMappingScreen {
align: center middle; align: center middle;
} }