Add Morning Routine to Admin and TV greeting state
Adds a per-user Morning Routine item list that appears in the TV dashboard Activities panel during the "Good Morning" countdown (before the first block starts). - morning_routine_items table (auto-created on startup) - CRUD API at /api/morning-routine (auth-required) - Items included in the public DashboardSnapshot so TV gets them without auth - Morning Routine section in Admin page (same add/edit/delete UX as subject options) - TV Activities column shows routine items when no block is active, switches to subject options once a block starts Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@ from app.config import get_settings
|
||||
from app.database import engine
|
||||
from app.models import Base
|
||||
from app.routers import auth, users, children, subjects, schedules, sessions, logs, dashboard
|
||||
from app.routers import morning_routine
|
||||
from app.websocket.manager import manager
|
||||
|
||||
settings = get_settings()
|
||||
@@ -62,6 +63,7 @@ app.include_router(subjects.router)
|
||||
app.include_router(schedules.router)
|
||||
app.include_router(sessions.router)
|
||||
app.include_router(logs.router)
|
||||
app.include_router(morning_routine.router)
|
||||
app.include_router(dashboard.router)
|
||||
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ from app.models.subject import Subject, SubjectOption
|
||||
from app.models.schedule import ScheduleTemplate, ScheduleBlock
|
||||
from app.models.session import DailySession, TimerEvent, TimerEventType
|
||||
from app.models.activity import ActivityLog
|
||||
from app.models.morning_routine import MorningRoutineItem
|
||||
from app.models.strike import StrikeEvent
|
||||
|
||||
__all__ = [
|
||||
@@ -21,5 +22,6 @@ __all__ = [
|
||||
"TimerEvent",
|
||||
"TimerEventType",
|
||||
"ActivityLog",
|
||||
"MorningRoutineItem",
|
||||
"StrikeEvent",
|
||||
]
|
||||
|
||||
15
backend/app/models/morning_routine.py
Normal file
15
backend/app/models/morning_routine.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from sqlalchemy import ForeignKey, Integer, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.models.base import Base
|
||||
|
||||
|
||||
class MorningRoutineItem(Base):
|
||||
__tablename__ = "morning_routine_items"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
|
||||
text: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
order_index: Mapped[int] = mapped_column(Integer, default=0)
|
||||
|
||||
user: Mapped["User"] = relationship("User") # noqa: F821
|
||||
@@ -11,6 +11,7 @@ from sqlalchemy.orm import selectinload
|
||||
|
||||
from app.dependencies import get_db
|
||||
from app.models.child import Child
|
||||
from app.models.morning_routine import MorningRoutineItem
|
||||
from app.models.schedule import ScheduleBlock, ScheduleTemplate
|
||||
from app.models.subject import Subject # noqa: F401 — needed for selectinload chain
|
||||
from app.models.session import DailySession, TimerEvent
|
||||
@@ -97,6 +98,13 @@ async def get_dashboard(child_id: int, db: AsyncSession = Depends(get_db)):
|
||||
# Paused if the last tick event was a pause (last_start is None but events exist)
|
||||
is_paused = bool(tick_events) and tick_events[-1].event_type == "pause"
|
||||
|
||||
routine_result = await db.execute(
|
||||
select(MorningRoutineItem)
|
||||
.where(MorningRoutineItem.user_id == child.user_id)
|
||||
.order_by(MorningRoutineItem.order_index, MorningRoutineItem.id)
|
||||
)
|
||||
morning_routine = [item.text for item in routine_result.scalars().all()]
|
||||
|
||||
return DashboardSnapshot(
|
||||
session=session,
|
||||
child=child,
|
||||
@@ -106,4 +114,5 @@ async def get_dashboard(child_id: int, db: AsyncSession = Depends(get_db)):
|
||||
is_paused=is_paused,
|
||||
day_start_time=day_start_time,
|
||||
day_end_time=day_end_time,
|
||||
morning_routine=morning_routine,
|
||||
)
|
||||
|
||||
97
backend/app/routers/morning_routine.py
Normal file
97
backend/app/routers/morning_routine.py
Normal file
@@ -0,0 +1,97 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.dependencies import get_db, get_current_user
|
||||
from app.models.morning_routine import MorningRoutineItem
|
||||
from app.models.user import User
|
||||
|
||||
router = APIRouter(prefix="/api/morning-routine", tags=["morning-routine"])
|
||||
|
||||
|
||||
class MorningRoutineItemOut(BaseModel):
|
||||
id: int
|
||||
text: str
|
||||
order_index: int
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
class MorningRoutineItemCreate(BaseModel):
|
||||
text: str
|
||||
order_index: int = 0
|
||||
|
||||
|
||||
class MorningRoutineItemUpdate(BaseModel):
|
||||
text: str | None = None
|
||||
order_index: int | None = None
|
||||
|
||||
|
||||
@router.get("", response_model=list[MorningRoutineItemOut])
|
||||
async def list_items(
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
result = await db.execute(
|
||||
select(MorningRoutineItem)
|
||||
.where(MorningRoutineItem.user_id == current_user.id)
|
||||
.order_by(MorningRoutineItem.order_index, MorningRoutineItem.id)
|
||||
)
|
||||
return result.scalars().all()
|
||||
|
||||
|
||||
@router.post("", response_model=MorningRoutineItemOut, status_code=status.HTTP_201_CREATED)
|
||||
async def create_item(
|
||||
body: MorningRoutineItemCreate,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
item = MorningRoutineItem(user_id=current_user.id, text=body.text, order_index=body.order_index)
|
||||
db.add(item)
|
||||
await db.commit()
|
||||
await db.refresh(item)
|
||||
return item
|
||||
|
||||
|
||||
@router.patch("/{item_id}", response_model=MorningRoutineItemOut)
|
||||
async def update_item(
|
||||
item_id: int,
|
||||
body: MorningRoutineItemUpdate,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
result = await db.execute(
|
||||
select(MorningRoutineItem).where(
|
||||
MorningRoutineItem.id == item_id,
|
||||
MorningRoutineItem.user_id == current_user.id,
|
||||
)
|
||||
)
|
||||
item = result.scalar_one_or_none()
|
||||
if not item:
|
||||
raise HTTPException(status_code=404, detail="Item not found")
|
||||
if body.text is not None:
|
||||
item.text = body.text
|
||||
if body.order_index is not None:
|
||||
item.order_index = body.order_index
|
||||
await db.commit()
|
||||
await db.refresh(item)
|
||||
return item
|
||||
|
||||
|
||||
@router.delete("/{item_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def delete_item(
|
||||
item_id: int,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
result = await db.execute(
|
||||
select(MorningRoutineItem).where(
|
||||
MorningRoutineItem.id == item_id,
|
||||
MorningRoutineItem.user_id == current_user.id,
|
||||
)
|
||||
)
|
||||
item = result.scalar_one_or_none()
|
||||
if not item:
|
||||
raise HTTPException(status_code=404, detail="Item not found")
|
||||
await db.delete(item)
|
||||
await db.commit()
|
||||
@@ -46,3 +46,4 @@ class DashboardSnapshot(BaseModel):
|
||||
is_paused: bool = False # whether the current block's timer is paused
|
||||
day_start_time: time | None = None
|
||||
day_end_time: time | None = None
|
||||
morning_routine: list[str] = [] # text items shown on TV during greeting state
|
||||
|
||||
Reference in New Issue
Block a user