This repository has been archived on 2026-02-15. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
breakpilot-pwa/klausur-service/backend/routes/students.py
Benjamin Admin bfdaf63ba9 fix: Restore all files lost during destructive rebase
A previous `git pull --rebase origin main` dropped 177 local commits,
losing 3400+ files across admin-v2, backend, studio-v2, website,
klausur-service, and many other services. The partial restore attempt
(660295e2) only recovered some files.

This commit restores all missing files from pre-rebase ref 98933f5e
while preserving post-rebase additions (night-scheduler, night-mode UI,
NightModeWidget dashboard integration).

Restored features include:
- AI Module Sidebar (FAB), OCR Labeling, OCR Compare
- GPU Dashboard, RAG Pipeline, Magic Help
- Klausur-Korrektur (8 files), Abitur-Archiv (5+ files)
- Companion, Zeugnisse-Crawler, Screen Flow
- Full backend, studio-v2, website, klausur-service
- All compliance SDKs, agent-core, voice-service
- CI/CD configs, documentation, scripts

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-09 09:51:32 +01:00

132 lines
3.8 KiB
Python

"""
Klausur-Service Student Routes
Endpoints for student work management.
"""
import os
import uuid
from datetime import datetime, timezone
from fastapi import APIRouter, HTTPException, Request, UploadFile, File, Form
from fastapi.responses import FileResponse
from models.exam import StudentKlausur
from models.enums import StudentKlausurStatus
from services.auth_service import get_current_user
from config import UPLOAD_DIR
import storage
router = APIRouter()
@router.post("/api/v1/klausuren/{klausur_id}/students")
async def upload_student_work(
klausur_id: str,
student_name: str = Form(...),
file: UploadFile = File(...),
request: Request = None
):
"""Upload a student's work."""
user = get_current_user(request)
if klausur_id not in storage.klausuren_db:
raise HTTPException(status_code=404, detail="Klausur not found")
klausur = storage.klausuren_db[klausur_id]
# Save file
upload_dir = f"{UPLOAD_DIR}/{klausur_id}"
os.makedirs(upload_dir, exist_ok=True)
file_ext = os.path.splitext(file.filename)[1]
file_id = str(uuid.uuid4())
file_path = f"{upload_dir}/{file_id}{file_ext}"
with open(file_path, "wb") as f:
content = await file.read()
f.write(content)
# Create student record
student = StudentKlausur(
id=file_id,
klausur_id=klausur_id,
student_name=student_name,
student_id=None,
file_path=file_path,
ocr_text=None,
status=StudentKlausurStatus.UPLOADED,
criteria_scores={},
gutachten=None,
raw_points=0,
grade_points=0,
created_at=datetime.now(timezone.utc)
)
storage.students_db[student.id] = student
klausur.students.append(student)
return student.to_dict()
@router.get("/api/v1/klausuren/{klausur_id}/students")
async def list_students(klausur_id: str, request: Request):
"""List all students for a Klausur."""
user = get_current_user(request)
if klausur_id not in storage.klausuren_db:
raise HTTPException(status_code=404, detail="Klausur not found")
klausur = storage.klausuren_db[klausur_id]
return [s.to_dict() for s in klausur.students]
@router.get("/api/v1/students/{student_id}/file")
async def get_student_file(student_id: str, request: Request):
"""Get the uploaded file for a student."""
user = get_current_user(request)
if student_id not in storage.students_db:
raise HTTPException(status_code=404, detail="Student work not found")
student = storage.students_db[student_id]
if not student.file_path or not os.path.exists(student.file_path):
raise HTTPException(status_code=404, detail="File not found")
# Determine media type from file extension
ext = os.path.splitext(student.file_path)[1].lower()
media_types = {
'.pdf': 'application/pdf',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.gif': 'image/gif',
}
media_type = media_types.get(ext, 'application/octet-stream')
return FileResponse(student.file_path, media_type=media_type)
@router.delete("/api/v1/students/{student_id}")
async def delete_student_work(student_id: str, request: Request):
"""Delete a student's work."""
user = get_current_user(request)
if student_id not in storage.students_db:
raise HTTPException(status_code=404, detail="Student work not found")
student = storage.students_db[student_id]
# Remove from klausur
if student.klausur_id in storage.klausuren_db:
klausur = storage.klausuren_db[student.klausur_id]
klausur.students = [s for s in klausur.students if s.id != student_id]
# Delete file
if student.file_path and os.path.exists(student.file_path):
os.remove(student.file_path)
del storage.students_db[student_id]
return {"success": True}