|
|
@ -1,10 +1,10 @@ |
|
|
import json |
|
|
import json |
|
|
from django.core.management.base import BaseCommand |
|
|
from django.core.management.base import BaseCommand |
|
|
from repostajes.models import Repostaje, Vehiculo |
|
|
|
|
|
|
|
|
from entrenadores.models import Entrenador, Macrociclo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand): |
|
|
class Command(BaseCommand): |
|
|
help = "Importa repostajes desde un archivo JSON" |
|
|
|
|
|
|
|
|
help = "Importa entrenadores desde un archivo JSON" |
|
|
|
|
|
|
|
|
def add_arguments(self, parser): |
|
|
def add_arguments(self, parser): |
|
|
parser.add_argument('archivo_json', type=str, help="Ruta del archivo JSON") |
|
|
parser.add_argument('archivo_json', type=str, help="Ruta del archivo JSON") |
|
|
@ -16,36 +16,36 @@ class Command(BaseCommand): |
|
|
with open(archivo_json, 'r', encoding='utf-8') as file: |
|
|
with open(archivo_json, 'r', encoding='utf-8') as file: |
|
|
datos = json.load(file) |
|
|
datos = json.load(file) |
|
|
|
|
|
|
|
|
self.stdout.write(self.style.WARNING(f"\nSe encontraron {len(datos)} repostajes en el archivo '{archivo_json}'.")) |
|
|
|
|
|
|
|
|
self.stdout.write(self.style.WARNING(f"\nSe encontraron {len(datos)} entrenadores en el archivo '{archivo_json}'.")) |
|
|
confirmar = input("¿Deseas continuar con la importación? (s/n): ").strip().lower() |
|
|
confirmar = input("¿Deseas continuar con la importación? (s/n): ").strip().lower() |
|
|
|
|
|
|
|
|
if confirmar != 's': |
|
|
if confirmar != 's': |
|
|
self.stdout.write(self.style.ERROR("Importación cancelada.")) |
|
|
self.stdout.write(self.style.ERROR("Importación cancelada.")) |
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
repostajes_creados = 0 |
|
|
|
|
|
for repostaje_data in datos: |
|
|
|
|
|
|
|
|
entrenadores_creados = 0 |
|
|
|
|
|
for entrenador_data in datos: |
|
|
try: |
|
|
try: |
|
|
vehiculo = Vehiculo.objects.get(matricula=repostaje_data["vehiculo_matricula"]) |
|
|
|
|
|
|
|
|
|
|
|
creado = Repostaje.objects.create( |
|
|
|
|
|
vehiculo=vehiculo, |
|
|
|
|
|
fecha=repostaje_data['fecha'], |
|
|
|
|
|
kms=repostaje_data['kms'], |
|
|
|
|
|
litros=repostaje_data['litros'], |
|
|
|
|
|
descuento=repostaje_data['descuento'], |
|
|
|
|
|
importe=repostaje_data['importe'], |
|
|
|
|
|
precioxlitro=repostaje_data['precioxlitro'], |
|
|
|
|
|
kmsrecorridos=repostaje_data['kmsrecorridos'], |
|
|
|
|
|
consumo=repostaje_data['consumo'] |
|
|
|
|
|
|
|
|
macrociclo = Macrociclo.objects.get(matricula=entrenador_data["macrociclo_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'] |
|
|
) |
|
|
) |
|
|
if creado: |
|
|
if creado: |
|
|
repostajes_creados += 1 |
|
|
|
|
|
|
|
|
entrenadores_creados += 1 |
|
|
|
|
|
|
|
|
except Vehiculo.DoesNotExist: |
|
|
|
|
|
self.stderr.write(self.style.ERROR(f"Vehiculo con matrícula '{repostaje_data['vehiculo_matricula']}' no encontrado.")) |
|
|
|
|
|
|
|
|
except Macrociclo.DoesNotExist: |
|
|
|
|
|
self.stderr.write(self.style.ERROR(f"Macrociclo con matrícula '{entrenador_data['macrociclo_matricula']}' no encontrado.")) |
|
|
|
|
|
|
|
|
self.stdout.write(self.style.SUCCESS(f'Se importaron {repostajes_creados} repostajes correctamente.')) |
|
|
|
|
|
|
|
|
self.stdout.write(self.style.SUCCESS(f'Se importaron {entrenadores_creados} entrenadores correctamente.')) |
|
|
|
|
|
|
|
|
except FileNotFoundError: |
|
|
except FileNotFoundError: |
|
|
self.stderr.write(self.style.ERROR(f"El archivo {archivo_json} no se encontró.")) |
|
|
self.stderr.write(self.style.ERROR(f"El archivo {archivo_json} no se encontró.")) |
|
|
|