Improve the frontend 5
This commit is contained in:
@@ -9,8 +9,10 @@ import structlog
|
||||
import asyncio
|
||||
|
||||
from app.registry.city_registry import CityRegistry
|
||||
from app.registry.calendar_registry import CalendarRegistry
|
||||
from .adapters import get_adapter
|
||||
from app.repositories.city_data_repository import CityDataRepository
|
||||
from app.repositories.calendar_repository import CalendarRepository
|
||||
from app.core.database import database_manager
|
||||
|
||||
logger = structlog.get_logger()
|
||||
@@ -266,3 +268,99 @@ class DataIngestionManager:
|
||||
error=str(e)
|
||||
)
|
||||
return False
|
||||
|
||||
async def seed_school_calendars(self) -> bool:
|
||||
"""
|
||||
Seed school calendars from CalendarRegistry into database
|
||||
Called during initialization - idempotent
|
||||
"""
|
||||
try:
|
||||
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")
|
||||
|
||||
async with self.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,
|
||||
year=cal_def.academic_year
|
||||
)
|
||||
|
||||
# Check if calendar already exists (idempotency)
|
||||
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,
|
||||
year=cal_def.academic_year
|
||||
)
|
||||
seeded_count += 1
|
||||
|
||||
logger.info(
|
||||
"School calendar seeding completed",
|
||||
seeded=seeded_count,
|
||||
skipped=skipped_count,
|
||||
total=len(calendars)
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error seeding school calendars", error=str(e))
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user