Explorar el Código

Añado comando de importación de lugares

Tino hace 3 meses
padre
commit
33df54f028

+ 1 - 1
K8S/.current_version

@@ -1 +1 @@
-alpine-23
+alpine-24

+ 1 - 1
K8S/.version

@@ -1 +1 @@
-alpine-24
+alpine-25

+ 42 - 0
src/eventos/management/commands/importar_lugares.py

@@ -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."))

+ 1 - 1
src/extraeDatos.sh

@@ -4,7 +4,7 @@ curl https://jugaralpadel.es/usuarios/api/usuarios/ > mediafiles/usuarios.json
 echo "extrayendo ayuda..."
 curl https://jugaralpadel.es/api/ayuda/ > mediafiles/ayuda.json
 
-for i in eventos reservas noticias listaespera
+for i in eventos reservas noticias listaespera lugares
 do
 	echo "extrayendo $i..."
 	curl https://jugaralpadel.es/eventos/api/$i/ > mediafiles/$i.json

+ 1 - 0
src/importaDatos.sh

@@ -2,6 +2,7 @@
 ./manage.py importar_ayuda mediafiles/ayuda.json 
 ./manage.py importar_noticias mediafiles/noticias.json 
 ./manage.py importar_eventos mediafiles/eventos.json 
+./manage.py importar_lugares mediafiles/lugares.json 
 ./manage.py importar_reservas mediafiles/reservas.json 
 ./manage.py importar_listaespera mediafiles/listaespera.json