File size: 1,985 Bytes
afd56bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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()