Compare commits
No commits in common. "0c58555b43e1fc80037ee24465c82d484a1c8f71" and "f7edf1839172aa30d4b5228aba0cdcab9a064248" have entirely different histories.
0c58555b43
...
f7edf18391
@ -1,5 +1,3 @@
|
||||
from datetime import datetime
|
||||
|
||||
from textual import on
|
||||
from textual.app import App, ComposeResult
|
||||
from textual.binding import Binding
|
||||
@ -35,13 +33,12 @@ from .kimai import (
|
||||
|
||||
class ListScreen(Screen):
|
||||
def compose(self) -> ComposeResult:
|
||||
"""create child widgets for the app."""
|
||||
yield Header()
|
||||
with Vertical():
|
||||
yield DataTable()
|
||||
with Horizontal(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')))
|
||||
with Horizontal():
|
||||
yield Input(id="filter")
|
||||
yield Footer()
|
||||
|
||||
def action_refresh(self) -> None:
|
||||
@ -56,28 +53,29 @@ class ListScreen(Screen):
|
||||
event.data_table.cursor_type = "row"
|
||||
|
||||
def action_filter(self) -> None:
|
||||
self.query_one("#filter").display = True
|
||||
self._refresh()
|
||||
self.query_one("#filter #search").focus()
|
||||
filter_input = self.query_one("#filter")
|
||||
filter_input.display = True
|
||||
self._refresh(filter_input.value)
|
||||
filter_input.focus()
|
||||
|
||||
def on_input_submitted(self, event):
|
||||
self.table.focus()
|
||||
|
||||
def action_cancelfilter(self) -> None:
|
||||
self.query_one("#filter").display = False
|
||||
self.query_one("#filter #search").clear()
|
||||
self.query_one("#filter #date").clear()
|
||||
filter_input = self.query_one("#filter")
|
||||
filter_input.display = False
|
||||
filter_input.clear()
|
||||
self.table.focus()
|
||||
self._refresh()
|
||||
|
||||
@on(Input.Changed, '#filter Input')
|
||||
def filter(self, event):
|
||||
self._refresh()
|
||||
|
||||
def on_input_changed(self, event):
|
||||
self._refresh(event.value)
|
||||
|
||||
|
||||
class ActivityEditScreen(ModalScreen):
|
||||
BINDINGS = [("escape", "cancel", "Cancel")]
|
||||
BINDINGS = [
|
||||
("escape", "cancel", "Cancel")
|
||||
]
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Grid(
|
||||
@ -95,7 +93,7 @@ class ActivityMappingScreen(ModalScreen):
|
||||
BINDINGS = [
|
||||
("ctrl+g", "global", "Toggle global"),
|
||||
("ctrl+s", "save", "Save"),
|
||||
("escape", "cancel", "Cancel"),
|
||||
("escape", "cancel", "Cancel")
|
||||
]
|
||||
|
||||
customer_id = None
|
||||
@ -113,8 +111,12 @@ class ActivityMappingScreen(ModalScreen):
|
||||
return sorted(matches, key=lambda v: v.main.plain.startswith(value.lower()))
|
||||
|
||||
def _get_customers(self, input_state):
|
||||
customers = [DropdownItem(c.name, str(c.id)) for c in KimaiCustomer.select()]
|
||||
return ActivityMappingScreen._filter_dropdowns(customers, input_state.value)
|
||||
customers = [
|
||||
DropdownItem(c.name, str(c.id))
|
||||
for c in KimaiCustomer.select()
|
||||
]
|
||||
return ActivityMappingScreen._filter_dropdowns(customers,
|
||||
input_state.value)
|
||||
|
||||
def _get_projects(self, input_state):
|
||||
projects = [
|
||||
@ -123,22 +125,25 @@ class ActivityMappingScreen(ModalScreen):
|
||||
KimaiProject.customer_id == self.customer_id
|
||||
)
|
||||
]
|
||||
return ActivityMappingScreen._filter_dropdowns(projects, input_state.value)
|
||||
return ActivityMappingScreen._filter_dropdowns(projects,
|
||||
input_state.value)
|
||||
|
||||
def _get_activities(self, input_state):
|
||||
activities = KimaiActivity.select()
|
||||
|
||||
if self.query_one("#global").value:
|
||||
if self.query_one('#global').value:
|
||||
activities = activities.where(
|
||||
KimaiActivity.project_id.is_null(),
|
||||
)
|
||||
else:
|
||||
activities = activities.where(KimaiActivity.project_id == self.project_id)
|
||||
|
||||
return ActivityMappingScreen._filter_dropdowns(
|
||||
[DropdownItem(a.name, str(a.id)) for a in activities], input_state.value
|
||||
activities = activities.where(
|
||||
KimaiActivity.project_id == self.project_id
|
||||
)
|
||||
|
||||
return ActivityMappingScreen._filter_dropdowns([
|
||||
DropdownItem(a.name, str(a.id))
|
||||
for a in activities], input_state.value)
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Vertical(
|
||||
Horizontal(
|
||||
@ -149,88 +154,78 @@ class ActivityMappingScreen(ModalScreen):
|
||||
AutoComplete(
|
||||
Input(placeholder="Type to search...", id="customer"),
|
||||
Dropdown(items=self._get_customers),
|
||||
),
|
||||
)
|
||||
),
|
||||
Horizontal(
|
||||
Label("Project"),
|
||||
AutoComplete(
|
||||
Input(placeholder="Type to search...", id="project"),
|
||||
Input(placeholder="Type to search...", id='project'),
|
||||
Dropdown(items=self._get_projects),
|
||||
),
|
||||
)
|
||||
),
|
||||
Horizontal(
|
||||
Label("Activity"),
|
||||
AutoComplete(
|
||||
Input(placeholder="Type to search...", id="activity"),
|
||||
Input(placeholder="Type to search...", id='activity'),
|
||||
Dropdown(items=self._get_activities),
|
||||
),
|
||||
)
|
||||
),
|
||||
Horizontal(
|
||||
Label("Description"),
|
||||
Input(id="description"),
|
||||
Input(id='description'),
|
||||
),
|
||||
Horizontal(
|
||||
Label("Tags"),
|
||||
Input(id="tags"),
|
||||
Input(id='tags'),
|
||||
),
|
||||
Horizontal(Checkbox("Global", id="global")),
|
||||
Horizontal(Checkbox("Global", id='global')),
|
||||
)
|
||||
|
||||
@on(Input.Submitted, "#customer")
|
||||
@on(Input.Submitted, '#customer')
|
||||
def customer_submitted(self, event):
|
||||
if event.control.parent.dropdown.selected_item is not None:
|
||||
self.customer_id = str(
|
||||
event.control.parent.dropdown.selected_item.left_meta
|
||||
)
|
||||
self.query_one("#project").focus()
|
||||
self.customer_id = str(event.control.parent.dropdown.selected_item.left_meta)
|
||||
self.query_one('#project').focus()
|
||||
|
||||
@on(DescendantBlur, "#customer")
|
||||
@on(DescendantBlur, '#customer')
|
||||
def customer_blur(self, event):
|
||||
if event.control.parent.dropdown.selected_item is not None:
|
||||
self.customer_id = str(
|
||||
event.control.parent.dropdown.selected_item.left_meta
|
||||
)
|
||||
self.customer_id = str(event.control.parent.dropdown.selected_item.left_meta)
|
||||
|
||||
@on(Input.Submitted, "#project")
|
||||
@on(Input.Submitted, '#project')
|
||||
def project_submitted(self, event):
|
||||
if event.control.parent.dropdown.selected_item is not None:
|
||||
self.project_id = str(event.control.parent.dropdown.selected_item.left_meta)
|
||||
self.query_one("#activity").focus()
|
||||
self.query_one('#activity').focus()
|
||||
|
||||
@on(DescendantBlur, "#project")
|
||||
@on(DescendantBlur, '#project')
|
||||
def project_blur(self, event):
|
||||
if event.control.parent.dropdown.selected_item is not None:
|
||||
self.project_id = str(event.control.parent.dropdown.selected_item.left_meta)
|
||||
|
||||
@on(Input.Submitted, "#activity")
|
||||
@on(Input.Submitted, '#activity')
|
||||
def activity_submitted(self, event):
|
||||
if event.control.parent.dropdown.selected_item is not None:
|
||||
self.activity_id = str(
|
||||
event.control.parent.dropdown.selected_item.left_meta
|
||||
)
|
||||
self.query_one("#activity").focus()
|
||||
self.activity_id = str(event.control.parent.dropdown.selected_item.left_meta)
|
||||
self.query_one('#activity').focus()
|
||||
|
||||
@on(DescendantBlur, "#activity")
|
||||
@on(DescendantBlur, '#activity')
|
||||
def activity_blur(self, event):
|
||||
if event.control.parent.dropdown.selected_item is not None:
|
||||
self.activity_id = str(
|
||||
event.control.parent.dropdown.selected_item.left_meta
|
||||
)
|
||||
self.activity_id = str(event.control.parent.dropdown.selected_item.left_meta)
|
||||
|
||||
def action_global(self):
|
||||
self.query_one("#global").value = not self.query_one("#global").value
|
||||
self.query_one('#global').value = not self.query_one('#global').value
|
||||
|
||||
def action_save(self):
|
||||
self.dismiss(
|
||||
{
|
||||
"kimai_customer_id": self.customer_id,
|
||||
"kimai_project_id": self.project_id,
|
||||
"kimai_activity_id": self.activity_id,
|
||||
"kimai_description": self.query_one("#description").value,
|
||||
"kimai_tags": self.query_one("#tags").value,
|
||||
"global": self.query_one("#global").value,
|
||||
}
|
||||
)
|
||||
self.dismiss({
|
||||
'kimai_customer_id': self.customer_id,
|
||||
'kimai_project_id': self.project_id,
|
||||
'kimai_activity_id': self.activity_id,
|
||||
'kimai_description': self.query_one('#description').value,
|
||||
'kimai_tags': self.query_one('#tags').value,
|
||||
'global': self.query_one('#global').value,
|
||||
})
|
||||
|
||||
def action_cancel(self):
|
||||
self.dismiss(None)
|
||||
@ -248,7 +243,7 @@ class ActivityListScreen(ListScreen):
|
||||
Binding(key="escape", action="cancelfilter", show=False),
|
||||
]
|
||||
|
||||
def _refresh(self):
|
||||
def _refresh(self, filter_query=None):
|
||||
self.table.clear()
|
||||
|
||||
facts_count_query = (
|
||||
@ -272,7 +267,6 @@ class ActivityListScreen(ListScreen):
|
||||
HamsterActivity.select(
|
||||
HamsterActivity,
|
||||
HamsterCategory.id,
|
||||
HamsterFact.start_time,
|
||||
fn.COALESCE(HamsterCategory.name, "None").alias("category_name"),
|
||||
fn.COALESCE(facts_count_query.c.facts_count, 0).alias("facts_count"),
|
||||
fn.COALESCE(mappings_count_query.c.mappings_count, 0).alias(
|
||||
@ -281,8 +275,6 @@ class ActivityListScreen(ListScreen):
|
||||
)
|
||||
.join(HamsterCategory, JOIN.LEFT_OUTER)
|
||||
.switch(HamsterActivity)
|
||||
.join(HamsterFact, JOIN.LEFT_OUTER)
|
||||
.switch(HamsterActivity)
|
||||
.join(
|
||||
facts_count_query,
|
||||
JOIN.LEFT_OUTER,
|
||||
@ -297,22 +289,12 @@ class ActivityListScreen(ListScreen):
|
||||
.group_by(HamsterActivity)
|
||||
)
|
||||
|
||||
filter_search = self.query_one('#filter #search').value
|
||||
if filter_search is not None:
|
||||
if filter_query:
|
||||
activities = activities.where(
|
||||
HamsterActivity.name.contains(filter_search)
|
||||
| HamsterCategory.name.contains(filter_search)
|
||||
HamsterActivity.name.contains(filter_query)
|
||||
| HamsterCategory.name.contains(filter_query)
|
||||
)
|
||||
|
||||
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(
|
||||
[
|
||||
[
|
||||
@ -372,7 +354,8 @@ class ActivityListScreen(ListScreen):
|
||||
HamsterFact.update({HamsterFact.activity: move_to_activity}).where(
|
||||
HamsterFact.activity == self.move_from_activity
|
||||
).execute()
|
||||
self._refresh()
|
||||
filter_input = self.query_one("#filter")
|
||||
self._refresh(filter_input.value)
|
||||
del self.move_from_activity
|
||||
|
||||
def action_edit(self):
|
||||
@ -382,38 +365,27 @@ class ActivityListScreen(ListScreen):
|
||||
self.app.push_screen(ActivityEditScreen(), handle_edit)
|
||||
|
||||
def action_mapping(self):
|
||||
selected_activity = (
|
||||
HamsterActivity.select(
|
||||
selected_activity = HamsterActivity.select(
|
||||
HamsterActivity,
|
||||
fn.COALESCE(HamsterCategory.name, "None").alias("category_name"),
|
||||
)
|
||||
.join(HamsterCategory, JOIN.LEFT_OUTER)
|
||||
.where(
|
||||
HamsterActivity.id
|
||||
== self.table.get_cell_at(
|
||||
).join(HamsterCategory, JOIN.LEFT_OUTER).where(
|
||||
HamsterActivity.id == self.table.get_cell_at(
|
||||
Coordinate(self.table.cursor_coordinate.row, 2),
|
||||
)
|
||||
)
|
||||
.get()
|
||||
)
|
||||
).get()
|
||||
|
||||
def handle_mapping(mapping):
|
||||
if mapping is None:
|
||||
return
|
||||
m = HamsterKimaiMapping.create(
|
||||
hamster_activity=selected_activity, **mapping
|
||||
)
|
||||
m = HamsterKimaiMapping.create(hamster_activity=selected_activity, **mapping)
|
||||
m.save()
|
||||
filter_search = self.query_one("#search")
|
||||
self._refresh()
|
||||
filter_input = self.query_one("#filter")
|
||||
self._refresh(filter_input.value)
|
||||
|
||||
self.app.push_screen(
|
||||
ActivityMappingScreen(
|
||||
self.app.push_screen(ActivityMappingScreen(
|
||||
category=selected_activity.category_name,
|
||||
activity=selected_activity.name,
|
||||
),
|
||||
handle_mapping,
|
||||
)
|
||||
activity=selected_activity.name
|
||||
), handle_mapping)
|
||||
|
||||
|
||||
class CategoryListScreen(ListScreen):
|
||||
@ -425,34 +397,19 @@ class CategoryListScreen(ListScreen):
|
||||
Binding(key="escape", action="cancelfilter", show=False),
|
||||
]
|
||||
|
||||
def _refresh(self):
|
||||
def _refresh(self, filter_query=None):
|
||||
self.table.clear()
|
||||
|
||||
categories = (
|
||||
HamsterCategory.select(
|
||||
HamsterCategory,
|
||||
fn.Count(HamsterActivity.id).alias("activities_count"),
|
||||
HamsterFact.start_time
|
||||
HamsterCategory, fn.Count(HamsterActivity.id).alias("activities_count")
|
||||
)
|
||||
.join(HamsterActivity, JOIN.LEFT_OUTER)
|
||||
.join(HamsterFact, JOIN.LEFT_OUTER)
|
||||
.group_by(HamsterCategory)
|
||||
)
|
||||
|
||||
filter_search = self.query_one('#filter #search').value
|
||||
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
|
||||
if filter_query:
|
||||
categories = categories.where(HamsterCategory.name.contains(filter_query))
|
||||
|
||||
self.table.add_rows(
|
||||
[
|
||||
@ -475,6 +432,7 @@ class CategoryListScreen(ListScreen):
|
||||
self._refresh()
|
||||
|
||||
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(
|
||||
@ -483,6 +441,7 @@ class CategoryListScreen(ListScreen):
|
||||
category = HamsterCategory.get(id=category_id)
|
||||
category.delete_instance()
|
||||
|
||||
# supply the row key to `remove_row` to delete the row.
|
||||
self.table.remove_row(row_key)
|
||||
|
||||
|
||||
@ -610,5 +569,5 @@ class HamsterToolsApp(App):
|
||||
self.switch_mode("activities")
|
||||
|
||||
def action_quit(self) -> None:
|
||||
db.close()
|
||||
self.exit()
|
||||
db.close()
|
||||
|
@ -14,10 +14,6 @@ DataTable:focus .datatable--cursor {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#filter Input {
|
||||
width: 50%;
|
||||
}
|
||||
|
||||
ActivityEditScreen, ActivityMappingScreen {
|
||||
align: center middle;
|
||||
}
|
||||
|
@ -9,24 +9,24 @@ class NotFound(Exception):
|
||||
|
||||
class KimaiAPI(object):
|
||||
# temporary hardcoded config
|
||||
KIMAI_API_URL = "https://kimai.autonomic.zone/api"
|
||||
KIMAI_API_URL = 'https://kimai.autonomic.zone/api'
|
||||
|
||||
KIMAI_USERNAME = "3wordchant"
|
||||
KIMAI_API_KEY = os.environ["KIMAI_API_KEY"]
|
||||
KIMAI_USERNAME = '3wordchant'
|
||||
KIMAI_API_KEY = os.environ['KIMAI_API_KEY']
|
||||
|
||||
auth_headers = {"X-AUTH-USER": KIMAI_USERNAME, "X-AUTH-TOKEN": KIMAI_API_KEY}
|
||||
auth_headers = {
|
||||
'X-AUTH-USER': KIMAI_USERNAME,
|
||||
'X-AUTH-TOKEN': KIMAI_API_KEY
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
requests_cache.install_cache("kimai", backend="sqlite", expire_after=1800)
|
||||
requests_cache.install_cache('kimai', backend='sqlite', expire_after=1800)
|
||||
self.customers_json = requests.get(
|
||||
f"{self.KIMAI_API_URL}/customers?visible=3", headers=self.auth_headers
|
||||
).json()
|
||||
f'{self.KIMAI_API_URL}/customers?visible=3', headers=self.auth_headers).json()
|
||||
self.projects_json = requests.get(
|
||||
f"{self.KIMAI_API_URL}/projects?visible=3", headers=self.auth_headers
|
||||
).json()
|
||||
f'{self.KIMAI_API_URL}/projects?visible=3', headers=self.auth_headers).json()
|
||||
self.activities_json = requests.get(
|
||||
f"{self.KIMAI_API_URL}/activities?visible=3", headers=self.auth_headers
|
||||
).json()
|
||||
f'{self.KIMAI_API_URL}/activities?visible=3', headers=self.auth_headers).json()
|
||||
|
||||
|
||||
class BaseAPI(object):
|
||||
@ -41,17 +41,19 @@ class Customer(BaseAPI):
|
||||
|
||||
@staticmethod
|
||||
def list(api):
|
||||
return [Customer(api, c["id"], c["name"]) for c in api.customers_json]
|
||||
return [
|
||||
Customer(api, c['id'], c['name']) for c in api.customers_json
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def get_by_id(api, id):
|
||||
for value in api.customers_json:
|
||||
if value["id"] == id:
|
||||
return Customer(api, value["id"], value["name"])
|
||||
if value['id'] == id:
|
||||
return Customer(api, value['id'], value['name'])
|
||||
raise NotFound()
|
||||
|
||||
def __repr__(self):
|
||||
return f"Customer (id={self.id}, name={self.name})"
|
||||
return f'Customer (id={self.id}, name={self.name})'
|
||||
|
||||
|
||||
class Project(BaseAPI):
|
||||
@ -61,25 +63,20 @@ class Project(BaseAPI):
|
||||
@staticmethod
|
||||
def list(api):
|
||||
return [
|
||||
Project(api, p["id"], p["name"], Customer.get_by_id(api, p["customer"]))
|
||||
for p in api.projects_json
|
||||
Project(api, p['id'], p['name'], Customer.get_by_id(api, p['customer'])) for p in api.projects_json
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def get_by_id(api, id, none=False):
|
||||
for value in api.projects_json:
|
||||
if value["id"] == id:
|
||||
return Project(
|
||||
api,
|
||||
value["id"],
|
||||
value["name"],
|
||||
Customer.get_by_id(api, value["customer"]),
|
||||
)
|
||||
if value['id'] == id:
|
||||
return Project(api, value['id'], value['name'],
|
||||
Customer.get_by_id(api, value['customer']))
|
||||
if not none:
|
||||
raise NotFound()
|
||||
|
||||
def __repr__(self):
|
||||
return f"Project (id={self.id}, name={self.name}, customer={self.customer})"
|
||||
return f'Project (id={self.id}, name={self.name}, customer={self.customer})'
|
||||
|
||||
|
||||
class Activity(BaseAPI):
|
||||
@ -89,24 +86,20 @@ class Activity(BaseAPI):
|
||||
@staticmethod
|
||||
def list(api):
|
||||
return [
|
||||
Activity(
|
||||
api, a["id"], a["name"], Project.get_by_id(api, a["project"], none=True)
|
||||
)
|
||||
Activity(api, a['id'], a['name'], Project.get_by_id(api,
|
||||
a['project'],
|
||||
none=True))
|
||||
for a in api.activities_json
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def get_by_id(api, id, none=False):
|
||||
for value in api.activities_json:
|
||||
if value["id"] == id:
|
||||
return Activity(
|
||||
api,
|
||||
value["id"],
|
||||
value["name"],
|
||||
Project.get_by_id(api, value["project"]),
|
||||
)
|
||||
if value['id'] == id:
|
||||
return Activity(api, value['id'], value['name'],
|
||||
Project.get_by_id(api, value['project']))
|
||||
if not none:
|
||||
raise NotFound()
|
||||
|
||||
def __repr__(self):
|
||||
return f"Activity (id={self.id}, name={self.name}, project={self.project})"
|
||||
return f'Activity (id={self.id}, name={self.name}, project={self.project})'
|
||||
|
Loading…
Reference in New Issue
Block a user