64 lines
1.4 KiB
Python
64 lines
1.4 KiB
Python
import os
|
|
import sys
|
|
from datetime import date
|
|
from pathlib import Path
|
|
|
|
from werkzeug.security import generate_password_hash
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
from app import create_app
|
|
from app.database import get_db, init_db
|
|
|
|
|
|
def seed_question(db):
|
|
db.execute(
|
|
"""
|
|
INSERT OR IGNORE INTO questions (question, option_a, option_b, date)
|
|
VALUES (?, ?, ?, ?)
|
|
""",
|
|
(
|
|
"Would you rather be blind or deaf?",
|
|
"Blind",
|
|
"Deaf",
|
|
date.today().isoformat(),
|
|
),
|
|
)
|
|
|
|
|
|
def seed_admin(db):
|
|
username = os.environ.get("WYR_ADMIN_USERNAME", "admin")
|
|
password = os.environ.get("WYR_ADMIN_PASSWORD", "admin")
|
|
|
|
existing = db.execute(
|
|
"SELECT id FROM admins WHERE username = ?",
|
|
(username,),
|
|
).fetchone()
|
|
|
|
if existing is None:
|
|
db.execute(
|
|
"""
|
|
INSERT INTO admins (username, password_hash)
|
|
VALUES (?, ?)
|
|
""",
|
|
(username, generate_password_hash(password)),
|
|
)
|
|
|
|
return username, password
|
|
|
|
|
|
app = create_app()
|
|
|
|
with app.app_context():
|
|
init_db()
|
|
db = get_db()
|
|
seed_question(db)
|
|
username, password = seed_admin(db)
|
|
db.commit()
|
|
|
|
print("Database created.")
|
|
|
|
if username == "admin" and password == "admin":
|
|
print("Default admin login: admin / admin")
|