diff --git a/akarpov/music/migrations/0013_remove_author_albums_album_authors_alter_album_name_and_more.py b/akarpov/music/migrations/0013_remove_author_albums_album_authors_alter_album_name_and_more.py new file mode 100644 index 0000000..56ae3ac --- /dev/null +++ b/akarpov/music/migrations/0013_remove_author_albums_album_authors_alter_album_name_and_more.py @@ -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), + ), + ] diff --git a/akarpov/music/models.py b/akarpov/music/models.py index 37b646b..a0ce816 100644 --- a/akarpov/music/models.py +++ b/akarpov/music/models.py @@ -8,10 +8,9 @@ class Author(BaseImageModel, ShortLinkModel): - name = models.CharField(max_length=200) + name = models.CharField(max_length=200, unique=True) link = models.URLField(blank=True) meta = models.JSONField(blank=True, null=True) - albums = models.ManyToManyField("Album", related_name="authors") def get_absolute_url(self): return reverse("music:author", kwargs={"slug": self.slug}) @@ -21,9 +20,10 @@ def __str__(self): class Album(BaseImageModel, ShortLinkModel): - name = models.CharField(max_length=200) + name = models.CharField(max_length=200, unique=True) link = models.URLField(blank=True) meta = models.JSONField(blank=True, null=True) + authors = models.ManyToManyField("Album", related_name="albums") def get_absolute_url(self): return reverse("music:album", kwargs={"slug": self.slug}) diff --git a/akarpov/music/services/db.py b/akarpov/music/services/db.py index e361548..44dd6c2 100644 --- a/akarpov/music/services/db.py +++ b/akarpov/music/services/db.py @@ -27,16 +27,28 @@ def load_track( if album and type(album) is str and album.startswith("['"): album = album.replace("['", "").replace("']", "") + re_authors = [] if authors: - authors = [Author.objects.get_or_create(name=x)[0] for x in authors if authors] - else: - authors = [] + for x in authors: + try: + 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 type(album) is str: - album = Album.objects.get_or_create(name=album)[0] + album_name = album elif type(album) is list: - album = Album.objects.get_or_create(name=album[0])[0] - else: + album_name = album[0] + 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 if sng := Song.objects.filter( diff --git a/akarpov/music/services/yandex.py b/akarpov/music/services/yandex.py index 32b6c86..b53e31b 100644 --- a/akarpov/music/services/yandex.py +++ b/akarpov/music/services/yandex.py @@ -130,9 +130,9 @@ def update_album_info(album: AlbumModel) -> None: if search_album.artists: for x in search_album.artists: try: - authors.append(Author.objects.get_or_create(name=x.name)[0]) - except Author.MultipleObjectsReturned: - authors.append(Author.objects.filter(name=x.name).first()) + authors.append(Author.objects.get(name=x.name)) + except Author.DoesNotExist: + authors.append(Author.objects.create(name=x.name)) album.authors.set(authors) album.meta = data image_path = str(settings.MEDIA_ROOT + f"/_{str(randint(10000, 99999))}.png")