Show kimai activity counts, clear data before get

This commit is contained in:
3wc 2023-10-28 01:21:23 +01:00
parent cd278b32aa
commit c68e373b18
3 changed files with 45 additions and 8 deletions

View File

@ -240,6 +240,7 @@ class KimaiScreen(ListScreen):
.join(KimaiCustomer, JOIN.LEFT_OUTER) .join(KimaiCustomer, JOIN.LEFT_OUTER)
.switch(KimaiProject) .switch(KimaiProject)
.join(KimaiActivity, JOIN.LEFT_OUTER) .join(KimaiActivity, JOIN.LEFT_OUTER)
.where(KimaiActivity.project.is_null(False))
.group_by(KimaiProject) .group_by(KimaiProject)
) )
@ -249,7 +250,6 @@ class KimaiScreen(ListScreen):
| KimaiCustomer.name.contains(filter_query) | KimaiCustomer.name.contains(filter_query)
) )
self.table.add_rows( self.table.add_rows(
[ [
[ [
@ -268,6 +268,10 @@ class KimaiScreen(ListScreen):
def action_get(self) -> None: def action_get(self) -> None:
api = KimaiAPI() api = KimaiAPI()
KimaiCustomer.delete().execute()
KimaiProject.delete().execute()
KimaiActivity.delete().execute()
customers = KimaiAPICustomer.list(api) customers = KimaiAPICustomer.list(api)
with db.atomic(): with db.atomic():
KimaiCustomer.insert_many([{ KimaiCustomer.insert_many([{
@ -283,6 +287,14 @@ class KimaiScreen(ListScreen):
'customer_id': project.customer.id 'customer_id': project.customer.id
} for project in projects]).execute() } 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() self._refresh()
def on_mount(self) -> None: def on_mount(self) -> None:

View File

@ -1,5 +1,5 @@
import logging import logging
from peewee import SqliteDatabase, Model, CharField, DateField, ForeignKeyField from peewee import SqliteDatabase, Model, CharField, ForeignKeyField
from textual.logging import TextualHandler from textual.logging import TextualHandler

View File

@ -1,5 +1,5 @@
import requests import requests
# import requests_cache import requests_cache
import os import os
@ -20,11 +20,13 @@ class KimaiAPI(object):
} }
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(
f'{self.KIMAI_API_URL}/activities?visible=3', headers=self.auth_headers).json()
class BaseAPI(object): class BaseAPI(object):
@ -65,16 +67,39 @@ class Project(BaseAPI):
] ]
@staticmethod @staticmethod
def get_by_id(api, id): 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(api, value['id'], value['name'],
Customer.get_by_id(api, value['customer'])) Customer.get_by_id(api, value['customer']))
raise NotFound() if not none:
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(): class Activity(BaseAPI):
pass 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})'