112 lines
3.7 KiB
Python
112 lines
3.7 KiB
Python
from sqlalchemy.orm import Session
|
|
|
|
from app.auth import hash_password
|
|
from app.core.config import settings
|
|
from app.db import SessionLocal
|
|
from app.models import Course, Module, ModuleType, QuizQuestion, QuizChoice, User, UserRole, Achievement
|
|
|
|
|
|
def seed():
|
|
db: Session = SessionLocal()
|
|
try:
|
|
admin = db.query(User).filter(User.email == settings.admin_seed_email).first()
|
|
if not admin:
|
|
admin = User(
|
|
email=settings.admin_seed_email,
|
|
password_hash=hash_password(settings.admin_seed_password),
|
|
role=UserRole.admin,
|
|
)
|
|
db.add(admin)
|
|
|
|
# Seed achievements
|
|
achievements_data = [
|
|
{
|
|
"code": "FIRST_COURSE_COMPLETE",
|
|
"name": "First Course Complete",
|
|
"description": "Complete your first course",
|
|
},
|
|
{
|
|
"code": "PERFECT_FINAL",
|
|
"name": "Perfect Final",
|
|
"description": "Score 100% on any final exam",
|
|
},
|
|
{
|
|
"code": "THREE_PERFECTS_ROW",
|
|
"name": "Triple Perfect",
|
|
"description": "Score 100% on three quiz attempts in a row",
|
|
},
|
|
{
|
|
"code": "FAST_FINISHER",
|
|
"name": "Fast Finisher",
|
|
"description": "Complete a course within 24 hours of enrollment",
|
|
},
|
|
{
|
|
"code": "CONSISTENT_LEARNER",
|
|
"name": "Consistent Learner",
|
|
"description": "Log in on 5 different days",
|
|
},
|
|
]
|
|
|
|
for ach_data in achievements_data:
|
|
existing = db.query(Achievement).filter(Achievement.code == ach_data["code"]).first()
|
|
if not existing:
|
|
db.add(Achievement(**ach_data))
|
|
|
|
course = db.query(Course).filter(Course.title == "Demo Lomda").first()
|
|
if not course:
|
|
course = Course(title="Demo Lomda", description="Intro demo course", is_published=True)
|
|
db.add(course)
|
|
db.flush()
|
|
|
|
module1 = Module(
|
|
course_id=course.id,
|
|
order_index=1,
|
|
type=ModuleType.content,
|
|
title="Welcome",
|
|
content_text="Welcome to the demo lomda. Mark complete to continue.",
|
|
)
|
|
module2 = Module(
|
|
course_id=course.id,
|
|
order_index=2,
|
|
type=ModuleType.quiz,
|
|
title="Quick Check",
|
|
pass_score=80,
|
|
)
|
|
module3 = Module(
|
|
course_id=course.id,
|
|
order_index=3,
|
|
type=ModuleType.quiz,
|
|
title="Final Exam",
|
|
pass_score=80,
|
|
)
|
|
db.add_all([module1, module2, module3])
|
|
db.flush()
|
|
|
|
q1 = QuizQuestion(module_id=module2.id, prompt="What does LMS stand for?")
|
|
db.add(q1)
|
|
db.flush()
|
|
db.add_all(
|
|
[
|
|
QuizChoice(question_id=q1.id, text="Learning Management System", is_correct=True),
|
|
QuizChoice(question_id=q1.id, text="Local Module Server", is_correct=False),
|
|
]
|
|
)
|
|
|
|
q2 = QuizQuestion(module_id=module3.id, prompt="Final exam question: 2 + 2 = ?")
|
|
db.add(q2)
|
|
db.flush()
|
|
db.add_all(
|
|
[
|
|
QuizChoice(question_id=q2.id, text="4", is_correct=True),
|
|
QuizChoice(question_id=q2.id, text="5", is_correct=False),
|
|
]
|
|
)
|
|
|
|
db.commit()
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
seed()
|