Spaces:
Sleeping
Sleeping
| """Progress tracking models.""" | |
| from datetime import datetime | |
| from sqlalchemy import Column, String, Boolean, DateTime, Integer, ForeignKey, Float, UniqueConstraint | |
| from sqlalchemy.orm import relationship | |
| from uuid import uuid4 | |
| from app.database import Base | |
| class Enrollment(Base): | |
| __tablename__ = "enrollments" | |
| __table_args__ = ( | |
| UniqueConstraint('student_id', 'course_id', name='unique_enrollment'), | |
| ) | |
| id = Column(String(36), primary_key=True, default=lambda: str(uuid4())) | |
| student_id = Column(String(36), ForeignKey("users.id"), nullable=False) | |
| course_id = Column(String(36), ForeignKey("courses.id"), nullable=False) | |
| enrolled_at = Column(DateTime, default=datetime.utcnow) | |
| completed_at = Column(DateTime, nullable=True) | |
| is_completed = Column(Boolean, default=False) | |
| progress_percentage = Column(Float, default=0.0) | |
| # Relationships | |
| student = relationship("User", back_populates="enrollments") | |
| course = relationship("Course", back_populates="enrollments") | |
| def __repr__(self): | |
| return f"<Enrollment {self.student_id} -> {self.course_id}>" | |
| class Progress(Base): | |
| __tablename__ = "progress" | |
| __table_args__ = ( | |
| UniqueConstraint('student_id', 'lesson_id', name='unique_progress'), | |
| ) | |
| id = Column(String(36), primary_key=True, default=lambda: str(uuid4())) | |
| student_id = Column(String(36), ForeignKey("users.id"), nullable=False) | |
| lesson_id = Column(String(36), ForeignKey("lessons.id"), nullable=False) | |
| is_completed = Column(Boolean, default=False) | |
| completed_at = Column(DateTime, nullable=True) | |
| time_spent_seconds = Column(Integer, default=0) | |
| last_position = Column(Integer, default=0) # For video progress | |
| # Timestamps | |
| started_at = Column(DateTime, default=datetime.utcnow) | |
| updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) | |
| # Relationships | |
| student = relationship("User", back_populates="progress_records") | |
| lesson = relationship("Lesson", back_populates="progress_records") | |
| def __repr__(self): | |
| return f"<Progress {self.student_id} -> {self.lesson_id}>" | |
| class StudyStreak(Base): | |
| __tablename__ = "study_streaks" | |
| id = Column(String(36), primary_key=True, default=lambda: str(uuid4())) | |
| student_id = Column(String(36), ForeignKey("users.id"), nullable=False, unique=True) | |
| current_streak = Column(Integer, default=0) | |
| longest_streak = Column(Integer, default=0) | |
| last_study_date = Column(DateTime, nullable=True) | |
| total_study_days = Column(Integer, default=0) | |
| # Relationships | |
| student = relationship("User") | |
| def __repr__(self): | |
| return f"<StudyStreak {self.student_id}: {self.current_streak} days>" | |