File size: 2,319 Bytes
68e9069
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# patch_api_generator.py
import os
import shutil
from pathlib import Path

def fix_generated_api(api_dir):
    """Corrige automáticamente las APIs generadas"""
    
    # 1. Corregir models.py
    models_path = Path(api_dir) / "app" / "models.py"
    if models_path.exists():
        fixed_models = '''from sqlalchemy import Column, Integer, String, Float, Boolean, DateTime, Text
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime

Base = declarative_base()

class Item(Base):
    __tablename__ = "items"
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    nombre = Column(String(255), nullable=True)
    descripcion = Column(Text, nullable=True)
    precio = Column(Float, nullable=True)
    activo = Column(Boolean, default=True)
    fecha_creacion = Column(DateTime, default=datetime.utcnow)
    
    # Columnas dinámicas basadas en datos reales
    def add_columns_from_dataframe(self, df):
        \"\"\"Método para añadir columnas dinámicamente\"\"\"
        for col in df.columns:
            if col not in ['id', 'fecha_creacion']:
                setattr(Item, col, Column(Text, nullable=True))
'''
        models_path.write_text(fixed_models)
        print(f"✅ models.py corregido en {api_dir}")

    # 2. Corregir schemas.py
    schemas_path = Path(api_dir) / "app" / "schemas.py"
    if schemas_path.exists():
        fixed_schemas = '''from pydantic import BaseModel
from datetime import datetime
from typing import Optional

class ItemBase(BaseModel):
    nombre: Optional[str] = None
    descripcion: Optional[str] = None
    precio: Optional[float] = None
    activo: Optional[bool] = True

class ItemCreate(ItemBase):
    pass

class ItemUpdate(ItemBase):
    pass

class Item(ItemBase):
    id: int
    fecha_creacion: Optional[datetime] = None
    
    class Config:
        from_attributes = True
'''
        schemas_path.write_text(fixed_schemas)
        print(f"✅ schemas.py corregido en {api_dir}")

# Aplicar parche a todas las APIs generadas
api_dirs = Path("exports/apis").glob("*")
for api_dir in api_dirs:
    if api_dir.is_dir():
        fix_generated_api(api_dir)

print("🎯 Parche aplicado a todas las APIs existentes")
print("📌 Para futuras generaciones, corrige las plantillas en backend/api_generator/")