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 django.core.files import File
from django.db import transaction
from django.db.models import Min
from django.utils.text import slugify
from mutagen import File as MutagenFile
from mutagen.id3 import APIC, ID3, TCON, TORY, TextFrame
@ -19,29 +18,11 @@
def get_or_create_author(author_name):
retry = True
while retry:
retry = False
try:
with transaction.atomic():
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
with transaction.atomic():
author = Author.objects.filter(name__iexact=author_name).order_by("id").first()
if author is None:
author = Author.objects.create(name=author_name)
return author
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("['"):
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 type(album) is str:
album_name = album
@ -125,6 +99,13 @@ def load_track(
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(
name=name if name else p_name,
authors__id__in=[x.id for x in authors],

View File

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

View File

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