From aa49e4afc3466d23e0c11c123d31adec14623ecc Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Thu, 1 Feb 2024 03:51:44 +0300 Subject: [PATCH] update author save --- akarpov/music/services/db.py | 43 +++++++++---------------------- akarpov/music/services/info.py | 7 +++-- akarpov/tools/shortener/models.py | 5 ++-- 3 files changed, 20 insertions(+), 35 deletions(-) diff --git a/akarpov/music/services/db.py b/akarpov/music/services/db.py index 8ba47bd..46ba290 100644 --- a/akarpov/music/services/db.py +++ b/akarpov/music/services/db.py @@ -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], diff --git a/akarpov/music/services/info.py b/akarpov/music/services/info.py index f35e839..c1b99bf 100644 --- a/akarpov/music/services/info.py +++ b/akarpov/music/services/info.py @@ -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: diff --git a/akarpov/tools/shortener/models.py b/akarpov/tools/shortener/models.py index dccdc12..56fadaf 100644 --- a/akarpov/tools/shortener/models.py +++ b/akarpov/tools/shortener/models.py @@ -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):