Browse Source

Añado comandos para importar json

politica
Celestino Rey 10 months ago
parent
commit
71b1f17b7a
4 changed files with 77 additions and 0 deletions
  1. +0
    -0
      ReyMotaAppsDj/reymota/repostajes/management/__init__.py
  2. +0
    -0
      ReyMotaAppsDj/reymota/repostajes/management/commands/__init__.py
  3. +41
    -0
      ReyMotaAppsDj/reymota/repostajes/management/commands/importar_repostajes.py
  4. +36
    -0
      ReyMotaAppsDj/reymota/repostajes/management/commands/importar_vehiculos.py

+ 0
- 0
ReyMotaAppsDj/reymota/repostajes/management/__init__.py View File


+ 0
- 0
ReyMotaAppsDj/reymota/repostajes/management/commands/__init__.py View File


+ 41
- 0
ReyMotaAppsDj/reymota/repostajes/management/commands/importar_repostajes.py View File

@ -0,0 +1,41 @@
import json
from django.core.management.base import BaseCommand
from repostajes.models import Repostaje
class Command(BaseCommand):
help = "Importa repostajes 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)
repostajes_creados = 0
for repostaje_data in datos:
repostaje, creado = Evento.objects.get_or_create(
fecha=repostaje_data['fecha'],
defaults={
'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']
'vehiculo': repostaje_data['vehiculo']
}
)
if creado:
repostajes_creados += 1
self.stdout.write(self.style.SUCCESS(f'Se importaron {repostajes_creados} repostajes 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."))

+ 36
- 0
ReyMotaAppsDj/reymota/repostajes/management/commands/importar_vehiculos.py View File

@ -0,0 +1,36 @@
import json
from django.core.management.base import BaseCommand
from repostajes.models import Vehiculo
class Command(BaseCommand):
help = "Importa vehiculos 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)
vehiculos_creados = 0
for vehiculo_data in datos:
vehiculo, creado = Evento.objects.get_or_create(
marca=vehiculo_data['marca'],
defaults={
'modelo': vehiculo_data['modelo'],
'matricula': vehiculo_data['matricula'],
'foto': vehiculo_data['foto']
}
)
if creado:
vehiculos_creados += 1
self.stdout.write(self.style.SUCCESS(f'Se importaron {vehiculos_creados} vehiculos 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."))

Loading…
Cancel
Save