import os import sys import uuid # Dodaj główny katalog do PYTHONPATH, aby moduły z 'core' były widoczne sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from sqlalchemy.orm import Session from core.subscription.db import SessionLocal from core.projects.models import Project, ProjectSection, ProjectSectionTemplate def fix_empty(): db: Session = SessionLocal() try: projects = db.query(Project).all() count = 0 for project in projects: sections = ( db.query(ProjectSection) .filter(ProjectSection.project_id == project.id) .all() ) if not sections: templates = ( db.query(ProjectSectionTemplate) .filter(ProjectSectionTemplate.program_type == project.program_type) .order_by(ProjectSectionTemplate.order.asc()) .all() ) if not templates: templates = ( db.query(ProjectSectionTemplate) .filter(ProjectSectionTemplate.program_type == "SMART") .order_by(ProjectSectionTemplate.order.asc()) .all() ) for tmpl in templates: db.add( ProjectSection( id=str(uuid.uuid4()), project_id=project.id, section_type=tmpl.section_type, order=tmpl.order, content="", is_approved=False, generated_by_ai=False, ) ) count += 1 db.commit() print(f"Fixed {count} empty projects.") finally: db.close() if __name__ == "__main__": fix_empty()