updated author creation

This commit is contained in:
Alexander Karpov 2023-12-18 14:51:24 +03:00
parent 75d787f460
commit 79e865bf37
4 changed files with 55 additions and 12 deletions

View File

@ -0,0 +1,31 @@
# Generated by Django 4.2.8 on 2023-12-18 11:44
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("music", "0012_album_meta_author_albums_author_meta"),
]
operations = [
migrations.RemoveField(
model_name="author",
name="albums",
),
migrations.AddField(
model_name="album",
name="authors",
field=models.ManyToManyField(related_name="albums", to="music.album"),
),
migrations.AlterField(
model_name="album",
name="name",
field=models.CharField(max_length=200, unique=True),
),
migrations.AlterField(
model_name="author",
name="name",
field=models.CharField(max_length=200, unique=True),
),
]

View File

@ -8,10 +8,9 @@
class Author(BaseImageModel, ShortLinkModel): class Author(BaseImageModel, ShortLinkModel):
name = models.CharField(max_length=200) name = models.CharField(max_length=200, unique=True)
link = models.URLField(blank=True) link = models.URLField(blank=True)
meta = models.JSONField(blank=True, null=True) meta = models.JSONField(blank=True, null=True)
albums = models.ManyToManyField("Album", related_name="authors")
def get_absolute_url(self): def get_absolute_url(self):
return reverse("music:author", kwargs={"slug": self.slug}) return reverse("music:author", kwargs={"slug": self.slug})
@ -21,9 +20,10 @@ def __str__(self):
class Album(BaseImageModel, ShortLinkModel): class Album(BaseImageModel, ShortLinkModel):
name = models.CharField(max_length=200) name = models.CharField(max_length=200, unique=True)
link = models.URLField(blank=True) link = models.URLField(blank=True)
meta = models.JSONField(blank=True, null=True) meta = models.JSONField(blank=True, null=True)
authors = models.ManyToManyField("Album", related_name="albums")
def get_absolute_url(self): def get_absolute_url(self):
return reverse("music:album", kwargs={"slug": self.slug}) return reverse("music:album", kwargs={"slug": self.slug})

View File

@ -27,16 +27,28 @@ def load_track(
if album and type(album) is str and album.startswith("['"): if album and type(album) is str and album.startswith("['"):
album = album.replace("['", "").replace("']", "") album = album.replace("['", "").replace("']", "")
re_authors = []
if authors: if authors:
authors = [Author.objects.get_or_create(name=x)[0] for x in authors if authors] for x in authors:
else: try:
authors = [] re_authors.append(Author.objects.get(name=x))
except Author.DoesNotExist:
re_authors.append(Author.objects.create(name=x))
authors = re_authors
album_name = None
if album: if album:
if type(album) is str: if type(album) is str:
album = Album.objects.get_or_create(name=album)[0] album_name = album
elif type(album) is list: elif type(album) is list:
album = Album.objects.get_or_create(name=album[0])[0] album_name = album[0]
else: else:
album_name = None
if album_name:
try:
album = Album.objects.get(name=album_name)
except Album.DoesNotExist:
album = Album.objects.create(name=album_name)
if not album_name:
album = None album = None
if sng := Song.objects.filter( if sng := Song.objects.filter(

View File

@ -130,9 +130,9 @@ def update_album_info(album: AlbumModel) -> None:
if search_album.artists: if search_album.artists:
for x in search_album.artists: for x in search_album.artists:
try: try:
authors.append(Author.objects.get_or_create(name=x.name)[0]) authors.append(Author.objects.get(name=x.name))
except Author.MultipleObjectsReturned: except Author.DoesNotExist:
authors.append(Author.objects.filter(name=x.name).first()) authors.append(Author.objects.create(name=x.name))
album.authors.set(authors) album.authors.set(authors)
album.meta = data album.meta = data
image_path = str(settings.MEDIA_ROOT + f"/_{str(randint(10000, 99999))}.png") image_path = str(settings.MEDIA_ROOT + f"/_{str(randint(10000, 99999))}.png")