diff --git a/src/macrociclos/admin.py b/src/macrociclos/admin.py index 4805a7b..eab3bba 100644 --- a/src/macrociclos/admin.py +++ b/src/macrociclos/admin.py @@ -3,7 +3,8 @@ from django.contrib import admin # Register your models here. -from .models import Vehiculo, Repostaje +from .models import Entrenador, Deportista, Macrociclo -admin.site.register(Vehiculo) -admin.site.register(Repostaje) +admin.site.register(Entrenador) +admin.site.register(Deportista) +admin.site.register(Macrociclo) diff --git a/src/macrociclos/management/commands/importar_deportistas.py b/src/macrociclos/management/commands/importar_deportistas.py new file mode 100644 index 0000000..33b1e8b --- /dev/null +++ b/src/macrociclos/management/commands/importar_deportistas.py @@ -0,0 +1,47 @@ +import json +from django.core.management.base import BaseCommand +from entrenadores.models import Deportista + + +class Command(BaseCommand): + help = "Importa deportistas 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)} deportistas 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 + + entrenadores_creados = 0 + for entrenador_data in datos: + try: + deportista = Deportista.objects.get(matricula=entrenador_data["deportista_matricula"]) + + creado = Deportista.objects.create( + deportista=deportista, + nombre=entrenador_data['nombre'], + entrenador=entrenador_data['entrenador'] + ) + if creado: + entrenadores_creados += 1 + + except Deportista.DoesNotExist: + self.stderr.write(self.style.ERROR(f"Deportista con matrícula '{entrenador_data['deportista_matricula']}' no encontrado.")) + + self.stdout.write(self.style.SUCCESS(f'Se importaron {entrenadores_creados} entrenadores 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.")) diff --git a/src/macrociclos/management/commands/importar_repostajes.py b/src/macrociclos/management/commands/importar_entrenadores.py similarity index 63% rename from src/macrociclos/management/commands/importar_repostajes.py rename to src/macrociclos/management/commands/importar_entrenadores.py index 7120eb5..f0c2c7a 100644 --- a/src/macrociclos/management/commands/importar_repostajes.py +++ b/src/macrociclos/management/commands/importar_entrenadores.py @@ -1,6 +1,6 @@ import json from django.core.management.base import BaseCommand -from entrenadores.models import Entrenador, Macrociclo +from entrenadores.models import Entrenador class Command(BaseCommand): @@ -26,24 +26,17 @@ class Command(BaseCommand): entrenadores_creados = 0 for entrenador_data in datos: try: - macrociclo = Macrociclo.objects.get(matricula=entrenador_data["macrociclo_matricula"]) + entrenador = Entrenador.objects.get(matricula=entrenador_data["entrenador_matricula"]) creado = Entrenador.objects.create( - macrociclo=macrociclo, - fecha=entrenador_data['fecha'], - kms=entrenador_data['kms'], - litros=entrenador_data['litros'], - descuento=entrenador_data['descuento'], - importe=entrenador_data['importe'], - precioxlitro=entrenador_data['precioxlitro'], - kmsrecorridos=entrenador_data['kmsrecorridos'], - consumo=entrenador_data['consumo'] + entrenador=entrenador, + nombre=entrenador_data['nombre'] ) if creado: entrenadores_creados += 1 - except Macrociclo.DoesNotExist: - self.stderr.write(self.style.ERROR(f"Macrociclo con matrícula '{entrenador_data['macrociclo_matricula']}' no encontrado.")) + except Entrenador.DoesNotExist: + self.stderr.write(self.style.ERROR(f"Entrenador con matrícula '{entrenador_data['entrenador_matricula']}' no encontrado.")) self.stdout.write(self.style.SUCCESS(f'Se importaron {entrenadores_creados} entrenadores correctamente.')) diff --git a/src/macrociclos/management/commands/importar_vehiculos.py b/src/macrociclos/management/commands/importar_macrociclos.py similarity index 61% rename from src/macrociclos/management/commands/importar_vehiculos.py rename to src/macrociclos/management/commands/importar_macrociclos.py index e3ae66b..54b3cb3 100644 --- a/src/macrociclos/management/commands/importar_vehiculos.py +++ b/src/macrociclos/management/commands/importar_macrociclos.py @@ -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ó.")) diff --git a/src/macrociclos/views.py b/src/macrociclos/views.py index 10303a2..88635bb 100644 --- a/src/macrociclos/views.py +++ b/src/macrociclos/views.py @@ -133,6 +133,9 @@ def eliminar_entrenador(request, entrenador_id): entrenador.delete() return redirect('macrociclos:lista_deportistas') +# +# API +# @api_view(['GET']) def api_lista_entrenadores(request): diff --git a/src/templates/repostajes/_menu-repostajes.html b/src/templates/repostajes/_menu-entrenadores.html similarity index 100% rename from src/templates/repostajes/_menu-repostajes.html rename to src/templates/repostajes/_menu-entrenadores.html diff --git a/src/templates/repostajes/detalle_repostaje.html b/src/templates/repostajes/detalle_deportista.html similarity index 100% rename from src/templates/repostajes/detalle_repostaje.html rename to src/templates/repostajes/detalle_deportista.html diff --git a/src/templates/repostajes/detalle_vehiculo.html b/src/templates/repostajes/detalle_entrenador.html similarity index 100% rename from src/templates/repostajes/detalle_vehiculo.html rename to src/templates/repostajes/detalle_entrenador.html diff --git a/src/templates/repostajes/form_repostaje.html b/src/templates/repostajes/form_deportista.html similarity index 100% rename from src/templates/repostajes/form_repostaje.html rename to src/templates/repostajes/form_deportista.html diff --git a/src/templates/repostajes/form_vehiculo.html b/src/templates/repostajes/form_entrenador.html similarity index 100% rename from src/templates/repostajes/form_vehiculo.html rename to src/templates/repostajes/form_entrenador.html diff --git a/src/templates/repostajes/lista_repostajes.html b/src/templates/repostajes/lista_deportistas.html similarity index 100% rename from src/templates/repostajes/lista_repostajes.html rename to src/templates/repostajes/lista_deportistas.html diff --git a/src/templates/repostajes/lista_vehiculos.html b/src/templates/repostajes/lista_entrenadores.html similarity index 100% rename from src/templates/repostajes/lista_vehiculos.html rename to src/templates/repostajes/lista_entrenadores.html