Files
homeschool/backend/app/utils/timer.py
derekc ff9a863393 Add Done button, tablet controls, super admin management, midnight strike reset, and activity log improvements
- Done button snaps block to full duration, marks complete, logs "Marked Done by User"; Reset after Done fully un-completes the block
- Session action buttons stretch full-width and double height for tablet tapping
- Super admin: reset password, disable/enable accounts, delete user (with cascade), last active date per user's timezone
- Disabled account login returns specific error message instead of generic invalid credentials
- Users can change own password from Admin → Settings
- Strikes reset automatically at midnight in user's configured timezone (lazy reset on page load)
- Break timer state fully restored when navigating away and back to dashboard
- Timer no longer auto-starts on navigation if it wasn't running before
- Implicit pause guard: no duplicate pause events when switching already-paused blocks or starting a break
- Block selection events removed from activity log; all event types have human-readable labels
- House emoji favicon via inline SVG data URI
- README updated to reflect all changes

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-06 00:08:15 -08:00

82 lines
2.7 KiB
Python

"""Shared timer-elapsed computation used by sessions and dashboard routers."""
from datetime import datetime
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from app.models.session import TimerEvent
async def compute_block_elapsed(
db: AsyncSession, session_id: int, block_id: int
) -> tuple[int, bool]:
"""Return (elapsed_seconds, is_paused) for a block.
'reset' events are treated as zero-elapsed restart markers: any elapsed
time accumulated before a reset is discarded.
"""
tick_result = await db.execute(
select(TimerEvent)
.where(
TimerEvent.session_id == session_id,
TimerEvent.block_id == block_id,
TimerEvent.event_type.in_(["start", "resume", "pause", "reset"]),
)
.order_by(TimerEvent.occurred_at)
)
tick_events = tick_result.scalars().all()
elapsed = 0.0
last_start = None
for e in tick_events:
if e.event_type == "reset":
elapsed = 0.0
last_start = None
elif e.event_type in ("start", "resume"):
last_start = e.occurred_at
elif e.event_type == "pause" and last_start:
elapsed += (e.occurred_at - last_start).total_seconds()
last_start = None
running = last_start is not None
if running:
elapsed += (datetime.utcnow() - last_start).total_seconds()
# is_paused is True whenever the timer is not actively running —
# covers: explicitly paused, never started, or only selected.
is_paused = not running
return int(elapsed), is_paused
async def compute_break_elapsed(
db: AsyncSession, session_id: int, block_id: int
) -> tuple[int, bool]:
"""Return (break_elapsed_seconds, is_break_paused) for a block's break timer."""
tick_result = await db.execute(
select(TimerEvent)
.where(
TimerEvent.session_id == session_id,
TimerEvent.block_id == block_id,
TimerEvent.event_type.in_(["break_start", "break_resume", "break_pause", "break_reset"]),
)
.order_by(TimerEvent.occurred_at)
)
tick_events = tick_result.scalars().all()
elapsed = 0.0
last_start = None
for e in tick_events:
if e.event_type == "break_reset":
elapsed = 0.0
last_start = e.occurred_at
elif e.event_type in ("break_start", "break_resume"):
last_start = e.occurred_at
elif e.event_type == "break_pause" and last_start:
elapsed += (e.occurred_at - last_start).total_seconds()
last_start = None
running = last_start is not None
if running:
elapsed += (datetime.utcnow() - last_start).total_seconds()
is_paused = not running
return int(elapsed), is_paused