More formatting

This commit is contained in:
3wc 2023-10-29 21:56:39 +00:00
parent f7edf18391
commit 4b8df70527
2 changed files with 109 additions and 90 deletions

View File

@ -73,9 +73,7 @@ class ListScreen(Screen):
class ActivityEditScreen(ModalScreen): class ActivityEditScreen(ModalScreen):
BINDINGS = [ BINDINGS = [("escape", "cancel", "Cancel")]
("escape", "cancel", "Cancel")
]
def compose(self) -> ComposeResult: def compose(self) -> ComposeResult:
yield Grid( yield Grid(
@ -93,7 +91,7 @@ class ActivityMappingScreen(ModalScreen):
BINDINGS = [ BINDINGS = [
("ctrl+g", "global", "Toggle global"), ("ctrl+g", "global", "Toggle global"),
("ctrl+s", "save", "Save"), ("ctrl+s", "save", "Save"),
("escape", "cancel", "Cancel") ("escape", "cancel", "Cancel"),
] ]
customer_id = None customer_id = None
@ -111,12 +109,8 @@ class ActivityMappingScreen(ModalScreen):
return sorted(matches, key=lambda v: v.main.plain.startswith(value.lower())) return sorted(matches, key=lambda v: v.main.plain.startswith(value.lower()))
def _get_customers(self, input_state): def _get_customers(self, input_state):
customers = [ customers = [DropdownItem(c.name, str(c.id)) for c in KimaiCustomer.select()]
DropdownItem(c.name, str(c.id)) return ActivityMappingScreen._filter_dropdowns(customers, input_state.value)
for c in KimaiCustomer.select()
]
return ActivityMappingScreen._filter_dropdowns(customers,
input_state.value)
def _get_projects(self, input_state): def _get_projects(self, input_state):
projects = [ projects = [
@ -125,24 +119,21 @@ class ActivityMappingScreen(ModalScreen):
KimaiProject.customer_id == self.customer_id KimaiProject.customer_id == self.customer_id
) )
] ]
return ActivityMappingScreen._filter_dropdowns(projects, return ActivityMappingScreen._filter_dropdowns(projects, input_state.value)
input_state.value)
def _get_activities(self, input_state): def _get_activities(self, input_state):
activities = KimaiActivity.select() activities = KimaiActivity.select()
if self.query_one('#global').value: if self.query_one("#global").value:
activities = activities.where( activities = activities.where(
KimaiActivity.project_id.is_null(), KimaiActivity.project_id.is_null(),
) )
else: else:
activities = activities.where( activities = activities.where(KimaiActivity.project_id == self.project_id)
KimaiActivity.project_id == self.project_id
)
return ActivityMappingScreen._filter_dropdowns([ return ActivityMappingScreen._filter_dropdowns(
DropdownItem(a.name, str(a.id)) [DropdownItem(a.name, str(a.id)) for a in activities], input_state.value
for a in activities], input_state.value) )
def compose(self) -> ComposeResult: def compose(self) -> ComposeResult:
yield Vertical( yield Vertical(
@ -154,78 +145,88 @@ class ActivityMappingScreen(ModalScreen):
AutoComplete( AutoComplete(
Input(placeholder="Type to search...", id="customer"), Input(placeholder="Type to search...", id="customer"),
Dropdown(items=self._get_customers), Dropdown(items=self._get_customers),
) ),
), ),
Horizontal( Horizontal(
Label("Project"), Label("Project"),
AutoComplete( AutoComplete(
Input(placeholder="Type to search...", id='project'), Input(placeholder="Type to search...", id="project"),
Dropdown(items=self._get_projects), Dropdown(items=self._get_projects),
) ),
), ),
Horizontal( Horizontal(
Label("Activity"), Label("Activity"),
AutoComplete( AutoComplete(
Input(placeholder="Type to search...", id='activity'), Input(placeholder="Type to search...", id="activity"),
Dropdown(items=self._get_activities), Dropdown(items=self._get_activities),
) ),
), ),
Horizontal( Horizontal(
Label("Description"), Label("Description"),
Input(id='description'), Input(id="description"),
), ),
Horizontal( Horizontal(
Label("Tags"), 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): def customer_submitted(self, event):
if event.control.parent.dropdown.selected_item is not None: 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(
self.query_one('#project').focus() event.control.parent.dropdown.selected_item.left_meta
)
self.query_one("#project").focus()
@on(DescendantBlur, '#customer') @on(DescendantBlur, "#customer")
def customer_blur(self, event): def customer_blur(self, event):
if event.control.parent.dropdown.selected_item is not None: 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): def project_submitted(self, event):
if event.control.parent.dropdown.selected_item is not None: if event.control.parent.dropdown.selected_item is not None:
self.project_id = str(event.control.parent.dropdown.selected_item.left_meta) 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): def project_blur(self, event):
if event.control.parent.dropdown.selected_item is not None: if event.control.parent.dropdown.selected_item is not None:
self.project_id = str(event.control.parent.dropdown.selected_item.left_meta) 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): def activity_submitted(self, event):
if event.control.parent.dropdown.selected_item is not None: 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(
self.query_one('#activity').focus() event.control.parent.dropdown.selected_item.left_meta
)
self.query_one("#activity").focus()
@on(DescendantBlur, '#activity') @on(DescendantBlur, "#activity")
def activity_blur(self, event): def activity_blur(self, event):
if event.control.parent.dropdown.selected_item is not None: 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): 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): def action_save(self):
self.dismiss({ self.dismiss(
'kimai_customer_id': self.customer_id, {
'kimai_project_id': self.project_id, "kimai_customer_id": self.customer_id,
'kimai_activity_id': self.activity_id, "kimai_project_id": self.project_id,
'kimai_description': self.query_one('#description').value, "kimai_activity_id": self.activity_id,
'kimai_tags': self.query_one('#tags').value, "kimai_description": self.query_one("#description").value,
'global': self.query_one('#global').value, "kimai_tags": self.query_one("#tags").value,
}) "global": self.query_one("#global").value,
}
)
def action_cancel(self): def action_cancel(self):
self.dismiss(None) self.dismiss(None)
@ -365,27 +366,38 @@ class ActivityListScreen(ListScreen):
self.app.push_screen(ActivityEditScreen(), handle_edit) self.app.push_screen(ActivityEditScreen(), handle_edit)
def action_mapping(self): def action_mapping(self):
selected_activity = HamsterActivity.select( selected_activity = (
HamsterActivity, HamsterActivity.select(
fn.COALESCE(HamsterCategory.name, "None").alias("category_name"), HamsterActivity,
).join(HamsterCategory, JOIN.LEFT_OUTER).where( fn.COALESCE(HamsterCategory.name, "None").alias("category_name"),
HamsterActivity.id == self.table.get_cell_at(
Coordinate(self.table.cursor_coordinate.row, 2),
) )
).get() .join(HamsterCategory, JOIN.LEFT_OUTER)
.where(
HamsterActivity.id
== self.table.get_cell_at(
Coordinate(self.table.cursor_coordinate.row, 2),
)
)
.get()
)
def handle_mapping(mapping): def handle_mapping(mapping):
if mapping is None: if mapping is None:
return return
m = HamsterKimaiMapping.create(hamster_activity=selected_activity, **mapping) m = HamsterKimaiMapping.create(
hamster_activity=selected_activity, **mapping
)
m.save() m.save()
filter_input = self.query_one("#filter") filter_input = self.query_one("#filter")
self._refresh(filter_input.value) self._refresh(filter_input.value)
self.app.push_screen(ActivityMappingScreen( self.app.push_screen(
category=selected_activity.category_name, ActivityMappingScreen(
activity=selected_activity.name category=selected_activity.category_name,
), handle_mapping) activity=selected_activity.name,
),
handle_mapping,
)
class CategoryListScreen(ListScreen): class CategoryListScreen(ListScreen):

View File

@ -9,24 +9,24 @@ class NotFound(Exception):
class KimaiAPI(object): class KimaiAPI(object):
# temporary hardcoded config # temporary hardcoded config
KIMAI_API_URL = 'https://kimai.autonomic.zone/api' KIMAI_API_URL = "https://kimai.autonomic.zone/api"
KIMAI_USERNAME = '3wordchant' KIMAI_USERNAME = "3wordchant"
KIMAI_API_KEY = os.environ['KIMAI_API_KEY'] KIMAI_API_KEY = os.environ["KIMAI_API_KEY"]
auth_headers = { auth_headers = {"X-AUTH-USER": KIMAI_USERNAME, "X-AUTH-TOKEN": KIMAI_API_KEY}
'X-AUTH-USER': KIMAI_USERNAME,
'X-AUTH-TOKEN': KIMAI_API_KEY
}
def __init__(self): 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( 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( 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( 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): class BaseAPI(object):
@ -41,19 +41,17 @@ class Customer(BaseAPI):
@staticmethod @staticmethod
def list(api): def list(api):
return [ return [Customer(api, c["id"], c["name"]) for c in api.customers_json]
Customer(api, c['id'], c['name']) for c in api.customers_json
]
@staticmethod @staticmethod
def get_by_id(api, id): def get_by_id(api, id):
for value in api.customers_json: for value in api.customers_json:
if value['id'] == id: if value["id"] == id:
return Customer(api, value['id'], value['name']) return Customer(api, value["id"], value["name"])
raise NotFound() raise NotFound()
def __repr__(self): def __repr__(self):
return f'Customer (id={self.id}, name={self.name})' return f"Customer (id={self.id}, name={self.name})"
class Project(BaseAPI): class Project(BaseAPI):
@ -63,20 +61,25 @@ class Project(BaseAPI):
@staticmethod @staticmethod
def list(api): def list(api):
return [ 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 @staticmethod
def get_by_id(api, id, none=False): def get_by_id(api, id, none=False):
for value in api.projects_json: for value in api.projects_json:
if value['id'] == id: if value["id"] == id:
return Project(api, value['id'], value['name'], return Project(
Customer.get_by_id(api, value['customer'])) api,
value["id"],
value["name"],
Customer.get_by_id(api, value["customer"]),
)
if not none: if not none:
raise NotFound() raise NotFound()
def __repr__(self): 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): class Activity(BaseAPI):
@ -86,20 +89,24 @@ class Activity(BaseAPI):
@staticmethod @staticmethod
def list(api): def list(api):
return [ return [
Activity(api, a['id'], a['name'], Project.get_by_id(api, Activity(
a['project'], api, a["id"], a["name"], Project.get_by_id(api, a["project"], none=True)
none=True)) )
for a in api.activities_json for a in api.activities_json
] ]
@staticmethod @staticmethod
def get_by_id(api, id, none=False): def get_by_id(api, id, none=False):
for value in api.activities_json: for value in api.activities_json:
if value['id'] == id: if value["id"] == id:
return Activity(api, value['id'], value['name'], return Activity(
Project.get_by_id(api, value['project'])) api,
value["id"],
value["name"],
Project.get_by_id(api, value["project"]),
)
if not none: if not none:
raise NotFound() raise NotFound()
def __repr__(self): 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})"