| @ -0,0 +1,48 @@ | |||
| import json | |||
| from django.core.management.base import BaseCommand | |||
| from lyrics.models import Album, Artista | |||
| class Command(BaseCommand): | |||
| help = "Importa albumes 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)} albumes 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 | |||
| albumes_creados = 0 | |||
| for album_data in datos: | |||
| try: | |||
| artista = Artista.objects.get(id=album_data["artist"]) | |||
| creado = Album.objects.create( | |||
| artist_id=artista.id, | |||
| name=album_data['name'], | |||
| year=album_data['year'], | |||
| cover_image=album_data['cover_image'], | |||
| ) | |||
| if creado: | |||
| albumes_creados += 1 | |||
| except Artista.DoesNotExist: | |||
| self.stderr.write(self.style.ERROR(f"Artista '{album_data['artista']}' no encontrado.")) | |||
| self.stdout.write(self.style.SUCCESS(f'Se importaron {albumes_creados} albumes 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.")) | |||
| @ -0,0 +1,42 @@ | |||
| import json | |||
| from django.core.management.base import BaseCommand | |||
| from lyrics.models import Artista | |||
| class Command(BaseCommand): | |||
| help = "Importa artistas 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)} artistas 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 | |||
| artistas_creados = 0 | |||
| for artista_data in datos: | |||
| creado = Artista.objects.create( | |||
| id=artista_data['id'], | |||
| nombre=artista_data['nombre'], | |||
| biografia=artista_data['biografia'], | |||
| foto=artista_data['foto'] | |||
| ) | |||
| if creado: | |||
| artistas_creados += 1 | |||
| self.stdout.write(self.style.SUCCESS(f'Se importaron {artistas_creados} artistas 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.")) | |||
| @ -0,0 +1,51 @@ | |||
| import json | |||
| from django.core.management.base import BaseCommand | |||
| from lyrics.models import Song, Album, Artista | |||
| class Command(BaseCommand): | |||
| help = "Importa canciones 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)} canciones 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 | |||
| canciones_creados = 0 | |||
| for cancion_data in datos: | |||
| try: | |||
| album = Album.objects.get(id=cancion_data["album"]) | |||
| artista = Artista.objects.get(id=cancion_data["artist"]) | |||
| creado = Song.objects.create( | |||
| album_id=album.id, | |||
| title=cancion_data['title'], | |||
| artist_id=artista.id, | |||
| year=cancion_data['year'], | |||
| lyrics=cancion_data['lyrics'], | |||
| pista=cancion_data['pista'], | |||
| ) | |||
| if creado: | |||
| canciones_creados += 1 | |||
| except Album.DoesNotExist: | |||
| self.stderr.write(self.style.ERROR(f"Album '{cancion_data['album']}' no encontrado.")) | |||
| self.stdout.write(self.style.SUCCESS(f'Se importaron {canciones_creados} canciones 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.")) | |||
| @ -0,0 +1,53 @@ | |||
| import json | |||
| from django.core.management.base import BaseCommand | |||
| from repostajes.models import Repostaje, Vehiculo | |||
| 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) | |||
| self.stdout.write(self.style.WARNING(f"\nSe encontraron {len(datos)} repostajes 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 | |||
| repostajes_creados = 0 | |||
| for repostaje_data in datos: | |||
| try: | |||
| vehiculo = Vehiculo.objects.get(id=repostaje_data["vehiculo"]) | |||
| creado = Repostaje.objects.create( | |||
| vehiculo_id=vehiculo.id, | |||
| 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'] | |||
| ) | |||
| if creado: | |||
| repostajes_creados += 1 | |||
| except Vehiculo.DoesNotExist: | |||
| self.stderr.write(self.style.ERROR(f"Vehiculo '{repostaje_data['vehiculo']}' no encontrado.")) | |||
| 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.")) | |||
| @ -0,0 +1,43 @@ | |||
| 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) | |||
| self.stdout.write(self.style.WARNING(f"\nSe encontraron {len(datos)} vehiculos 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( | |||
| id=vehiculo_data['id'], | |||
| marca=vehiculo_data['marca'], | |||
| 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.")) | |||
| @ -0,0 +1,48 @@ | |||
| import json | |||
| from django.core.management.base import BaseCommand | |||
| from reymotausers.models import ReyMotaUser | |||
| class Command(BaseCommand): | |||
| help = "Importa usuarios 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)} usuarios 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 | |||
| usuarios_creados = 0 | |||
| for usuario_data in datos: | |||
| creado = ReyMotaUser.objects.create( | |||
| foto=usuario_data['foto'], | |||
| password=usuario_data['password'], | |||
| is_superuser=usuario_data['is_superuser'], | |||
| is_staff=usuario_data['is_staff'], | |||
| is_active=usuario_data['is_active'], | |||
| nombre=usuario_data['nombre'], | |||
| email=usuario_data['email'], | |||
| groups=usuario_data['groups'], | |||
| user_permissions=usuario_data['user_permissions'], | |||
| last_login=usuario_data['last_login'] | |||
| ) | |||
| if creado: | |||
| usuarios_creados += 1 | |||
| self.stdout.write(self.style.SUCCESS(f'Se importaron {usuarios_creados} usuarios 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.")) | |||
| @ -0,0 +1,9 @@ | |||
| from django.urls import path | |||
| from . import views | |||
| from .views import api_lista_usuarios, api_detalle_usuario | |||
| urlpatterns = [ | |||
| path('api/usuarios/', api_lista_usuarios, name='api_lista_usuarios'), | |||
| path('api/usuarios/<int:usuario_id>/', api_detalle_usuario, name='api_detalle_usuario'), | |||
| ] | |||
| @ -1,3 +1,29 @@ | |||
| from django.shortcuts import render | |||
| from rest_framework.response import Response | |||
| from rest_framework.decorators import api_view | |||
| from .serializers import ReyMotaUserSerializer | |||
| from .models import ReyMotaUser | |||
| # Create your views here. | |||
| @api_view(['GET']) | |||
| def api_lista_usuarios(request): | |||
| """Devuelve la lista de todos los usuarios.""" | |||
| usuarios = ReyMotaUser.objects.all() | |||
| serializer = ReyMotaUserSerializer(usuarios, many=True) | |||
| return Response(serializer.data) | |||
| @api_view(['GET']) | |||
| def api_detalle_usuario(request, usuario_id): | |||
| """Devuelve los detalles de un usuario específico.""" | |||
| try: | |||
| usuario = ReyMotaUser.objects.get(id=usuario_id) | |||
| serializer = ReyMotaUserSerializer(usuario) | |||
| return Response(serializer.data) | |||
| except ReyMotaUser.DoesNotExist: | |||
| return Response({'error': 'Canción no encontrada'}, status=404) | |||