hamster-tools/hamstertools/sync.py

79 lines
2.0 KiB
Python
Raw Normal View History

2023-11-03 00:13:54 +00:00
from .kimaiapi import (
2023-11-01 21:11:21 +00:00
KimaiAPI,
Customer as KimaiAPICustomer,
Project as KimaiAPIProject,
Activity as KimaiAPIActivity,
Tag as KimaiAPITag,
2023-11-01 21:11:21 +00:00
)
from .db import (
KimaiTag,
2023-11-01 21:11:21 +00:00
db,
KimaiProject,
KimaiCustomer,
KimaiActivity,
)
def sync() -> None:
api = KimaiAPI()
KimaiCustomer.delete().execute()
KimaiProject.delete().execute()
KimaiActivity.delete().execute()
KimaiTag.delete().execute()
2023-11-01 21:11:21 +00:00
customers = KimaiAPICustomer.list(api)
with db.atomic():
KimaiCustomer.insert_many(
[
{
"id": customer.id,
"name": customer.name,
"visible": customer.visible,
}
for customer in customers
]
2023-11-01 21:11:21 +00:00
).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,
"visible": project.visible,
2023-11-01 21:11:21 +00:00
}
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.id if activity.project else None),
"visible": activity.visible,
2023-11-01 21:11:21 +00:00
}
for activity in activities
]
).execute()
tags = KimaiAPITag.list(api)
with db.atomic():
KimaiTag.insert_many(
[
{
"id": tag.id,
"name": tag.name,
"visible": tag.visible,
}
for tag in tags
]
).execute()