|
|
|
@ -1,10 +1,10 @@ |
|
|
|
import json |
|
|
|
from django.core.management.base import BaseCommand |
|
|
|
from repostajes.models import Vehiculo |
|
|
|
from macrociclos.models import Macrociclo |
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand): |
|
|
|
help = "Importa vehiculos desde un archivo JSON" |
|
|
|
help = "Importa macrociclos desde un archivo JSON" |
|
|
|
|
|
|
|
def add_arguments(self, parser): |
|
|
|
parser.add_argument('archivo_json', type=str, help="Ruta del archivo JSON") |
|
|
|
@ -16,25 +16,27 @@ class Command(BaseCommand): |
|
|
|
with open(archivo_json, 'r', encoding='utf-8') as file: |
|
|
|
datos = json.load(file) |
|
|
|
|
|
|
|
self.stdout.write(self.style.WARNING(f"\nSe encontraron {len(datos)} vehiculos en el archivo '{archivo_json}'.")) |
|
|
|
self.stdout.write(self.style.WARNING(f"\nSe encontraron {len(datos)} macrociclos en el archivo '{archivo_json}'.")) |
|
|
|
confirmar = input("¿Deseas continuar con la importación? (s/n): ").strip().lower() |
|
|
|
|
|
|
|
if confirmar != 's': |
|
|
|
self.stdout.write(self.style.ERROR("Importación cancelada.")) |
|
|
|
return |
|
|
|
|
|
|
|
vehiculos_creados = 0 |
|
|
|
for vehiculo_data in datos: |
|
|
|
creado = Vehiculo.objects.create( |
|
|
|
marca=vehiculo_data['marca'], |
|
|
|
modelo=vehiculo_data['modelo'], |
|
|
|
matricula=vehiculo_data['matricula'], |
|
|
|
foto=vehiculo_data['foto'] |
|
|
|
macrociclos_creados = 0 |
|
|
|
for macrociclo_data in datos: |
|
|
|
creado = Macrociclo.objects.create( |
|
|
|
nombre=macrociclo_data['nombre'], |
|
|
|
tipo=macrociclo_data['tipo'], |
|
|
|
desde=macrociclo_data['desde'], |
|
|
|
hasta=macrociclo_data['hasta'] |
|
|
|
deportista=macrociclo_data['deportista'] |
|
|
|
entrenador=macrociclo_data['entrenador'] |
|
|
|
) |
|
|
|
if creado: |
|
|
|
vehiculos_creados += 1 |
|
|
|
macrociclos_creados += 1 |
|
|
|
|
|
|
|
self.stdout.write(self.style.SUCCESS(f'Se importaron {vehiculos_creados} vehiculos correctamente.')) |
|
|
|
self.stdout.write(self.style.SUCCESS(f'Se importaron {macrociclos_creados} macrociclos correctamente.')) |
|
|
|
|
|
|
|
except FileNotFoundError: |
|
|
|
self.stderr.write(self.style.ERROR(f"El archivo {archivo_json} no se encontró.")) |