Spaces:
Sleeping
Sleeping
Download src/dao/task_dao.py from Arclen/ucprojects-backend: direct link, hf CLI and curl.
- Browser
- Download file 8.59 kB
-
https://huggingface.co/spaces/Arclen/ucprojects-backend/resolve/main/src/dao/task_dao.py
- Command line
-
hf download hf://spaces/Arclen/ucprojects-backend/src/dao/task_dao.py
-
curl -L -o task_dao.py https://huggingface.co/spaces/Arclen/ucprojects-backend/resolve/main/src/dao/task_dao.py
8.59 kB
| from typing import List, Optional | |
| from src.db import get_db | |
| from bson import ObjectId | |
| from uuid import uuid4 | |
| from datetime import datetime | |
| from datetime import timedelta | |
| from src.dao.user_dao import UserDAO | |
| from src.dao.audit_dao import AuditDAO | |
| class TaskDAO: | |
| COLLECTION = "tasks" | |
| async def create_task(project_name: str, task_name: Optional[str], task_description: str, assignees: List[str], week: str, frequency: Optional[str] = "weekly", deadline: Optional[str] = None, priority: str = "Medium", client: Optional[str] = None, contact: Optional[str] = None, status: str = "todo") -> dict: | |
| # Validate that all assigned persons exist | |
| for user_id in assignees: | |
| user = await UserDAO.find_by_id(user_id) | |
| if not user: | |
| raise ValueError(f"User with ID {user_id} does not exist") | |
| db = get_db() | |
| doc = { | |
| "project_name": project_name.strip(), | |
| "task_name": task_name, | |
| "task_description": task_description, | |
| "assignees": assignees, | |
| "week": week, | |
| "frequency": frequency, | |
| "deadline": deadline, | |
| "priority": priority, | |
| "client": client, | |
| "contact": contact, | |
| "moved_from": None, | |
| "moved_at": None, | |
| "moved_history": [], | |
| "status": status, | |
| "created_at": datetime.utcnow() | |
| } | |
| res = await db[TaskDAO.COLLECTION].insert_one(doc) | |
| # Audio Log | |
| await AuditDAO.log_event("create_task", "task", str(res.inserted_id), { | |
| "project": project_name, | |
| "task": task_name or task_description, | |
| "assignees": assignees, | |
| "deadline": deadline, | |
| "priority": priority, | |
| "status": status | |
| }) | |
| return { | |
| "id": str(res.inserted_id), | |
| "project_name": project_name, | |
| "task_name": task_name, | |
| "task_description": task_description, | |
| "assignees": assignees, | |
| "week": week, | |
| "frequency": frequency, | |
| "deadline": deadline, | |
| "priority": priority, | |
| "client": client, | |
| "contact": contact, | |
| "moved_from": None, | |
| "moved_at": None, | |
| "moved_history": [], | |
| "status": status, | |
| "created_at": doc["created_at"] | |
| } | |
| async def list_tasks(project_name: Optional[str] = None) -> List[dict]: | |
| db = get_db() | |
| query = {} | |
| if project_name: | |
| query["project_name"] = project_name | |
| cursor = db[TaskDAO.COLLECTION].find(query).sort("created_at", -1) | |
| items = [] | |
| async for doc in cursor: | |
| # Handle backward compatibility for old field names | |
| task_name = doc.get("task_name") or doc.get("task") | |
| task_description = doc.get("task_description") or doc.get("task") | |
| assignees = doc.get("assignees") or doc.get("assigned_persons", []) | |
| assignee_details = [] | |
| if assignees: | |
| # If assignees is just a string (legacy), wrap it | |
| if isinstance(assignees, str): | |
| assignees = [assignees] | |
| for uid in assignees: | |
| if not uid: continue | |
| try: | |
| u = await UserDAO.find_by_id(uid) | |
| if u: | |
| assignee_details.append({"id": str(u.get("_id")), "name": u.get("name")}) | |
| except: | |
| pass # Ignore invalid IDs | |
| items.append({ | |
| "id": str(doc.get("_id")), | |
| "project_name": doc.get("project_name"), | |
| "task_name": task_name, | |
| "task_description": task_description, | |
| "assignees": assignees, | |
| "assignee_details": assignee_details, | |
| "week": doc.get("week"), | |
| "frequency": doc.get("frequency", "weekly"), | |
| "status": doc.get("status", "pending"), | |
| "completion_note": doc.get("completion_note"), | |
| "deadline": doc.get("deadline"), | |
| "priority": doc.get("priority", "Medium"), | |
| "client": doc.get("client"), | |
| "contact": doc.get("contact"), | |
| "moved_from": doc.get("moved_from"), | |
| "moved_at": doc.get("moved_at"), | |
| "moved_history": doc.get("moved_history", []), | |
| "subtasks": doc.get("subtasks", []), | |
| "created_at": doc.get("created_at") | |
| }) | |
| return items | |
| async def get_task(task_id: str) -> Optional[dict]: | |
| db = get_db() | |
| doc = await db[TaskDAO.COLLECTION].find_one({"_id": ObjectId(task_id)}) | |
| if not doc: | |
| return None | |
| # Handle backward compatibility for old field names | |
| task_name = doc.get("task_name") or doc.get("task") | |
| task_description = doc.get("task_description") or doc.get("task") | |
| assignees = doc.get("assignees") or doc.get("assigned_persons", []) | |
| return { | |
| "id": str(doc.get("_id")), | |
| "project_name": doc.get("project_name"), | |
| "task_name": task_name, | |
| "task_description": task_description, | |
| "assignees": assignees, | |
| "week": doc.get("week"), | |
| "frequency": doc.get("frequency", "weekly"), | |
| "deadline": doc.get("deadline"), | |
| "priority": doc.get("priority", "Medium"), | |
| "client": doc.get("client"), | |
| "contact": doc.get("contact"), | |
| "status": doc.get("status", "pending"), | |
| "completion_note": doc.get("completion_note"), | |
| "moved_from": doc.get("moved_from"), | |
| "moved_at": doc.get("moved_at"), | |
| "moved_history": doc.get("moved_history", []), | |
| "subtasks": doc.get("subtasks", []), | |
| "created_at": doc.get("created_at") | |
| } | |
| async def update_task(task_id: str, updates: dict) -> bool: | |
| db = get_db() | |
| if not updates: | |
| return False | |
| # Prevent moving a completed task to another week | |
| if "week" in updates: | |
| doc = await db[TaskDAO.COLLECTION].find_one({"_id": ObjectId(task_id)}) | |
| if doc and doc.get("status") == "done": | |
| raise ValueError("Cannot move a completed task to another week") | |
| # compute previous week: prefer existing week, otherwise derive from created_at | |
| prev_week = doc.get("week") if doc and doc.get("week") else None | |
| if not prev_week and doc and doc.get("created_at"): | |
| created_dt = doc.get("created_at") | |
| if isinstance(created_dt, datetime): | |
| d = created_dt.date() | |
| # Monday = 0 | |
| monday = d - timedelta(days=d.weekday()) | |
| prev_week = monday.isoformat() | |
| set_fields = updates.copy() | |
| # Avoid conflict: remove moved_history from $set if we are using $addToSet | |
| set_fields.pop("moved_history", None) | |
| if prev_week: | |
| set_fields["moved_from"] = prev_week | |
| set_fields["moved_at"] = datetime.utcnow() | |
| update_ops = {"$set": set_fields} | |
| if prev_week: | |
| update_ops["$addToSet"] = {"moved_history": prev_week} | |
| res = await db[TaskDAO.COLLECTION].update_one({"_id": ObjectId(task_id)}, update_ops) | |
| if res.modified_count > 0: | |
| await AuditDAO.log_event("update_task", "task", task_id, {"updates": updates}) | |
| return res.modified_count > 0 | |
| # default update for non-week changes | |
| res = await db[TaskDAO.COLLECTION].update_one({"_id": ObjectId(task_id)}, {"$set": updates}) | |
| if res.modified_count > 0: | |
| # Check if status changed | |
| if "status" in updates: | |
| await AuditDAO.log_event("update_status", "task", task_id, {"new_status": updates["status"]}) | |
| else: | |
| await AuditDAO.log_event("update_task", "task", task_id, {"updates": updates}) | |
| return res.modified_count > 0 | |
| async def delete_task(task_id: str) -> bool: | |
| db = get_db() | |
| res = await db[TaskDAO.COLLECTION].delete_one({"_id": ObjectId(task_id)}) | |
| return res.deleted_count > 0 |