moved config. saved a workable state

This commit is contained in:
buenosairesam
2025-05-13 04:23:40 -03:00
parent f531be7158
commit 7995040c82
4 changed files with 102 additions and 62 deletions

View File

@@ -1,35 +1,47 @@
from pymongo import MongoClient
client = MongoClient()
db = client.deskmeter
state = db.state
from config import logger, states, tasks
def update_current_task(task):
state.update_one(
{"_id": "current_task"},
{"$set": {"task": task}},
upsert=True,
)
def save(doc_id: str, *, task: str | None = None, workspace: str | None = None) -> None:
"""
Upsert a document with _id=doc_id, setting any of the provided fields.
Leave fields you dont pass unchanged.
"""
updates: dict = {}
if task is not None:
updates["task"] = task
if workspace is not None:
updates["workspace"] = workspace
if updates:
states.update_one(
{"_id": doc_id},
{"$set": updates},
upsert=True,
)
def update_current_workspace(workspace):
state.update_one(
{"_id": "current_workspace"},
{"$set": {"workspace": workspace}},
upsert=True,
)
def retrieve(doc_id: str) -> dict[str, str | None]:
"""
Fetches the document with _id=doc_id and returns its 'task' and 'workspace'.
If the document doesnt exist, both will be None.
"""
doc = states.find_one({"_id": doc_id})
return {
"task": doc.get("task") if doc else None,
"workspace": doc.get("workspace") if doc else None,
}
def get_current_task():
current_task = state.find_one({"_id": "current_task"})
if current_task:
return current_task.get("task")
return None
def get_current_workspace():
current_workspace = state.find_one({"_id": "current_workspace"})
if current_workspace:
return current_workspace.get("workspace")
return None
def init_work_state(wd: dict):
"""
init work states with default values
"""
if not states.find_one({"_id": "work"}):
states.insert_one(
{
"_id": "work",
**{
wd[k]: tasks.find_one({"path": f"work/{wd[k]}"})["task_id"]
for k in wd
},
}
)