Spaces:
Runtime error
Runtime error
File size: 6,698 Bytes
4c4a722 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | """SQLAlchemy ORM models for ClassLens.
All tables are isolated in a dedicated schema (``classlens`` on Supabase) so they
never collide with other apps sharing the same Postgres instance.
"""
from __future__ import annotations
from datetime import datetime
from typing import Optional
from sqlalchemy import (
JSON,
Boolean,
DateTime,
Float,
ForeignKey,
Integer,
MetaData,
String,
Text,
UniqueConstraint,
func,
)
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from .db import active_schema
SCHEMA = active_schema()
def _fk(target: str) -> str:
"""Schema-qualified FK target, e.g. 'classlens.teachers.id' or 'teachers.id'."""
return f"{SCHEMA}.{target}" if SCHEMA else target
class Base(DeclarativeBase):
metadata = MetaData(schema=SCHEMA)
class Teacher(Base):
__tablename__ = "teachers"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
email: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, index=True)
password_hash: Mapped[str] = mapped_column(String(255), nullable=False)
display_name: Mapped[str] = mapped_column(String(255), default="")
full_name: Mapped[str] = mapped_column(String(255), default="")
school: Mapped[str] = mapped_column(String(255), default="")
is_admin: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
last_login: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), nullable=True)
class Student(Base):
__tablename__ = "students"
__table_args__ = (
UniqueConstraint("teacher_id", "normalized_name", name="uq_student_teacher_name"),
)
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
teacher_id: Mapped[int] = mapped_column(ForeignKey(_fk("teachers.id"), ondelete="CASCADE"), index=True)
name: Mapped[str] = mapped_column(String(255), nullable=False)
normalized_name: Mapped[str] = mapped_column(String(255), nullable=False, index=True)
external_id: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
class Quiz(Base):
__tablename__ = "quizzes"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
teacher_id: Mapped[int] = mapped_column(ForeignKey(_fk("teachers.id"), ondelete="CASCADE"), index=True)
title: Mapped[str] = mapped_column(String(512), default="Untitled Session")
subject: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
status: Mapped[str] = mapped_column(String(32), default="draft")
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
)
class RawFile(Base):
__tablename__ = "raw_files"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
quiz_id: Mapped[int] = mapped_column(ForeignKey(_fk("quizzes.id"), ondelete="CASCADE"), index=True)
data_type: Mapped[str] = mapped_column(String(64), nullable=False)
file_name: Mapped[str] = mapped_column(String(512), default="")
content_type: Mapped[str] = mapped_column(String(128), default="")
size_bytes: Mapped[int] = mapped_column(Integer, default=0)
storage_path: Mapped[str] = mapped_column(String(1024), default="")
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
class ParsedData(Base):
__tablename__ = "parsed_data"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
quiz_id: Mapped[int] = mapped_column(ForeignKey(_fk("quizzes.id"), ondelete="CASCADE"), index=True)
data_type: Mapped[str] = mapped_column(String(64), nullable=False)
file_name: Mapped[str] = mapped_column(String(512), default="")
raw_text: Mapped[str] = mapped_column(Text, default="")
structured_data: Mapped[dict] = mapped_column(JSON, nullable=False)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
class StudentResult(Base):
"""One student's outcome on one quiz — the backbone for cross-quiz analytics."""
__tablename__ = "student_results"
__table_args__ = (
UniqueConstraint("quiz_id", "student_id", name="uq_result_quiz_student"),
)
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
quiz_id: Mapped[int] = mapped_column(ForeignKey(_fk("quizzes.id"), ondelete="CASCADE"), index=True)
student_id: Mapped[int] = mapped_column(ForeignKey(_fk("students.id"), ondelete="CASCADE"), index=True)
answers: Mapped[dict] = mapped_column(JSON, default=dict)
total_questions: Mapped[int] = mapped_column(Integer, default=0)
correct_count: Mapped[int] = mapped_column(Integer, default=0)
score: Mapped[float] = mapped_column(Float, default=0.0)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
class Prompt(Base):
__tablename__ = "prompts"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
teacher_id: Mapped[int] = mapped_column(ForeignKey(_fk("teachers.id"), ondelete="CASCADE"), index=True)
name: Mapped[str] = mapped_column(String(255), nullable=False)
content: Mapped[str] = mapped_column(Text, nullable=False)
is_default: Mapped[bool] = mapped_column(Boolean, default=False)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
)
class Report(Base):
__tablename__ = "reports"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
quiz_id: Mapped[int] = mapped_column(ForeignKey(_fk("quizzes.id"), ondelete="CASCADE"), index=True)
student_id: Mapped[Optional[int]] = mapped_column(
ForeignKey(_fk("students.id"), ondelete="SET NULL"), nullable=True, index=True
)
prompt_id: Mapped[Optional[int]] = mapped_column(
ForeignKey(_fk("prompts.id"), ondelete="SET NULL"), nullable=True
)
html_content: Mapped[str] = mapped_column(Text, nullable=False)
model: Mapped[str] = mapped_column(String(64), default="")
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|