update author save

This commit is contained in:
Alexander Karpov 2024-02-01 03:51:44 +03:00
parent a309d5653d
commit aa49e4afc3
3 changed files with 20 additions and 35 deletions

View File

@ -5,7 +5,6 @@
from deep_translator import GoogleTranslator from deep_translator import GoogleTranslator
from django.core.files import File from django.core.files import File
from django.db import transaction from django.db import transaction
from django.db.models import Min
from django.utils.text import slugify from django.utils.text import slugify
from mutagen import File as MutagenFile from mutagen import File as MutagenFile
from mutagen.id3 import APIC, ID3, TCON, TORY, TextFrame from mutagen.id3 import APIC, ID3, TCON, TORY, TextFrame
@ -19,29 +18,11 @@
def get_or_create_author(author_name): def get_or_create_author(author_name):
retry = True with transaction.atomic():
while retry: author = Author.objects.filter(name__iexact=author_name).order_by("id").first()
retry = False if author is None:
try: author = Author.objects.create(name=author_name)
with transaction.atomic(): return author
author, created = Author.objects.get_or_create(
name__iexact=author_name, defaults={"name": author_name}
)
return author
except Author.MultipleObjectsReturned:
with transaction.atomic():
# If multiple authors are found, get the first one and delete the rest
min_id = Author.objects.filter(name__iexact=author_name).aggregate(
Min("id")
)["id__min"]
author = Author.objects.get(id=min_id)
Author.objects.filter(name__iexact=author_name).exclude(
id=min_id
).delete()
return author
except Exception as e:
if "could not serialize access due to concurrent update" in str(e):
retry = True
def process_track_name(track_name: str) -> str: def process_track_name(track_name: str) -> str:
@ -106,13 +87,6 @@ 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("']", "")
processed_authors = []
if authors:
for author_name in authors:
author = get_or_create_author(author_name)
processed_authors.append(author)
authors = processed_authors
if album: if album:
if type(album) is str: if type(album) is str:
album_name = album album_name = album
@ -125,6 +99,13 @@ def load_track(
name__iexact=album_name, defaults={"name": album_name} name__iexact=album_name, defaults={"name": album_name}
) )
processed_authors = []
if authors:
for author_name in authors:
author = get_or_create_author(author_name)
processed_authors.append(author)
authors = processed_authors
if sng := Song.objects.filter( if sng := Song.objects.filter(
name=name if name else p_name, name=name if name else p_name,
authors__id__in=[x.id for x in authors], authors__id__in=[x.id for x in authors],

View File

@ -6,6 +6,7 @@
from deep_translator import GoogleTranslator from deep_translator import GoogleTranslator
from django.conf import settings from django.conf import settings
from django.core.files import File from django.core.files import File
from django.db import transaction
from django.utils.text import slugify from django.utils.text import slugify
from spotipy import SpotifyClientCredentials from spotipy import SpotifyClientCredentials
from yandex_music import Client, Cover from yandex_music import Client, Cover
@ -321,7 +322,8 @@ def update_author_info(author: Author) -> None:
) )
author.meta = author_data author.meta = author_data
author.save() with transaction.atomic():
author.save()
# Handle Author Image - Prefer Spotify, fallback to Yandex # Handle Author Image - Prefer Spotify, fallback to Yandex
image_path = None image_path = None
@ -353,7 +355,8 @@ def update_author_info(author: Author) -> None:
author.save() author.save()
author.slug = generate_readable_slug(author.name, Author) author.slug = generate_readable_slug(author.name, Author)
author.save() with transaction.atomic():
author.save()
def search_all_platforms(track_name: str) -> dict: def search_all_platforms(track_name: str) -> dict:

View File

@ -1,7 +1,7 @@
from abc import abstractmethod from abc import abstractmethod
from django.conf import settings from django.conf import settings
from django.db import models from django.db import models, transaction
from django.urls import reverse from django.urls import reverse
from model_utils.models import TimeStampedModel from model_utils.models import TimeStampedModel
@ -79,7 +79,8 @@ def create_model_link(sender, instance, created, **kwargs):
link.save() link.save()
instance.short_link = link instance.short_link = link
instance.save() with transaction.atomic():
instance.save()
def update_model_link(sender, instance, **kwargs): def update_model_link(sender, instance, **kwargs):