moved config. saved a workable state
This commit is contained in:
@@ -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 don’t 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 doesn’t 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
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user