Spaces:
Runtime error
Runtime error
Greg Claude Sonnet 4.6 commited on
Commit ·
ca484ca
1
Parent(s): a3df693
feat(teacher-attendance): allow marking outside schedule, structured audit
Browse filesRemove _check_active_schedule and _check_not_before_class calls from
swipe, batch-toggle, and toggle endpoints. Add _is_out_of_schedule
helper and structured AuditLog entries (fuera_de_horario flag, section,
subject, students_marked, click_time_utc) after each successful mark.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
backend/app/api/v1/endpoints/teacher.py
CHANGED
|
@@ -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 |
|
| 28 |
router = APIRouter(prefix="/teacher", tags=["teacher"])
|
| 29 |
|
|
@@ -125,6 +126,26 @@ def _check_not_before_class(section_id: int, account_id: int, db: Session, targe
|
|
| 125 |
raise HTTPException(403, "No puedes registrar asistencia antes del inicio de la clase")
|
| 126 |
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
@router.get("/subjects")
|
| 129 |
def teacher_subjects(
|
| 130 |
current_user: User = Depends(RBAC(["teacher"])),
|
|
@@ -440,7 +461,6 @@ def submit_swipe(
|
|
| 440 |
if is_holiday(current_user.account_id, date.today(), db):
|
| 441 |
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Hoy es día feriado. No se puede registrar asistencia.")
|
| 442 |
_ensure_teacher_section(payload.section_id, current_user.id, current_user.account_id, db)
|
| 443 |
-
_check_active_schedule(payload.section_id, current_user.account_id, db, payload.subject_id)
|
| 444 |
now = datetime.now()
|
| 445 |
today = now.date()
|
| 446 |
created = 0
|
|
@@ -471,6 +491,25 @@ def submit_swipe(
|
|
| 471 |
db.add(Attendance(account_id=current_user.account_id, user_id=rec.student_id, section_id=payload.section_id, time_log=datetime.now(timezone.utc), action="in", note="Swipe: present"))
|
| 472 |
created += 1
|
| 473 |
db.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 474 |
return {"created": created, "total": len(payload.records)}
|
| 475 |
|
| 476 |
|
|
@@ -485,7 +524,6 @@ def batch_toggle(
|
|
| 485 |
if is_holiday(current_user.account_id, day, db):
|
| 486 |
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Es día feriado. No se puede registrar asistencia.")
|
| 487 |
_ensure_teacher_section(payload.section_id, current_user.id, current_user.account_id, db)
|
| 488 |
-
_check_not_before_class(payload.section_id, current_user.account_id, db, day, payload.subject_id)
|
| 489 |
record_time = datetime.now(timezone.utc) if day == date.today() else datetime.combine(day, time(12, 0), tzinfo=timezone.utc)
|
| 490 |
|
| 491 |
student_ids = [rec.student_id for rec in payload.records]
|
|
@@ -530,6 +568,26 @@ def batch_toggle(
|
|
| 530 |
name_map = {u.id: f"{u.name} {u.lastname}" for u in rows}
|
| 531 |
skipped = [{"student_id": sid, "name": name_map.get(sid, "")} for sid in skipped_ids]
|
| 532 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 533 |
return {"skipped": skipped}
|
| 534 |
|
| 535 |
|
|
@@ -600,7 +658,6 @@ def toggle_attendance(
|
|
| 600 |
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Hoy es día feriado. No se puede registrar asistencia.")
|
| 601 |
_ensure_teacher_section(section_id, current_user.id, current_user.account_id, db)
|
| 602 |
day = datetime.strptime(date_str, "%Y-%m-%d").date() if date_str else date.today()
|
| 603 |
-
_check_not_before_class(section_id, current_user.account_id, db, day, subject_id)
|
| 604 |
|
| 605 |
existing_in = db.query(Attendance).filter(
|
| 606 |
Attendance.user_id == student_id,
|
|
@@ -613,9 +670,29 @@ def toggle_attendance(
|
|
| 613 |
),
|
| 614 |
).first()
|
| 615 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 616 |
if existing_in:
|
| 617 |
db.delete(existing_in)
|
| 618 |
db.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 619 |
return {"present": False, "attendance_id": None}
|
| 620 |
else:
|
| 621 |
has_entrance = db.query(EntranceLog).filter(
|
|
@@ -628,6 +705,21 @@ def toggle_attendance(
|
|
| 628 |
att = Attendance(account_id=current_user.account_id, user_id=student_id, section_id=section_id, time_log=datetime.now(timezone.utc), action="in", note="Swipe: present")
|
| 629 |
db.add(att)
|
| 630 |
db.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 631 |
return {"present": True, "attendance_id": att.id}
|
| 632 |
|
| 633 |
|
|
|
|
| 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"])
|
| 30 |
|
|
|
|
| 126 |
raise HTTPException(403, "No puedes registrar asistencia antes del inicio de la clase")
|
| 127 |
|
| 128 |
|
| 129 |
+
def _is_out_of_schedule(section_id: int, now: datetime, db: Session) -> bool:
|
| 130 |
+
"""True if *now* is NOT inside any schedule block for this section today."""
|
| 131 |
+
today_idx = now.weekday() # 0 = Monday
|
| 132 |
+
blocks = db.query(SectionSchedule).filter(
|
| 133 |
+
SectionSchedule.section_id == section_id,
|
| 134 |
+
SectionSchedule.day_of_week == today_idx,
|
| 135 |
+
).all()
|
| 136 |
+
if not blocks:
|
| 137 |
+
return True
|
| 138 |
+
now_minutes = now.hour * 60 + now.minute
|
| 139 |
+
for b in blocks:
|
| 140 |
+
h, m = map(int, b.entry_time.split(":"))
|
| 141 |
+
entry_min = h * 60 + m
|
| 142 |
+
h, m = map(int, b.exit_time.split(":"))
|
| 143 |
+
exit_min = h * 60 + m
|
| 144 |
+
if entry_min <= now_minutes <= exit_min:
|
| 145 |
+
return False
|
| 146 |
+
return True
|
| 147 |
+
|
| 148 |
+
|
| 149 |
@router.get("/subjects")
|
| 150 |
def teacher_subjects(
|
| 151 |
current_user: User = Depends(RBAC(["teacher"])),
|
|
|
|
| 461 |
if is_holiday(current_user.account_id, date.today(), db):
|
| 462 |
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Hoy es día feriado. No se puede registrar asistencia.")
|
| 463 |
_ensure_teacher_section(payload.section_id, current_user.id, current_user.account_id, db)
|
|
|
|
| 464 |
now = datetime.now()
|
| 465 |
today = now.date()
|
| 466 |
created = 0
|
|
|
|
| 491 |
db.add(Attendance(account_id=current_user.account_id, user_id=rec.student_id, section_id=payload.section_id, time_log=datetime.now(timezone.utc), action="in", note="Swipe: present"))
|
| 492 |
created += 1
|
| 493 |
db.commit()
|
| 494 |
+
|
| 495 |
+
section = db.query(Section).filter(Section.id == payload.section_id).first()
|
| 496 |
+
subject = db.query(Subject).filter(Subject.id == payload.subject_id).first() if payload.subject_id else None
|
| 497 |
+
out = _is_out_of_schedule(payload.section_id, now, db)
|
| 498 |
+
log_for(
|
| 499 |
+
db, current_user, "Marcar asistencia", "asistencias",
|
| 500 |
+
details={
|
| 501 |
+
"endpoint": "swipe",
|
| 502 |
+
"section_id": payload.section_id,
|
| 503 |
+
"section_name": section.name if section else None,
|
| 504 |
+
"subject_id": payload.subject_id,
|
| 505 |
+
"subject_name": subject.name if subject else None,
|
| 506 |
+
"fuera_de_horario": out,
|
| 507 |
+
"students_marked": [rec.student_id for rec in payload.records if rec.present],
|
| 508 |
+
"click_time_utc": datetime.now(timezone.utc).isoformat(),
|
| 509 |
+
},
|
| 510 |
+
)
|
| 511 |
+
db.commit()
|
| 512 |
+
|
| 513 |
return {"created": created, "total": len(payload.records)}
|
| 514 |
|
| 515 |
|
|
|
|
| 524 |
if is_holiday(current_user.account_id, day, db):
|
| 525 |
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Es día feriado. No se puede registrar asistencia.")
|
| 526 |
_ensure_teacher_section(payload.section_id, current_user.id, current_user.account_id, db)
|
|
|
|
| 527 |
record_time = datetime.now(timezone.utc) if day == date.today() else datetime.combine(day, time(12, 0), tzinfo=timezone.utc)
|
| 528 |
|
| 529 |
student_ids = [rec.student_id for rec in payload.records]
|
|
|
|
| 568 |
name_map = {u.id: f"{u.name} {u.lastname}" for u in rows}
|
| 569 |
skipped = [{"student_id": sid, "name": name_map.get(sid, "")} for sid in skipped_ids]
|
| 570 |
|
| 571 |
+
now_local = datetime.now()
|
| 572 |
+
section = db.query(Section).filter(Section.id == payload.section_id).first()
|
| 573 |
+
subject = db.query(Subject).filter(Subject.id == payload.subject_id).first() if payload.subject_id else None
|
| 574 |
+
out = _is_out_of_schedule(payload.section_id, now_local, db)
|
| 575 |
+
log_for(
|
| 576 |
+
db, current_user, "Marcar asistencia", "asistencias",
|
| 577 |
+
details={
|
| 578 |
+
"endpoint": "batch-toggle",
|
| 579 |
+
"section_id": payload.section_id,
|
| 580 |
+
"section_name": section.name if section else None,
|
| 581 |
+
"subject_id": payload.subject_id,
|
| 582 |
+
"subject_name": subject.name if subject else None,
|
| 583 |
+
"fuera_de_horario": out,
|
| 584 |
+
"date": day.isoformat(),
|
| 585 |
+
"students_marked": [rec.student_id for rec in payload.records if rec.present],
|
| 586 |
+
"click_time_utc": datetime.now(timezone.utc).isoformat(),
|
| 587 |
+
},
|
| 588 |
+
)
|
| 589 |
+
db.commit()
|
| 590 |
+
|
| 591 |
return {"skipped": skipped}
|
| 592 |
|
| 593 |
|
|
|
|
| 658 |
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Hoy es día feriado. No se puede registrar asistencia.")
|
| 659 |
_ensure_teacher_section(section_id, current_user.id, current_user.account_id, db)
|
| 660 |
day = datetime.strptime(date_str, "%Y-%m-%d").date() if date_str else date.today()
|
|
|
|
| 661 |
|
| 662 |
existing_in = db.query(Attendance).filter(
|
| 663 |
Attendance.user_id == student_id,
|
|
|
|
| 670 |
),
|
| 671 |
).first()
|
| 672 |
|
| 673 |
+
now_local = datetime.now()
|
| 674 |
+
section = db.query(Section).filter(Section.id == section_id).first()
|
| 675 |
+
subject = db.query(Subject).filter(Subject.id == subject_id).first() if subject_id else None
|
| 676 |
+
out = _is_out_of_schedule(section_id, now_local, db)
|
| 677 |
+
|
| 678 |
if existing_in:
|
| 679 |
db.delete(existing_in)
|
| 680 |
db.commit()
|
| 681 |
+
log_for(
|
| 682 |
+
db, current_user, "Marcar asistencia", "asistencias",
|
| 683 |
+
details={
|
| 684 |
+
"endpoint": "toggle",
|
| 685 |
+
"action": "unmarked",
|
| 686 |
+
"section_id": section_id,
|
| 687 |
+
"section_name": section.name if section else None,
|
| 688 |
+
"subject_id": subject_id,
|
| 689 |
+
"subject_name": subject.name if subject else None,
|
| 690 |
+
"fuera_de_horario": out,
|
| 691 |
+
"students_marked": [student_id],
|
| 692 |
+
"click_time_utc": datetime.now(timezone.utc).isoformat(),
|
| 693 |
+
},
|
| 694 |
+
)
|
| 695 |
+
db.commit()
|
| 696 |
return {"present": False, "attendance_id": None}
|
| 697 |
else:
|
| 698 |
has_entrance = db.query(EntranceLog).filter(
|
|
|
|
| 705 |
att = Attendance(account_id=current_user.account_id, user_id=student_id, section_id=section_id, time_log=datetime.now(timezone.utc), action="in", note="Swipe: present")
|
| 706 |
db.add(att)
|
| 707 |
db.commit()
|
| 708 |
+
log_for(
|
| 709 |
+
db, current_user, "Marcar asistencia", "asistencias",
|
| 710 |
+
details={
|
| 711 |
+
"endpoint": "toggle",
|
| 712 |
+
"action": "marked",
|
| 713 |
+
"section_id": section_id,
|
| 714 |
+
"section_name": section.name if section else None,
|
| 715 |
+
"subject_id": subject_id,
|
| 716 |
+
"subject_name": subject.name if subject else None,
|
| 717 |
+
"fuera_de_horario": out,
|
| 718 |
+
"students_marked": [student_id],
|
| 719 |
+
"click_time_utc": datetime.now(timezone.utc).isoformat(),
|
| 720 |
+
},
|
| 721 |
+
)
|
| 722 |
+
db.commit()
|
| 723 |
return {"present": True, "attendance_id": att.id}
|
| 724 |
|
| 725 |
|