|
|
@@ -0,0 +1,42 @@
|
|
|
+import json
|
|
|
+from django.core.management.base import BaseCommand
|
|
|
+from eventos.models import Lugar
|
|
|
+
|
|
|
+
|
|
|
+class Command(BaseCommand):
|
|
|
+ help = "Importa lugares desde un archivo JSON"
|
|
|
+
|
|
|
+ def add_arguments(self, parser):
|
|
|
+ parser.add_argument('archivo_json', type=str, help="Ruta del archivo JSON")
|
|
|
+
|
|
|
+ def handle(self, *args, **kwargs):
|
|
|
+ archivo_json = kwargs['archivo_json']
|
|
|
+
|
|
|
+ try:
|
|
|
+ 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)} lugares 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
|
|
|
+
|
|
|
+ lugares_creados = 0
|
|
|
+ for lugar_data in datos:
|
|
|
+ creado = Lugar.objects.create(
|
|
|
+ id=lugar_data['id'],
|
|
|
+ nombre=lugar_data['nombre'],
|
|
|
+ url_ubicacion=lugar_data['url_ubicacion'],
|
|
|
+ notas=lugar_data.get('notas'),
|
|
|
+ )
|
|
|
+ if creado:
|
|
|
+ lugares_creados += 1
|
|
|
+
|
|
|
+ self.stdout.write(self.style.SUCCESS(f'Se importaron {lugares_creados} lugares correctamente.'))
|
|
|
+
|
|
|
+ except FileNotFoundError:
|
|
|
+ self.stderr.write(self.style.ERROR(f"El archivo {archivo_json} no se encontró."))
|
|
|
+ except json.JSONDecodeError:
|
|
|
+ self.stderr.write(self.style.ERROR("Error al leer el archivo JSON. Asegúrate de que el formato sea correcto."))
|