Preliminary clockify support
This commit is contained in:
65
hamstertools/clockify.py
Normal file
65
hamstertools/clockify.py
Normal file
@ -0,0 +1,65 @@
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from clockify_sdk import Clockify
|
||||
|
||||
class NotFound(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@dataclass()
|
||||
class Project:
|
||||
api: Clockify = field(repr=False)
|
||||
id: str
|
||||
name: str
|
||||
workspace_id: str
|
||||
|
||||
@staticmethod
|
||||
def list(api):
|
||||
projects = api.projects.get_all()
|
||||
return [
|
||||
Project(api, p['id'], p['name'], p['workspaceId'])
|
||||
for p in projects
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def get_by_id(api, project_id):
|
||||
return api.projects.get_by_id(project_id)
|
||||
|
||||
|
||||
def sync_projects(api, db):
|
||||
"""Sync Clockify projects to local database"""
|
||||
from .db import ClockifyProject
|
||||
|
||||
# Fetch and store all projects
|
||||
projects = Project.list(api)
|
||||
with db.atomic():
|
||||
# Delete all existing projects
|
||||
ClockifyProject.delete().execute()
|
||||
|
||||
query = ClockifyProject.insert_many(
|
||||
[
|
||||
{
|
||||
"clockify_id": project.id,
|
||||
"name": project.name,
|
||||
"workspace_id": project.workspace_id
|
||||
}
|
||||
for project in projects
|
||||
]
|
||||
)
|
||||
breakpoint()
|
||||
query.execute()
|
||||
return len(projects)
|
||||
|
||||
|
||||
def export_fact(api, fact, project_id):
|
||||
"""Export a Hamster fact to Clockify as a time entry"""
|
||||
start = fact.start_time.isoformat()
|
||||
end = fact.end_time.isoformat() if fact.end_time else datetime.now().isoformat()
|
||||
|
||||
time_entry = api.create_time_entry(
|
||||
project_id=project_id,
|
||||
start=start,
|
||||
end=end,
|
||||
description=fact.description
|
||||
)
|
||||
return time_entry.id
|
Reference in New Issue
Block a user