Break out sync to separate file

This commit is contained in:
3wc 2023-11-01 21:11:21 +00:00
parent 77f87e5299
commit 21fc28a14e
3 changed files with 72 additions and 44 deletions

View File

@ -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",

View File

@ -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:

57
hamstertools/sync.py Normal file
View File

@ -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()