Devashishraghav's picture
Upload 13 files
b3d3cdb verified
Raw
History Blame Contribute Delete
4 kB
import os
import sys
import random
import uuid
import hashlib
from datetime import datetime, timedelta
sys.path.insert(0, os.path.dirname(__file__))
from database import engine, SessionLocal, Base
from models import *
def _hash(pin): return hashlib.sha256(pin.encode()).hexdigest()
def seed_more():
db = SessionLocal()
def get_or_create_district(id, name):
d = db.query(District).filter(District.district_id == id).first()
if not d:
d = District(district_id=id, name=name, state_code="GJ")
db.add(d)
return d
def get_or_create_school(id, udise, name, dist_id):
s = db.query(School).filter(School.school_id == id).first()
if not s:
s = School(school_id=id, udise_code=udise, name=name, district_id=dist_id, block="City", programme_id="prog-1")
db.add(s)
return s
d2 = get_or_create_district("dist-ahmedabad", "Ahmedabad")
s2 = get_or_create_school("school-2", "240701001", "Ahmedabad Primary #1", "dist-ahmedabad")
s3 = get_or_create_school("school-3", "240701002", "Ahmedabad Primary #2", "dist-ahmedabad")
s4 = get_or_create_school("school-4", "2430101002", "Surat Municipal School #8", "dist-surat")
db.commit()
# Add classes for these schools if they don't have any
classes = []
for s_id in ["school-2", "school-3", "school-4"]:
existing_classes = db.query(ClassAccount).filter(ClassAccount.school_id == s_id).all()
if not existing_classes:
for _ in range(3):
c_id = f"class-{uuid.uuid4().hex[:6]}"
cls = ClassAccount(class_id=c_id, school_id=s_id, grade=random.randint(1,5), section=random.choice(["A", "B", "C"]), year=2026, state="in_progress", reading_level=random.choice(["new_reader", "growing_reader", "fluent_reader", "confident_reader"]))
db.add(cls)
classes.append(c_id)
# Journey
j = ClassJourney(class_id=c_id, total_books_read=random.randint(5, 25), current_book_index=random.randint(1, 3))
j.completed_cluster_ids = ["cl-l1-animals", "cl-l3-nature"][:random.randint(1,2)]
db.add(j)
else:
classes.extend([c.class_id for c in existing_classes])
db.commit()
# Seed more teachers
teachers = [
{"name": "Sarah Khan", "email": "sarah@school.in", "phone": "9900011122"},
{"name": "Rajesh Gupta", "email": "raj@school.in", "phone": "9900011133"},
{"name": "Priya Sharma", "email": "priya@school.in", "phone": "9900011144"},
{"name": "Amit Shah", "email": "amit@school.in", "phone": "9900011155"}
]
for t in teachers:
acc = db.query(TeacherAccount).filter(TeacherAccount.email == t['email']).first()
if not acc:
acc = TeacherAccount(
teacher_id=f"t-{t['email'].split('@')[0]}",
name=t['name'],
email=t['email'],
phone=t['phone'],
password_hash=_hash("teacher123"),
is_active=True
)
db.add(acc)
# Update classes with whatsapp links
for c_id in classes:
cls = db.query(ClassAccount).filter(ClassAccount.class_id == c_id).first()
if cls:
cls.whatsapp_group_link = f"https://chat.whatsapp.com/demo-{c_id[:4]}"
# Seed Field Staff
staff = db.query(FieldStaff).filter(FieldStaff.staff_id == "staff-2").first()
if not staff:
staff = FieldStaff(staff_id="staff-2", name="Ravi Kumar", email="ravi@drp.in", password_hash=_hash("staff123"), district_id="dist-ahmedabad", assigned_blocks=["City"])
db.add(staff)
db.flush()
for s_id in ["school-2", "school-3", "school-4"]:
db.add(SchoolStaffMapping(staff_id="staff-2", school_id=s_id, is_primary=True))
db.commit()
db.close()
print("[OK] More mock data added/updated!")
if __name__ == "__main__":
seed_more()