diff --git a/hamstertools/__init__.py b/hamstertools/__init__.py index 3d5672f..1d91ba2 100755 --- a/hamstertools/__init__.py +++ b/hamstertools/__init__.py @@ -22,6 +22,7 @@ from .db import ( HamsterActivityKimaiMapping, HamsterFactKimaiImport, ) +from .sync import sync HAMSTER_DIR = Path.home() / ".local/share/hamster" # HAMSTER_FILE = HAMSTER_DIR / 'hamster.db' @@ -34,9 +35,13 @@ db.init(HAMSTER_FILE) @click.option("-d", "--debug", is_flag=True) def cli(debug): if debug: + logging.basicConfig() + logging.getLogger().setLevel(logging.DEBUG) + peewee_logger = logging.getLogger("peewee") peewee_logger.addHandler(TextualHandler()) peewee_logger.setLevel(logging.DEBUG) + requests_log = logging.getLogger("requests.packages.urllib3") requests_log.setLevel(logging.DEBUG) requests_log.propagate = True @@ -342,7 +347,7 @@ def _get_kimai_mapping_file(path, category_search=None): return open(path) -@kimai.command() +@kimai.command("sync") @click.option( "--mapping-path", help="Mapping file (default ~/.local/share/hamster/mapping.kimai.csv)", @@ -352,9 +357,9 @@ def _get_kimai_mapping_file(path, category_search=None): @click.argument("api_key") @click.option("--just-errors", "just_errors", is_flag=True, help="Only display errors") @click.option("--ignore-activities", is_flag=True, help="Ignore missing activities") -def sync(username, api_key, just_errors, ignore_activities, mapping_path=None): +def check(username, api_key, just_errors, ignore_activities, mapping_path=None): """ - Download customer / project / activity data from Kimai + Check customer / project / activity data from Kimai """ kimai_api_url = "https://kimai.autonomic.zone/api" @@ -656,6 +661,11 @@ def reset(): HamsterActivityKimaiMapping.delete().execute() +@db_.command("sync") +def kimai_db_sync(): + sync() + + @db_.command() @click.option( "-g", diff --git a/hamstertools/app.py b/hamstertools/app.py index 7c956c6..ea41aa6 100644 --- a/hamstertools/app.py +++ b/hamstertools/app.py @@ -30,6 +30,7 @@ from .kimai import ( Project as KimaiAPIProject, Activity as KimaiAPIActivity, ) +from .sync import sync class ListScreen(Screen): @@ -601,47 +602,7 @@ class KimaiProjectListScreen(ListScreen): self.table.sort(self.sort) 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( - [{"id": customer.id, "name": customer.name} for customer in customers] - ).execute() - - projects = KimaiAPIProject.list(api) - with db.atomic(): - KimaiProject.insert_many( - [ - { - "id": project.id, - "name": project.name, - "customer_id": project.customer.id, - "allow_global_activities": project.allow_global_activities, - } - 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() - + sync() self._refresh() def on_mount(self) -> None: diff --git a/hamstertools/sync.py b/hamstertools/sync.py new file mode 100644 index 0000000..4e7b108 --- /dev/null +++ b/hamstertools/sync.py @@ -0,0 +1,57 @@ +from .kimai import ( + KimaiAPI, + Customer as KimaiAPICustomer, + Project as KimaiAPIProject, + Activity as KimaiAPIActivity, +) +from .db import ( + db, + HamsterCategory, + HamsterActivity, + HamsterFact, + KimaiProject, + KimaiCustomer, + KimaiActivity, + HamsterActivityKimaiMapping, +) + + +def sync() -> None: + api = KimaiAPI() + + KimaiCustomer.delete().execute() + KimaiProject.delete().execute() + KimaiActivity.delete().execute() + + customers = KimaiAPICustomer.list(api) + with db.atomic(): + KimaiCustomer.insert_many( + [{"id": customer.id, "name": customer.name} for customer in customers] + ).execute() + + projects = KimaiAPIProject.list(api) + with db.atomic(): + KimaiProject.insert_many( + [ + { + "id": project.id, + "name": project.name, + "customer_id": project.customer.id, + "allow_global_activities": project.allow_global_activities, + } + 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()