diff --git a/apps/vod/tasks.py b/apps/vod/tasks.py index 93cef3e1..3c429e15 100644 --- a/apps/vod/tasks.py +++ b/apps/vod/tasks.py @@ -135,11 +135,11 @@ def refresh_movies(client, account, categories_by_provider, relations, scan_star # Process movies in chunks using the simple approach chunk_size = 1000 total_movies = len(all_movies_data) + total_chunks = (total_movies + chunk_size - 1) // chunk_size if total_movies > 0 else 0 for i in range(0, total_movies, chunk_size): chunk = all_movies_data[i:i + chunk_size] chunk_num = (i // chunk_size) + 1 - total_chunks = (total_movies + chunk_size - 1) // chunk_size logger.info(f"Processing movie chunk {chunk_num}/{total_chunks} ({len(chunk)} movies)") process_movie_batch(account, chunk, categories_by_provider, relations, scan_start_time) @@ -158,11 +158,11 @@ def refresh_series(client, account, categories_by_provider, relations, scan_star # Process series in chunks using the simple approach chunk_size = 1000 total_series = len(all_series_data) + total_chunks = (total_series + chunk_size - 1) // chunk_size if total_series > 0 else 0 for i in range(0, total_series, chunk_size): chunk = all_series_data[i:i + chunk_size] chunk_num = (i // chunk_size) + 1 - total_chunks = (total_series + chunk_size - 1) // chunk_size logger.info(f"Processing series chunk {chunk_num}/{total_chunks} ({len(chunk)} series)") process_series_batch(account, chunk, categories_by_provider, relations, scan_start_time) @@ -427,17 +427,18 @@ def process_movie_batch(account, batch, categories, relations, scan_start_time=N updated = True # Handle logo assignment for existing movies + logo_updated = False if logo_url and len(logo_url) <= 500 and logo_url in existing_logos: new_logo = existing_logos[logo_url] if movie.logo != new_logo: - movie.logo = new_logo - updated = True + movie._logo_to_update = new_logo + logo_updated = True elif (not logo_url or len(logo_url) > 500) and movie.logo: # Clear logo if no logo URL provided or URL is too long - movie.logo = None - updated = True + movie._logo_to_update = None + logo_updated = True - if updated: + if updated or logo_updated: movies_to_update.append(movie) else: # Create new movie @@ -509,11 +510,18 @@ def process_movie_batch(account, batch, categories, relations, scan_start_time=N # Update existing movies if movies_to_update: + # First, update all fields except logo to avoid unsaved related object issues Movie.objects.bulk_update(movies_to_update, [ 'description', 'rating', 'genre', 'year', 'tmdb_id', 'imdb_id', - 'duration_secs', 'custom_properties', 'logo' + 'duration_secs', 'custom_properties' ]) + # Handle logo updates separately to avoid bulk_update issues + for movie in movies_to_update: + if hasattr(movie, '_logo_to_update'): + movie.logo = movie._logo_to_update + movie.save(update_fields=['logo']) + # Update relations to reference the correct movie objects for relation in relations_to_create: if id(relation.movie) in created_movies: @@ -741,17 +749,18 @@ def process_series_batch(account, batch, categories, relations, scan_start_time= updated = True # Handle logo assignment for existing series + logo_updated = False if logo_url and len(logo_url) <= 500 and logo_url in existing_logos: new_logo = existing_logos[logo_url] if series.logo != new_logo: - series.logo = new_logo - updated = True + series._logo_to_update = new_logo + logo_updated = True elif (not logo_url or len(logo_url) > 500) and series.logo: # Clear logo if no logo URL provided or URL is too long - series.logo = None - updated = True + series._logo_to_update = None + logo_updated = True - if updated: + if updated or logo_updated: series_to_update.append(series) else: # Create new series @@ -823,11 +832,18 @@ def process_series_batch(account, batch, categories, relations, scan_start_time= # Update existing series if series_to_update: + # First, update all fields except logo to avoid unsaved related object issues Series.objects.bulk_update(series_to_update, [ 'description', 'rating', 'genre', 'year', 'tmdb_id', 'imdb_id', - 'custom_properties', 'logo' + 'custom_properties' ]) + # Handle logo updates separately to avoid bulk_update issues + for series in series_to_update: + if hasattr(series, '_logo_to_update'): + series.logo = series._logo_to_update + series.save(update_fields=['logo']) + # Update relations to reference the correct series objects for relation in relations_to_create: if id(relation.series) in created_series: