trymeapp/app/seed.py

173 lines
6.3 KiB
Python

from __future__ import annotations
from sqlalchemy import func, select
from sqlalchemy.orm import Session
from app.models import Question, Quiz, User, utcnow
from app.services.quiz_service import generate_share_slug
def seed_starter_data_if_empty(db: Session) -> None:
existing_count = db.scalar(select(func.count(Quiz.id))) or 0
if existing_count > 0:
return
author = db.scalar(select(User).where(User.auth_sub == "system|quiz_club"))
if author is None:
author = User(
auth_sub="system|quiz_club",
email="club@example.com",
nickname="quiz_club",
first_name="TryMe",
last_name="Club",
age=15,
gender="prefer_not_to_say",
avatar_animal="owl",
avatar_color="avatar-2",
public_website_consent=True,
consent_at=utcnow(),
onboarded=True,
is_admin=False,
)
db.add(author)
db.flush()
starter_specs = [
{
"title": "Solar System Quick Check",
"description": "Four multiple-choice questions on planets, moons, and orbits.",
"questions": [
{
"type": "multiple_choice",
"prompt": "Which planet in our solar system has the largest ring system?",
"options": [
{"id": "a", "text": "Mars"},
{"id": "b", "text": "Saturn"},
{"id": "c", "text": "Venus"},
{"id": "d", "text": "Mercury"},
],
"correct": "b",
"aliases": [],
},
{
"type": "multiple_choice",
"prompt": "Which planet is known as the Red Planet due to iron oxide on its surface?",
"options": [
{"id": "a", "text": "Mars"},
{"id": "b", "text": "Jupiter"},
{"id": "c", "text": "Neptune"},
{"id": "d", "text": "Uranus"},
],
"correct": "a",
"aliases": [],
},
{
"type": "multiple_choice",
"prompt": "Roughly how long does light from the Sun take to reach Earth?",
"options": [
{"id": "a", "text": "8 seconds"},
{"id": "b", "text": "8 minutes"},
{"id": "c", "text": "8 hours"},
{"id": "d", "text": "8 days"},
],
"correct": "b",
"aliases": [],
},
{
"type": "multiple_choice",
"prompt": "Which moon of Jupiter is the largest moon in our solar system?",
"options": [
{"id": "a", "text": "Europa"},
{"id": "b", "text": "Io"},
{"id": "c", "text": "Ganymede"},
{"id": "d", "text": "Callisto"},
],
"correct": "c",
"aliases": [],
},
],
},
{
"title": "World Capitals Write-In",
"description": "Type the capital city for each country. Spelling counts, capitalization does not.",
"questions": [
{
"type": "write_in",
"prompt": "What is the capital city of Japan?",
"options": [],
"correct": "Tokyo",
"aliases": ["tokyo city"],
},
{
"type": "write_in",
"prompt": "What is the capital city of Canada?",
"options": [],
"correct": "Ottawa",
"aliases": [],
},
{
"type": "write_in",
"prompt": "What is the capital city of Egypt?",
"options": [],
"correct": "Cairo",
"aliases": [],
},
],
},
{
"title": "Biology & Chemistry Mix",
"description": "A short warm-up combining multiple-choice and write-in lab questions.",
"questions": [
{
"type": "multiple_choice",
"prompt": "Which organelle produces most of the chemical energy (ATP) in a eukaryotic cell?",
"options": [
{"id": "a", "text": "Ribosome"},
{"id": "b", "text": "Mitochondrion"},
{"id": "c", "text": "Golgi apparatus"},
{"id": "d", "text": "Lysosome"},
],
"correct": "b",
"aliases": [],
},
{
"type": "write_in",
"prompt": "What is the chemical symbol for gold on the periodic table?",
"options": [],
"correct": "Au",
"aliases": ["au"],
},
{
"type": "write_in",
"prompt": "How many chromosomes are found in a typical human somatic cell?",
"options": [],
"correct": "46",
"aliases": ["forty-six", "forty six"],
},
],
},
]
for spec in starter_specs:
quiz = Quiz(
share_slug=generate_share_slug(db, spec["title"]),
author_id=author.id,
title=spec["title"],
description=spec["description"],
is_public=True,
)
for pos, q in enumerate(spec["questions"]):
quiz.questions.append(
Question(
position=pos,
question_type=q["type"],
prompt=q["prompt"],
options=q["options"],
correct_answer=q["correct"],
accepted_answers=q["aliases"],
)
)
db.add(quiz)
db.commit()