Files
deskmeter/dmapp/dmcore/state.py
2025-05-13 04:23:40 -03:00

48 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from config import logger, states, tasks
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 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 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
},
}
)