Process groups and update their relationships with the M3U account. Args: account: M3UAccount instance groups: Dict of group names to custom properties scan_start_time: Timestamp when the scan started (for consistent last_seen marking)
(account, groups, scan_start_time=None)
| 703 | |
| 704 | @shared_task |
| 705 | def process_groups(account, groups, scan_start_time=None): |
| 706 | """Process groups and update their relationships with the M3U account. |
| 707 | |
| 708 | Args: |
| 709 | account: M3UAccount instance |
| 710 | groups: Dict of group names to custom properties |
| 711 | scan_start_time: Timestamp when the scan started (for consistent last_seen marking) |
| 712 | """ |
| 713 | # Use scan_start_time if provided, otherwise current time |
| 714 | # This ensures consistency with stream processing and cleanup logic |
| 715 | if scan_start_time is None: |
| 716 | scan_start_time = timezone.now() |
| 717 | |
| 718 | existing_groups = { |
| 719 | group.name: group |
| 720 | for group in ChannelGroup.objects.filter(name__in=groups.keys()) |
| 721 | } |
| 722 | logger.info(f"Currently {len(existing_groups)} existing groups") |
| 723 | |
| 724 | # Check if we should auto-enable new groups based on account settings |
| 725 | account_custom_props = ensure_custom_properties_dict(account.custom_properties) |
| 726 | auto_enable_new_groups_live = account_custom_props.get("auto_enable_new_groups_live", True) |
| 727 | |
| 728 | # Separate existing groups from groups that need to be created |
| 729 | existing_group_objs = [] |
| 730 | groups_to_create = [] |
| 731 | |
| 732 | for group_name, custom_props in groups.items(): |
| 733 | if group_name in existing_groups: |
| 734 | existing_group_objs.append(existing_groups[group_name]) |
| 735 | else: |
| 736 | groups_to_create.append(ChannelGroup(name=group_name)) |
| 737 | |
| 738 | # Create new groups and fetch them back with IDs |
| 739 | newly_created_group_objs = [] |
| 740 | if groups_to_create: |
| 741 | logger.info(f"Creating {len(groups_to_create)} new groups for account {account.id}") |
| 742 | newly_created_group_objs = list(ChannelGroup.bulk_create_and_fetch(groups_to_create)) |
| 743 | logger.debug(f"Successfully created {len(newly_created_group_objs)} new groups") |
| 744 | |
| 745 | # Combine all groups |
| 746 | all_group_objs = existing_group_objs + newly_created_group_objs |
| 747 | |
| 748 | # Get existing relationships for this account |
| 749 | existing_relationships = { |
| 750 | rel.channel_group.name: rel |
| 751 | for rel in ChannelGroupM3UAccount.objects.filter( |
| 752 | m3u_account=account, |
| 753 | channel_group__name__in=groups.keys() |
| 754 | ).select_related('channel_group') |
| 755 | } |
| 756 | |
| 757 | relations_to_create = [] |
| 758 | relations_to_update = [] |
| 759 | |
| 760 | for group in all_group_objs: |
| 761 | custom_props = groups.get(group.name, {}) |
| 762 |
no test coverage detected