task.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """
  2. Pydantic schemas for Task API.
  3. Defines request and response models for task operations.
  4. """
  5. from datetime import datetime
  6. from typing import Optional, Any
  7. from pydantic import BaseModel, Field
  8. class TaskCreate(BaseModel):
  9. """Schema for creating a new task."""
  10. project_id: str = Field(..., min_length=1, description="Project ID this task belongs to")
  11. name: str = Field(..., min_length=1, description="Task name")
  12. data: dict = Field(..., description="Task data (JSON object)")
  13. assigned_to: Optional[str] = Field(None, description="User ID assigned to this task")
  14. class Config:
  15. json_schema_extra = {
  16. "example": {
  17. "project_id": "proj_123abc",
  18. "name": "Annotate Image Batch 1",
  19. "data": {
  20. "image_url": "https://example.com/image1.jpg",
  21. "metadata": {"batch": 1}
  22. },
  23. "assigned_to": "user_001"
  24. }
  25. }
  26. class TaskUpdate(BaseModel):
  27. """Schema for updating an existing task."""
  28. name: Optional[str] = Field(None, min_length=1, description="Task name")
  29. data: Optional[dict] = Field(None, description="Task data (JSON object)")
  30. status: Optional[str] = Field(None, description="Task status (pending, in_progress, completed)")
  31. assigned_to: Optional[str] = Field(None, description="User ID assigned to this task")
  32. class Config:
  33. json_schema_extra = {
  34. "example": {
  35. "name": "Updated Task Name",
  36. "status": "in_progress",
  37. "assigned_to": "user_002"
  38. }
  39. }
  40. class TaskResponse(BaseModel):
  41. """Schema for task response."""
  42. id: str = Field(..., description="Task unique identifier")
  43. project_id: str = Field(..., description="Project ID this task belongs to")
  44. name: str = Field(..., description="Task name")
  45. data: dict = Field(..., description="Task data (JSON object)")
  46. status: str = Field(..., description="Task status (pending, in_progress, completed)")
  47. assigned_to: Optional[str] = Field(None, description="User ID assigned to this task")
  48. created_at: datetime = Field(..., description="Task creation timestamp")
  49. progress: float = Field(default=0.0, description="Task completion progress (0.0 to 1.0)")
  50. class Config:
  51. json_schema_extra = {
  52. "example": {
  53. "id": "task_456def",
  54. "project_id": "proj_123abc",
  55. "name": "Annotate Image Batch 1",
  56. "data": {
  57. "image_url": "https://example.com/image1.jpg",
  58. "metadata": {"batch": 1}
  59. },
  60. "status": "in_progress",
  61. "assigned_to": "user_001",
  62. "created_at": "2024-01-12T11:00:00",
  63. "progress": 0.5
  64. }
  65. }