Files
ToDoList/api/app/schemas/goal.py
祀梦 5048de4fa1 feat: add data backup/import, goal step ordering, and PostgreSQL migration
- Add GET /api/backup/export and POST /api/backup/import endpoints for full data backup
- Add drag-and-drop reorder for goal steps with PUT /api/goals/{id}/steps/reorder
- Auto-assign sort_order on step creation (preserves creation order)
- Fix duplicate milestone rendering in goal detail page
- Add category management button in goal dialog
- Migrate database default from SQLite to PostgreSQL
- Fix router guard redirect loop for logged-in users on setup/login pages
- Fix ALTER TABLE ADD COLUMN crash on callable defaults (uuid.uuid4)
- Add auth status rate limiter and token version caching
- Update CLAUDE.md to reflect current architecture

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-18 00:02:18 +08:00

134 lines
3.5 KiB
Python

from pydantic import BaseModel, Field, field_validator
from datetime import datetime, date
from typing import Optional, List
from app.schemas.category import CategoryResponse
from app.schemas.task import TaskResponse
# ============ GoalStep Schemas ============
class GoalStepBase(BaseModel):
title: str = Field(..., max_length=200)
step_type: str = Field(..., pattern="^(phase|milestone)$")
status: str = Field(default="pending", pattern="^(pending|in_progress|completed)$")
target_date: Optional[date] = None
parent_id: Optional[int] = None
sort_order: int = Field(default=0)
class GoalStepCreate(GoalStepBase):
pass
class GoalStepUpdate(BaseModel):
title: Optional[str] = Field(None, max_length=200)
step_type: Optional[str] = Field(None, pattern="^(phase|milestone)$")
status: Optional[str] = Field(None, pattern="^(pending|in_progress|completed)$")
target_date: Optional[date] = None
parent_id: Optional[int] = None
sort_order: Optional[int] = None
@property
def clearable_fields(self) -> set:
return {"target_date", "parent_id"}
class GoalStepResponse(GoalStepBase):
id: int
uuid: Optional[str] = None
goal_id: int
reached_at: Optional[datetime] = None
created_at: datetime
children: List["GoalStepResponse"] = []
class Config:
from_attributes = True
# ============ GoalReview Schemas ============
class GoalReviewCreate(BaseModel):
content: str = Field(..., min_length=1)
rating: Optional[int] = Field(None, ge=1, le=5)
class GoalReviewResponse(BaseModel):
id: int
uuid: Optional[str] = None
goal_id: int
content: str
rating: Optional[int] = None
created_at: datetime
class Config:
from_attributes = True
# ============ Goal Schemas ============
class GoalBase(BaseModel):
title: str = Field(..., max_length=200)
description: Optional[str] = None
status: str = Field(default="active", pattern="^(active|paused|completed|abandoned)$")
target_date: Optional[date] = None
category_id: Optional[int] = None
color: str = Field(default="#FFB7C5", max_length=20)
icon: str = Field(default="flag", max_length=50)
sort_order: int = Field(default=0)
class GoalCreate(GoalBase):
pass
class GoalUpdate(BaseModel):
title: Optional[str] = Field(None, max_length=200)
description: Optional[str] = None
status: Optional[str] = Field(None, pattern="^(active|paused|completed|abandoned)$")
target_date: Optional[date] = None
category_id: Optional[int] = None
color: Optional[str] = Field(None, max_length=20)
icon: Optional[str] = Field(None, max_length=50)
sort_order: Optional[int] = None
@property
def clearable_fields(self) -> set:
return {"description", "target_date", "category_id"}
class GoalListResponse(GoalBase):
id: int
uuid: Optional[str] = None
progress: int
completed_at: Optional[datetime] = None
created_at: datetime
updated_at: datetime
category: Optional[CategoryResponse] = None
total_steps: int = 0
completed_steps: int = 0
class Config:
from_attributes = True
class GoalDetailResponse(GoalListResponse):
steps: List[GoalStepResponse] = []
reviews: List[GoalReviewResponse] = []
tasks: List[TaskResponse] = []
class GoalStatusUpdate(BaseModel):
status: str = Field(..., pattern="^(active|paused|completed|abandoned)$")
# ============ Reorder Schema ============
class ReorderItem(BaseModel):
id: int
sort_order: int
class ReorderRequest(BaseModel):
items: list[ReorderItem]