From 3ecd7137ff2bda225edd9ba2e8dd4ae6dc45bbb1 Mon Sep 17 00:00:00 2001 From: SergeantPanda Date: Fri, 22 Aug 2025 17:12:50 -0500 Subject: [PATCH] Added misssing bulk fetch function for vod categories. --- apps/m3u/tasks.py | 4 ++-- apps/vod/models.py | 26 ++++++++++++++++++++++++++ apps/vod/tasks.py | 30 ++++++++++-------------------- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/apps/m3u/tasks.py b/apps/m3u/tasks.py index 441c45ff..2617e7ea 100644 --- a/apps/m3u/tasks.py +++ b/apps/m3u/tasks.py @@ -663,8 +663,8 @@ def process_m3u_batch(account_id, batch, groups, hash_keys): def cleanup_streams(account_id, scan_start_time=timezone.now): account = M3UAccount.objects.get(id=account_id, is_active=True) existing_groups = ChannelGroup.objects.filter( - m3u_account__m3u_account=account, - m3u_account__enabled=True, + m3u_accounts__m3u_account=account, + m3u_accounts__enabled=True, ).values_list("id", flat=True) logger.info( f"Found {len(existing_groups)} active groups for M3U account {account_id}" diff --git a/apps/vod/models.py b/apps/vod/models.py index f2007b09..eccef94f 100644 --- a/apps/vod/models.py +++ b/apps/vod/models.py @@ -1,4 +1,5 @@ from django.db import models +from django.db.models import Q from django.utils import timezone from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType @@ -31,6 +32,31 @@ class VODCategory(models.Model): ordering = ['name'] unique_together = [('name', 'category_type')] + @classmethod + def bulk_create_and_fetch(cls, objects, ignore_conflicts=False): + # Perform the bulk create operation + cls.objects.bulk_create(objects, ignore_conflicts=ignore_conflicts) + + # Use the unique fields to fetch the created objects + # Since we have unique_together on ('name', 'category_type'), we need both fields + filter_conditions = [] + for obj in objects: + filter_conditions.append( + Q(name=obj.name, category_type=obj.category_type) + ) + + if filter_conditions: + # Combine all conditions with OR + combined_condition = filter_conditions[0] + for condition in filter_conditions[1:]: + combined_condition |= condition + + created_objects = cls.objects.filter(combined_condition) + else: + created_objects = cls.objects.none() + + return created_objects + def __str__(self): return f"{self.name} ({self.get_category_type_display()})" diff --git a/apps/vod/tasks.py b/apps/vod/tasks.py index 8ee57185..a0fe42c1 100644 --- a/apps/vod/tasks.py +++ b/apps/vod/tasks.py @@ -128,7 +128,7 @@ def refresh_movies(client, account, categories): 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, category_map) + process_movie_batch(account, chunk, categories) logger.info(f"Completed processing all {total_movies} movies in {total_chunks} chunks") @@ -164,7 +164,7 @@ def refresh_series(client, account, categories): 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, category_map) + process_series_batch(account, chunk, categories) logger.info(f"Completed processing all {total_series} series in {total_chunks} chunks") @@ -188,14 +188,9 @@ def batch_create_categories_from_names(category_names, category_type): new_categories.append(VODCategory(name=name, category_type=category_type)) if new_categories: - VODCategory.objects.bulk_create(new_categories, ignore_conflicts=True) - # Fetch the newly created categories - newly_created = { - cat.name: cat for cat in VODCategory.objects.filter( - name__in=[cat.name for cat in new_categories], - category_type=category_type - ) - } + created_categories = VODCategory.bulk_create_and_fetch(new_categories, ignore_conflicts=True) + # Convert to dictionary for easy lookup + newly_created = {cat.name: cat for cat in created_categories} existing_categories.update(newly_created) return existing_categories @@ -228,14 +223,9 @@ def batch_create_categories(categories_data, category_type, account): )) if new_categories: - VODCategory.objects.bulk_create_and_fetch(new_categories, ignore_conflicts=True) - # Fetch the newly created categories - newly_created = { - cat.name: cat for cat in VODCategory.objects.filter( - name__in=[cat.name for cat in new_categories], - category_type=category_type - ) - } + created_categories = VODCategory.bulk_create_and_fetch(new_categories, ignore_conflicts=True) + # Convert to dictionary for easy lookup + newly_created = {cat.name: cat for cat in created_categories} relations = relations + [M3UVODCategoryRelation( category=cat, @@ -251,7 +241,7 @@ def batch_create_categories(categories_data, category_type, account): @shared_task -def process_movie_batch(account, batch, category_map): +def process_movie_batch(account, batch, categories): """Process a batch of movies using simple bulk operations like M3U processing""" logger.info(f"Processing movie batch of {len(batch)} movies for account {account.name}") @@ -537,7 +527,7 @@ def process_movie_batch(account, batch, category_map): @shared_task -def process_series_batch(account, batch, category_map): +def process_series_batch(account, batch, categories): """Process a batch of series using simple bulk operations like M3U processing""" logger.info(f"Processing series batch of {len(batch)} series for account {account.name}")