| @ -1,23 +0,0 @@ | |||||
| from django.core.management.base import BaseCommand, CommandError | |||||
| from lyrics.models import Album, Artista, Song | |||||
| import csv | |||||
| import argparse | |||||
| from datetime import datetime | |||||
| class Command(BaseCommand): | |||||
| help = "Importa la lista de letras" | |||||
| def add_arguments(self, parser): | |||||
| parser.add_argument("fichero_csv", type=str, help='Ruta al fichero csv') | |||||
| def handle(self, *args, **options): | |||||
| fichero = options["fichero_csv"] | |||||
| with open(fichero, 'r') as file: | |||||
| reader = csv.DictReader(file) | |||||
| for row in reader: | |||||
| name = row['name'], | |||||
| artist = row['artist'], | |||||
| year = row['year'], | |||||
| cover_image = row['cover_image'] | |||||
| print(name, ",", artist, ",", year, ",", cover_image) | |||||
| @ -1,23 +0,0 @@ | |||||
| from django.core.management.base import BaseCommand, CommandError | |||||
| from lyrics.models import Album, Artista, Song | |||||
| import csv | |||||
| import argparse | |||||
| from datetime import datetime | |||||
| class Command(BaseCommand): | |||||
| help = "Importa la lista de letras" | |||||
| def add_arguments(self, parser): | |||||
| parser.add_argument("fichero_csv", type=str, help='Ruta al fichero csv') | |||||
| def handle(self, *args, **options): | |||||
| fichero = options["fichero_csv"] | |||||
| with open(fichero, 'r') as file: | |||||
| reader = csv.DictReader(file) | |||||
| for row in reader: | |||||
| name = row['name'], | |||||
| artist = row['artist'], | |||||
| year = row['year'], | |||||
| cover_image = row['cover_image'] | |||||
| print(name, ",", artist, ",", year, ",", cover_image) | |||||
| @ -1,26 +0,0 @@ | |||||
| from django.core.management.base import BaseCommand, CommandError | |||||
| from lyrics.models import Album, Artista, Song | |||||
| import csv | |||||
| import argparse | |||||
| from datetime import datetime | |||||
| import pandas as pd | |||||
| class Command(BaseCommand): | |||||
| help = "Importa la lista de letras" | |||||
| def add_arguments(self, parser): | |||||
| parser.add_argument("fichero_csv", type=str, help='Ruta al fichero csv') | |||||
| def handle(self, *args, **options): | |||||
| fichero = options["fichero_csv"] | |||||
| contenido = pd.read_csv(fichero) | |||||
| for fila in contenido.iterrows(): | |||||
| print(fila[1].title, ", ", fila[1].artist, ", ", fila[1].album) | |||||
| artista = Artista.objects.get(nombre=fila[1].artist) | |||||
| album = Album.objects.get(pk=fila[1].album) | |||||
| cancion = Song(title=fila[1].title, artist=artista, album=album, pista=fila[1].pista, lyrics=fila[1].lyrics) | |||||
| cancion.save() | |||||
| @ -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( | |||||
| artista_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,50 @@ | |||||
| import json | |||||
| from django.core.management.base import BaseCommand | |||||
| from lyrics.models import Cancion, Album | |||||
| 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"]) | |||||
| creado = Cancion.objects.create( | |||||
| album_id=album.id, | |||||
| title=cancion_data['title'], | |||||
| artist=cancion_data['artist'], | |||||
| 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,20 @@ | |||||
| from rest_framework import serializers | |||||
| from .models import Artista, Album, Song | |||||
| class ArtistaSerializer(serializers.ModelSerializer): | |||||
| class Meta: | |||||
| model = Artista | |||||
| fields = '__all__' # Incluir todos los campos del modelo | |||||
| class AlbumSerializer(serializers.ModelSerializer): | |||||
| class Meta: | |||||
| model = Album | |||||
| fields = '__all__' # Incluir todos los campos del modelo | |||||
| class CancionSerializer(serializers.ModelSerializer): | |||||
| class Meta: | |||||
| model = Song | |||||
| fields = '__all__' # Incluir todos los campos del modelo | |||||