120 lines
4.0 KiB
Python
120 lines
4.0 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""
|
||
|
|
Seed School Calendars Script
|
||
|
|
Loads school calendars from CalendarRegistry into the database
|
||
|
|
"""
|
||
|
|
|
||
|
|
import asyncio
|
||
|
|
import sys
|
||
|
|
import os
|
||
|
|
|
||
|
|
# Add parent directory to path to allow imports
|
||
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||
|
|
|
||
|
|
from app.core.database import database_manager
|
||
|
|
from app.repositories.calendar_repository import CalendarRepository
|
||
|
|
from app.registry.calendar_registry import CalendarRegistry
|
||
|
|
import structlog
|
||
|
|
|
||
|
|
logger = structlog.get_logger()
|
||
|
|
|
||
|
|
|
||
|
|
async def seed_calendars():
|
||
|
|
"""Seed school calendars from registry into database"""
|
||
|
|
|
||
|
|
logger.info("Starting school calendar seeding...")
|
||
|
|
|
||
|
|
# Get all calendars from registry
|
||
|
|
calendars = CalendarRegistry.get_all_calendars()
|
||
|
|
logger.info(f"Found {len(calendars)} calendars in registry")
|
||
|
|
|
||
|
|
# Initialize database
|
||
|
|
await database_manager.initialize()
|
||
|
|
|
||
|
|
try:
|
||
|
|
async with database_manager.get_session() as session:
|
||
|
|
repo = CalendarRepository(session)
|
||
|
|
|
||
|
|
seeded_count = 0
|
||
|
|
skipped_count = 0
|
||
|
|
|
||
|
|
for cal_def in calendars:
|
||
|
|
logger.info(
|
||
|
|
"Processing calendar",
|
||
|
|
calendar_id=cal_def.calendar_id,
|
||
|
|
city=cal_def.city_id,
|
||
|
|
type=cal_def.school_type.value
|
||
|
|
)
|
||
|
|
|
||
|
|
# Check if calendar already exists
|
||
|
|
existing = await repo.get_calendar_by_city_type_year(
|
||
|
|
city_id=cal_def.city_id,
|
||
|
|
school_type=cal_def.school_type.value,
|
||
|
|
academic_year=cal_def.academic_year
|
||
|
|
)
|
||
|
|
|
||
|
|
if existing:
|
||
|
|
logger.info(
|
||
|
|
"Calendar already exists, skipping",
|
||
|
|
calendar_id=cal_def.calendar_id
|
||
|
|
)
|
||
|
|
skipped_count += 1
|
||
|
|
continue
|
||
|
|
|
||
|
|
# Convert holiday periods to dict format
|
||
|
|
holiday_periods = [
|
||
|
|
{
|
||
|
|
"name": hp.name,
|
||
|
|
"start_date": hp.start_date,
|
||
|
|
"end_date": hp.end_date,
|
||
|
|
"description": hp.description
|
||
|
|
}
|
||
|
|
for hp in cal_def.holiday_periods
|
||
|
|
]
|
||
|
|
|
||
|
|
# Convert school hours to dict format
|
||
|
|
school_hours = {
|
||
|
|
"morning_start": cal_def.school_hours.morning_start,
|
||
|
|
"morning_end": cal_def.school_hours.morning_end,
|
||
|
|
"has_afternoon_session": cal_def.school_hours.has_afternoon_session,
|
||
|
|
"afternoon_start": cal_def.school_hours.afternoon_start,
|
||
|
|
"afternoon_end": cal_def.school_hours.afternoon_end
|
||
|
|
}
|
||
|
|
|
||
|
|
# Create calendar in database
|
||
|
|
created_calendar = await repo.create_school_calendar(
|
||
|
|
city_id=cal_def.city_id,
|
||
|
|
calendar_name=cal_def.calendar_name,
|
||
|
|
school_type=cal_def.school_type.value,
|
||
|
|
academic_year=cal_def.academic_year,
|
||
|
|
holiday_periods=holiday_periods,
|
||
|
|
school_hours=school_hours,
|
||
|
|
source=cal_def.source,
|
||
|
|
enabled=cal_def.enabled
|
||
|
|
)
|
||
|
|
|
||
|
|
logger.info(
|
||
|
|
"Calendar seeded successfully",
|
||
|
|
calendar_id=str(created_calendar.id),
|
||
|
|
city=cal_def.city_id,
|
||
|
|
type=cal_def.school_type.value
|
||
|
|
)
|
||
|
|
seeded_count += 1
|
||
|
|
|
||
|
|
logger.info(
|
||
|
|
"Calendar seeding completed",
|
||
|
|
seeded=seeded_count,
|
||
|
|
skipped=skipped_count,
|
||
|
|
total=len(calendars)
|
||
|
|
)
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
logger.error("Error seeding calendars", error=str(e))
|
||
|
|
raise
|
||
|
|
finally:
|
||
|
|
await database_manager.close()
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
asyncio.run(seed_calendars())
|