mirror of
				https://github.com/Alexander-D-Karpov/akarpov
				synced 2025-11-04 10:17:24 +03:00 
			
		
		
		
	update author save
This commit is contained in:
		
							parent
							
								
									a309d5653d
								
							
						
					
					
						commit
						aa49e4afc3
					
				| 
						 | 
					@ -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
 | 
					 | 
				
			||||||
    while retry:
 | 
					 | 
				
			||||||
        retry = False
 | 
					 | 
				
			||||||
        try:
 | 
					 | 
				
			||||||
    with transaction.atomic():
 | 
					    with transaction.atomic():
 | 
				
			||||||
                author, created = Author.objects.get_or_create(
 | 
					        author = Author.objects.filter(name__iexact=author_name).order_by("id").first()
 | 
				
			||||||
                    name__iexact=author_name, defaults={"name": author_name}
 | 
					        if author is None:
 | 
				
			||||||
                )
 | 
					            author = Author.objects.create(name=author_name)
 | 
				
			||||||
        return author
 | 
					        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],
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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,6 +322,7 @@ def update_author_info(author: Author) -> None:
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    author.meta = author_data
 | 
					    author.meta = author_data
 | 
				
			||||||
 | 
					    with transaction.atomic():
 | 
				
			||||||
        author.save()
 | 
					        author.save()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Handle Author Image - Prefer Spotify, fallback to Yandex
 | 
					    # Handle Author Image - Prefer Spotify, fallback to Yandex
 | 
				
			||||||
| 
						 | 
					@ -353,6 +355,7 @@ 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)
 | 
				
			||||||
 | 
					    with transaction.atomic():
 | 
				
			||||||
        author.save()
 | 
					        author.save()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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,6 +79,7 @@ def create_model_link(sender, instance, created, **kwargs):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        link.save()
 | 
					        link.save()
 | 
				
			||||||
        instance.short_link = link
 | 
					        instance.short_link = link
 | 
				
			||||||
 | 
					        with transaction.atomic():
 | 
				
			||||||
            instance.save()
 | 
					            instance.save()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user