From c68e373b188a61a253fdf17033e8017abc29d3f2 Mon Sep 17 00:00:00 2001 From: 3wc <3wc@doesthisthing.work> Date: Sat, 28 Oct 2023 01:21:23 +0100 Subject: [PATCH] Show kimai activity counts, clear data before get --- hamstertools/app.py | 14 +++++++++++++- hamstertools/db.py | 2 +- hamstertools/kimai.py | 37 +++++++++++++++++++++++++++++++------ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/hamstertools/app.py b/hamstertools/app.py index b87d7a7..5e64149 100644 --- a/hamstertools/app.py +++ b/hamstertools/app.py @@ -240,6 +240,7 @@ class KimaiScreen(ListScreen): .join(KimaiCustomer, JOIN.LEFT_OUTER) .switch(KimaiProject) .join(KimaiActivity, JOIN.LEFT_OUTER) + .where(KimaiActivity.project.is_null(False)) .group_by(KimaiProject) ) @@ -249,7 +250,6 @@ class KimaiScreen(ListScreen): | KimaiCustomer.name.contains(filter_query) ) - self.table.add_rows( [ [ @@ -268,6 +268,10 @@ class KimaiScreen(ListScreen): def action_get(self) -> None: api = KimaiAPI() + KimaiCustomer.delete().execute() + KimaiProject.delete().execute() + KimaiActivity.delete().execute() + customers = KimaiAPICustomer.list(api) with db.atomic(): KimaiCustomer.insert_many([{ @@ -283,6 +287,14 @@ class KimaiScreen(ListScreen): 'customer_id': project.customer.id } for project in projects]).execute() + activities = KimaiAPIActivity.list(api) + with db.atomic(): + KimaiActivity.insert_many([{ + 'id': activity.id, + 'name': activity.name, + 'project_id': (activity.project and activity.project.id or None) + } for activity in activities]).execute() + self._refresh() def on_mount(self) -> None: diff --git a/hamstertools/db.py b/hamstertools/db.py index 336e47b..fad193b 100644 --- a/hamstertools/db.py +++ b/hamstertools/db.py @@ -1,5 +1,5 @@ import logging -from peewee import SqliteDatabase, Model, CharField, DateField, ForeignKeyField +from peewee import SqliteDatabase, Model, CharField, ForeignKeyField from textual.logging import TextualHandler diff --git a/hamstertools/kimai.py b/hamstertools/kimai.py index 19d5abd..8c0f51d 100644 --- a/hamstertools/kimai.py +++ b/hamstertools/kimai.py @@ -1,5 +1,5 @@ import requests -# import requests_cache +import requests_cache import os @@ -20,11 +20,13 @@ class KimaiAPI(object): } 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() self.projects_json = requests.get( 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() class BaseAPI(object): @@ -65,16 +67,39 @@ class Project(BaseAPI): ] @staticmethod - def get_by_id(api, id): + 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'])) - raise NotFound() + if not none: + raise NotFound() def __repr__(self): return f'Project (id={self.id}, name={self.name}, customer={self.customer})' -class Activity(): - pass +class Activity(BaseAPI): + def __init__(self, api, id, name, project): + super().__init__(api, id=id, name=name, project=project) + + @staticmethod + def list(api): + return [ + 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 not none: + raise NotFound() + + def __repr__(self): + return f'Activity (id={self.id}, name={self.name}, project={self.project})'