Break out sync to separate file
This commit is contained in:
57
hamstertools/sync.py
Normal file
57
hamstertools/sync.py
Normal 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()
|
Reference in New Issue
Block a user