Spaces:
Runtime error
Runtime error
Greg Claude Opus 4.7 (1M context) commited on
Commit ·
100eb7b
1
Parent(s): 4d635e1
feat(teacher): add GET /sections/{id}/present-summary for M/F chip
Browse filesReturns masculino/femenino/sin_especificar/total counts of students
with an 'in' Attendance record today (or a given ?date=). RBAC-gated
to teacher role; owner-check via _ensure_teacher_section.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
backend/app/api/v1/endpoints/teacher.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
| 2 |
from fastapi.responses import StreamingResponse
|
| 3 |
-
from sqlalchemy import or_, and_, func
|
| 4 |
from sqlalchemy.orm import Session
|
| 5 |
from datetime import date, datetime, time, timedelta, timezone
|
| 6 |
from typing import Optional
|
|
@@ -24,6 +24,7 @@ from app.models.entrance_log import EntranceLog
|
|
| 24 |
from app.models.attendance import Attendance
|
| 25 |
from app.models.section_schedule import SectionSchedule
|
| 26 |
from app.utils.holiday_check import is_holiday
|
|
|
|
| 27 |
from app.utils.audit import log_for
|
| 28 |
|
| 29 |
router = APIRouter(prefix="/teacher", tags=["teacher"])
|
|
@@ -526,6 +527,29 @@ def submit_swipe(
|
|
| 526 |
)
|
| 527 |
db.commit()
|
| 528 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 529 |
return {"created": created, "total": len(payload.records), "skipped": skipped}
|
| 530 |
|
| 531 |
|
|
|
|
| 1 |
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
| 2 |
from fastapi.responses import StreamingResponse
|
| 3 |
+
from sqlalchemy import or_, and_, func, cast, Date
|
| 4 |
from sqlalchemy.orm import Session
|
| 5 |
from datetime import date, datetime, time, timedelta, timezone
|
| 6 |
from typing import Optional
|
|
|
|
| 24 |
from app.models.attendance import Attendance
|
| 25 |
from app.models.section_schedule import SectionSchedule
|
| 26 |
from app.utils.holiday_check import is_holiday
|
| 27 |
+
from app.utils.sex_counts import count_by_sex
|
| 28 |
from app.utils.audit import log_for
|
| 29 |
|
| 30 |
router = APIRouter(prefix="/teacher", tags=["teacher"])
|
|
|
|
| 527 |
)
|
| 528 |
db.commit()
|
| 529 |
|
| 530 |
+
|
| 531 |
+
@router.get("/sections/{section_id}/present-summary")
|
| 532 |
+
def section_present_summary(
|
| 533 |
+
section_id: int,
|
| 534 |
+
date_str: str | None = Query(None, alias="date"),
|
| 535 |
+
current_user: User = Depends(RBAC(["teacher"])),
|
| 536 |
+
db: Session = Depends(get_db),
|
| 537 |
+
):
|
| 538 |
+
_ensure_teacher_section(section_id, current_user.id, current_user.account_id, db)
|
| 539 |
+
day = datetime.strptime(date_str, "%Y-%m-%d").date() if date_str else date.today()
|
| 540 |
+
student_ids = [
|
| 541 |
+
sid for (sid,) in db.query(Attendance.user_id)
|
| 542 |
+
.filter(
|
| 543 |
+
Attendance.section_id == section_id,
|
| 544 |
+
Attendance.account_id == current_user.account_id,
|
| 545 |
+
Attendance.action == "in",
|
| 546 |
+
cast(Attendance.time_log, Date) == day,
|
| 547 |
+
)
|
| 548 |
+
.distinct()
|
| 549 |
+
.all()
|
| 550 |
+
]
|
| 551 |
+
return count_by_sex(db, account_id=current_user.account_id, student_ids=student_ids)
|
| 552 |
+
|
| 553 |
return {"created": created, "total": len(payload.records), "skipped": skipped}
|
| 554 |
|
| 555 |
|
backend/tests/test_teacher_present_summary.py
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
| 3 |
+
from fastapi.testclient import TestClient
|
| 4 |
+
|
| 5 |
+
from app.main import app
|
| 6 |
+
from app.core.database import SessionLocal, Base, engine
|
| 7 |
+
from app.models.plan import Plan
|
| 8 |
+
from app.models.account import Account
|
| 9 |
+
from app.models.user import User
|
| 10 |
+
from app.models.section import Section
|
| 11 |
+
from app.models.subject import Subject
|
| 12 |
+
from app.models.enrollment import Enrollment
|
| 13 |
+
from app.models.teacher_section import TeacherSection
|
| 14 |
+
from app.models.attendance import Attendance
|
| 15 |
+
from app.core.security import hash_password
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@pytest.fixture(scope="module")
|
| 19 |
+
def client():
|
| 20 |
+
Base.metadata.create_all(bind=engine)
|
| 21 |
+
yield TestClient(app)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _cleanup_by_org_name(db, org_name):
|
| 25 |
+
"""Remove all data seeded under a given organization name (idempotent)."""
|
| 26 |
+
accs = db.query(Account).filter(Account.organization_name == org_name).all()
|
| 27 |
+
for acc in accs:
|
| 28 |
+
_cleanup(db, acc.id)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def _cleanup(db, acc_id):
|
| 32 |
+
db.query(Attendance).filter(Attendance.account_id == acc_id).delete()
|
| 33 |
+
db.query(Enrollment).filter(Enrollment.account_id == acc_id).delete()
|
| 34 |
+
db.query(TeacherSection).filter(TeacherSection.account_id == acc_id).delete()
|
| 35 |
+
db.query(Section).filter(Section.account_id == acc_id).delete()
|
| 36 |
+
db.query(Subject).filter(Subject.account_id == acc_id).delete()
|
| 37 |
+
try:
|
| 38 |
+
from app.models.audit_log import AuditLog
|
| 39 |
+
db.query(AuditLog).filter(AuditLog.account_id == acc_id).delete()
|
| 40 |
+
except Exception:
|
| 41 |
+
pass
|
| 42 |
+
db.query(User).filter(User.account_id == acc_id).update({"parent_id": None})
|
| 43 |
+
db.query(User).filter(User.account_id == acc_id).delete()
|
| 44 |
+
db.query(Account).filter(Account.id == acc_id).delete()
|
| 45 |
+
db.commit()
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def _seed_section_with_teacher_and_students(db, sex_pattern, org_name, teacher_email, student_email_prefix):
|
| 49 |
+
plan = db.query(Plan).first() or Plan(name="basic", features=[], price=0)
|
| 50 |
+
if not plan.id:
|
| 51 |
+
db.add(plan); db.commit(); db.refresh(plan)
|
| 52 |
+
acc = Account(organization_name=org_name, plan_id=plan.id, category="school")
|
| 53 |
+
db.add(acc); db.commit(); db.refresh(acc)
|
| 54 |
+
teacher = User(
|
| 55 |
+
account_id=acc.id, name="T", lastname="T",
|
| 56 |
+
email=teacher_email, password_hash=hash_password("x"),
|
| 57 |
+
role="teacher", is_active="1", sex="F",
|
| 58 |
+
)
|
| 59 |
+
db.add(teacher); db.commit(); db.refresh(teacher)
|
| 60 |
+
sec = Section(account_id=acc.id, name="S1", academic_period="2025-2026", max_students=40)
|
| 61 |
+
db.add(sec); db.commit(); db.refresh(sec)
|
| 62 |
+
subj = Subject(account_id=acc.id, name="Matemáticas")
|
| 63 |
+
db.add(subj); db.commit(); db.refresh(subj)
|
| 64 |
+
db.add(TeacherSection(account_id=acc.id, section_id=sec.id, subject_id=subj.id, teacher_id=teacher.id))
|
| 65 |
+
students = []
|
| 66 |
+
for i, sx in enumerate(sex_pattern):
|
| 67 |
+
s = User(
|
| 68 |
+
account_id=acc.id, name=f"E{i}", lastname="X",
|
| 69 |
+
email=f"{student_email_prefix}{i}@talos.com", password_hash=hash_password("x"),
|
| 70 |
+
role="student", is_active="1", sex=sx,
|
| 71 |
+
)
|
| 72 |
+
db.add(s); db.commit(); db.refresh(s)
|
| 73 |
+
db.add(Enrollment(account_id=acc.id, section_id=sec.id, student_id=s.id))
|
| 74 |
+
students.append(s)
|
| 75 |
+
db.commit()
|
| 76 |
+
return acc, teacher, sec, students
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def _login(client, email):
|
| 80 |
+
r = client.post("/api/v1/auth/login", json={"email": email, "password": "x"})
|
| 81 |
+
return r.json()["access_token"]
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def test_present_summary_zero_when_no_attendance(client):
|
| 85 |
+
org_name = "PresSum_Zero"
|
| 86 |
+
db = SessionLocal()
|
| 87 |
+
try:
|
| 88 |
+
_cleanup_by_org_name(db, org_name)
|
| 89 |
+
acc, teacher, sec, _ = _seed_section_with_teacher_and_students(
|
| 90 |
+
db, ["M", "F", "M"],
|
| 91 |
+
org_name=org_name,
|
| 92 |
+
teacher_email="psum_zero_t@talos.com",
|
| 93 |
+
student_email_prefix="psum_zero_e",
|
| 94 |
+
)
|
| 95 |
+
acc_id = acc.id
|
| 96 |
+
sec_id = sec.id
|
| 97 |
+
teacher_email = teacher.email
|
| 98 |
+
finally:
|
| 99 |
+
db.close()
|
| 100 |
+
|
| 101 |
+
token = _login(client, teacher_email)
|
| 102 |
+
r = client.get(
|
| 103 |
+
f"/api/v1/teacher/sections/{sec_id}/present-summary",
|
| 104 |
+
headers={"Authorization": f"Bearer {token}"},
|
| 105 |
+
)
|
| 106 |
+
assert r.status_code == 200
|
| 107 |
+
assert r.json() == {"masculino": 0, "femenino": 0, "sin_especificar": 0, "total": 0}
|
| 108 |
+
|
| 109 |
+
db = SessionLocal()
|
| 110 |
+
try:
|
| 111 |
+
_cleanup(db, acc_id)
|
| 112 |
+
finally:
|
| 113 |
+
db.close()
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def test_present_summary_counts_today_in_marks(client):
|
| 117 |
+
org_name = "PresSum_Counts"
|
| 118 |
+
db = SessionLocal()
|
| 119 |
+
try:
|
| 120 |
+
_cleanup_by_org_name(db, org_name)
|
| 121 |
+
acc, teacher, sec, students = _seed_section_with_teacher_and_students(
|
| 122 |
+
db, ["M", "F", "F"],
|
| 123 |
+
org_name=org_name,
|
| 124 |
+
teacher_email="psum_counts_t@talos.com",
|
| 125 |
+
student_email_prefix="psum_counts_e",
|
| 126 |
+
)
|
| 127 |
+
acc_id = acc.id
|
| 128 |
+
sec_id = sec.id
|
| 129 |
+
teacher_email = teacher.email
|
| 130 |
+
for s in students[:2]:
|
| 131 |
+
db.add(Attendance(
|
| 132 |
+
account_id=acc.id, user_id=s.id, section_id=sec.id,
|
| 133 |
+
time_log=datetime.now(timezone.utc), action="in",
|
| 134 |
+
))
|
| 135 |
+
db.commit()
|
| 136 |
+
finally:
|
| 137 |
+
db.close()
|
| 138 |
+
|
| 139 |
+
token = _login(client, teacher_email)
|
| 140 |
+
r = client.get(
|
| 141 |
+
f"/api/v1/teacher/sections/{sec_id}/present-summary",
|
| 142 |
+
headers={"Authorization": f"Bearer {token}"},
|
| 143 |
+
)
|
| 144 |
+
assert r.status_code == 200
|
| 145 |
+
body = r.json()
|
| 146 |
+
assert body["masculino"] == 1
|
| 147 |
+
assert body["femenino"] == 1
|
| 148 |
+
assert body["total"] == 2
|
| 149 |
+
|
| 150 |
+
db = SessionLocal()
|
| 151 |
+
try:
|
| 152 |
+
_cleanup(db, acc_id)
|
| 153 |
+
finally:
|
| 154 |
+
db.close()
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
def test_present_summary_forbids_non_owning_teacher(client):
|
| 158 |
+
org_name = "PresSum_Forbid"
|
| 159 |
+
db = SessionLocal()
|
| 160 |
+
try:
|
| 161 |
+
_cleanup_by_org_name(db, org_name)
|
| 162 |
+
acc, teacher, sec, _ = _seed_section_with_teacher_and_students(
|
| 163 |
+
db, ["M"],
|
| 164 |
+
org_name=org_name,
|
| 165 |
+
teacher_email="psum_forbid_t@talos.com",
|
| 166 |
+
student_email_prefix="psum_forbid_e",
|
| 167 |
+
)
|
| 168 |
+
acc_id = acc.id
|
| 169 |
+
sec_id = sec.id
|
| 170 |
+
# Another teacher with no TeacherSection for this section
|
| 171 |
+
other = User(
|
| 172 |
+
account_id=acc.id, name="T2", lastname="T",
|
| 173 |
+
email="psum_forbid_t2@talos.com", password_hash=hash_password("x"),
|
| 174 |
+
role="teacher", is_active="1", sex="M",
|
| 175 |
+
)
|
| 176 |
+
db.add(other); db.commit()
|
| 177 |
+
other_email = other.email
|
| 178 |
+
finally:
|
| 179 |
+
db.close()
|
| 180 |
+
|
| 181 |
+
token = _login(client, other_email)
|
| 182 |
+
r = client.get(
|
| 183 |
+
f"/api/v1/teacher/sections/{sec_id}/present-summary",
|
| 184 |
+
headers={"Authorization": f"Bearer {token}"},
|
| 185 |
+
)
|
| 186 |
+
assert r.status_code in (403, 404)
|
| 187 |
+
|
| 188 |
+
db = SessionLocal()
|
| 189 |
+
try:
|
| 190 |
+
_cleanup(db, acc_id)
|
| 191 |
+
finally:
|
| 192 |
+
db.close()
|