Files
ToDoList/api/app/models/goal.py
祀梦 5af8cb5486 feat: add goal management module (long-term goals with phases, milestones, reviews)
Backend:
- Goal model: title, description, status (active/paused/completed/abandoned),
  progress (auto-computed from milestones), target_date, category, color, icon
- GoalStep model: unified phase/milestone with parent nesting
- GoalReview model: periodic reflection with rating
- goal_tasks M2M: link existing tasks to goals
- /api/goals CRUD + steps CRUD + reviews + task linking + status toggle
- Progress auto-calculated from milestone completion ratio

Frontend:
- GoalPage: card grid with progress bars, status filter
- GoalDetailPage: step tree (phases > milestones), reviews, linked tasks
- GoalDialog: create/edit form with color/icon picker
- Goal navigation in AppHeader
- useGoalStore: full Pinia store for all goal operations

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

81 lines
3.0 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 sqlalchemy import Column, Integer, String, Text, Date, DateTime, ForeignKey, Table, desc
from sqlalchemy.orm import relationship
from app.database import Base
from app.utils.datetime import utcnow
# 目标-任务关联表(多对多)
goal_tasks = Table(
"goal_tasks",
Base.metadata,
Column("goal_id", Integer, ForeignKey("goals.id"), primary_key=True),
Column("task_id", Integer, ForeignKey("tasks.id"), primary_key=True),
)
class Goal(Base):
"""长期目标模型"""
__tablename__ = "goals"
id = Column(Integer, primary_key=True, index=True)
title = Column(String(200), nullable=False)
description = Column(Text, nullable=True)
status = Column(String(20), default="active") # active/paused/completed/abandoned
progress = Column(Integer, default=0) # 0-100从里程碑自动计算
target_date = Column(Date, nullable=True)
completed_at = Column(DateTime, nullable=True)
category_id = Column(Integer, ForeignKey("categories.id"), nullable=True)
color = Column(String(20), default="#FFB7C5")
icon = Column(String(50), default="flag")
sort_order = Column(Integer, default=0)
created_at = Column(DateTime, default=utcnow)
updated_at = Column(DateTime, default=utcnow, onupdate=utcnow)
# 关联关系
category = relationship("Category")
steps = relationship(
"GoalStep", back_populates="goal",
cascade="all, delete-orphan",
order_by="GoalStep.sort_order",
)
reviews = relationship(
"GoalReview", back_populates="goal",
cascade="all, delete-orphan",
order_by=lambda: desc(GoalReview.created_at),
)
tasks = relationship("Task", secondary=goal_tasks, back_populates="goals")
class GoalStep(Base):
"""目标阶段/里程碑模型step_type 区分类型)"""
__tablename__ = "goal_steps"
id = Column(Integer, primary_key=True, index=True)
goal_id = Column(Integer, ForeignKey("goals.id"), nullable=False)
parent_id = Column(Integer, ForeignKey("goal_steps.id"), nullable=True)
title = Column(String(200), nullable=False)
step_type = Column(String(20), nullable=False) # "phase" | "milestone"
status = Column(String(20), default="pending") # pending/in_progress/completed
target_date = Column(Date, nullable=True)
reached_at = Column(DateTime, nullable=True)
sort_order = Column(Integer, default=0)
created_at = Column(DateTime, default=utcnow)
# 关联关系
goal = relationship("Goal", back_populates="steps")
parent = relationship("GoalStep", remote_side=[id], backref="children")
class GoalReview(Base):
"""目标复盘记录模型"""
__tablename__ = "goal_reviews"
id = Column(Integer, primary_key=True, index=True)
goal_id = Column(Integer, ForeignKey("goals.id"), nullable=False)
content = Column(Text, nullable=False)
rating = Column(Integer, nullable=True) # 1-5 自评
created_at = Column(DateTime, default=utcnow)
# 关联关系
goal = relationship("Goal", back_populates="reviews")