Spaces:
Sleeping
Sleeping
Create models.py
Browse files
models.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# models.py
|
| 2 |
+
from sqlalchemy import create_engine, Column, Integer, String, DateTime, Boolean, ForeignKey, JSON, Float
|
| 3 |
+
from sqlalchemy.ext.declarative import declarative_base
|
| 4 |
+
from sqlalchemy.orm import relationship, sessionmaker
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
import uuid
|
| 7 |
+
|
| 8 |
+
Base = declarative_base()
|
| 9 |
+
|
| 10 |
+
class User(Base):
|
| 11 |
+
__tablename__ = 'users'
|
| 12 |
+
|
| 13 |
+
id = Column(Integer, primary_key=True)
|
| 14 |
+
username = Column(String, unique=True, nullable=False)
|
| 15 |
+
email = Column(String, unique=True, nullable=False)
|
| 16 |
+
password_hash = Column(String, nullable=False)
|
| 17 |
+
created_at = Column(DateTime, default=datetime.utcnow)
|
| 18 |
+
last_login = Column(DateTime)
|
| 19 |
+
is_active = Column(Boolean, default=True)
|
| 20 |
+
preferences = Column(JSON)
|
| 21 |
+
|
| 22 |
+
# Relationships
|
| 23 |
+
courses = relationship("UserCourse", back_populates="user")
|
| 24 |
+
progress = relationship("CourseProgress", back_populates="user")
|
| 25 |
+
interactions = relationship("UserInteraction", back_populates="user")
|
| 26 |
+
|
| 27 |
+
class Course(Base):
|
| 28 |
+
__tablename__ = 'courses'
|
| 29 |
+
|
| 30 |
+
id = Column(Integer, primary_key=True)
|
| 31 |
+
uuid = Column(String, unique=True, default=lambda: str(uuid.uuid4()))
|
| 32 |
+
title = Column(String, nullable=False)
|
| 33 |
+
description = Column(String)
|
| 34 |
+
difficulty_level = Column(String)
|
| 35 |
+
created_at = Column(DateTime, default=datetime.utcnow)
|
| 36 |
+
updated_at = Column(DateTime, onupdate=datetime.utcnow)
|
| 37 |
+
content = Column(JSON) # Stores complete course structure
|
| 38 |
+
metadata = Column(JSON) # Stores additional course metadata
|
| 39 |
+
|
| 40 |
+
# Relationships
|
| 41 |
+
modules = relationship("CourseModule", back_populates="course")
|
| 42 |
+
user_courses = relationship("UserCourse", back_populates="course")
|
| 43 |
+
|
| 44 |
+
class CourseModule(Base):
|
| 45 |
+
__tablename__ = 'course_modules'
|
| 46 |
+
|
| 47 |
+
id = Column(Integer, primary_key=True)
|
| 48 |
+
course_id = Column(Integer, ForeignKey('courses.id'))
|
| 49 |
+
title = Column(String, nullable=False)
|
| 50 |
+
description = Column(String)
|
| 51 |
+
order_index = Column(Integer)
|
| 52 |
+
content = Column(JSON)
|
| 53 |
+
prerequisites = Column(JSON)
|
| 54 |
+
|
| 55 |
+
# Relationships
|
| 56 |
+
course = relationship("Course", back_populates="modules")
|
| 57 |
+
sections = relationship("ModuleSection", back_populates="module")
|
| 58 |
+
|
| 59 |
+
class ModuleSection(Base):
|
| 60 |
+
__tablename__ = 'module_sections'
|
| 61 |
+
|
| 62 |
+
id = Column(Integer, primary_key=True)
|
| 63 |
+
module_id = Column(Integer, ForeignKey('course_modules.id'))
|
| 64 |
+
title = Column(String, nullable=False)
|
| 65 |
+
content = Column(String)
|
| 66 |
+
order_index = Column(Integer)
|
| 67 |
+
metadata = Column(JSON)
|
| 68 |
+
|
| 69 |
+
# Relationships
|
| 70 |
+
module = relationship("CourseModule", back_populates="sections")
|
| 71 |
+
quizzes = relationship("SectionQuiz", back_populates="section")
|
| 72 |
+
|
| 73 |
+
class UserCourse(Base):
|
| 74 |
+
__tablename__ = 'user_courses'
|
| 75 |
+
|
| 76 |
+
id = Column(Integer, primary_key=True)
|
| 77 |
+
user_id = Column(Integer, ForeignKey('users.id'))
|
| 78 |
+
course_id = Column(Integer, ForeignKey('courses.id'))
|
| 79 |
+
enrollment_date = Column(DateTime, default=datetime.utcnow)
|
| 80 |
+
completion_date = Column(DateTime)
|
| 81 |
+
status = Column(String) # 'enrolled', 'in_progress', 'completed', 'archived'
|
| 82 |
+
|
| 83 |
+
# Relationships
|
| 84 |
+
user = relationship("User", back_populates="courses")
|
| 85 |
+
course = relationship("Course", back_populates="user_courses")
|
| 86 |
+
progress = relationship("CourseProgress", back_populates="user_course")
|
| 87 |
+
|
| 88 |
+
class CourseProgress(Base):
|
| 89 |
+
__tablename__ = 'course_progress'
|
| 90 |
+
|
| 91 |
+
id = Column(Integer, primary_key=True)
|
| 92 |
+
user_id = Column(Integer, ForeignKey('users.id'))
|
| 93 |
+
user_course_id = Column(Integer, ForeignKey('user_courses.id'))
|
| 94 |
+
module_id = Column(Integer, ForeignKey('course_modules.id'))
|
| 95 |
+
last_accessed = Column(DateTime)
|
| 96 |
+
completion_percentage = Column(Float)
|
| 97 |
+
status = Column(String) # 'not_started', 'in_progress', 'completed'
|
| 98 |
+
|
| 99 |
+
# Relationships
|
| 100 |
+
user = relationship("User", back_populates="progress")
|
| 101 |
+
user_course = relationship("UserCourse", back_populates="progress")
|
| 102 |
+
|
| 103 |
+
class UserInteraction(Base):
|
| 104 |
+
__tablename__ = 'user_interactions'
|
| 105 |
+
|
| 106 |
+
id = Column(Integer, primary_key=True)
|
| 107 |
+
user_id = Column(Integer, ForeignKey('users.id'))
|
| 108 |
+
interaction_type = Column(String) # 'quiz_attempt', 'question_asked', 'content_viewed'
|
| 109 |
+
content_reference = Column(String)
|
| 110 |
+
timestamp = Column(DateTime, default=datetime.utcnow)
|
| 111 |
+
metadata = Column(JSON)
|
| 112 |
+
|
| 113 |
+
# Relationships
|
| 114 |
+
user = relationship("User", back_populates="interactions")
|