mirror of
https://github.com/Dispatcharr/Dispatcharr.git
synced 2026-01-23 10:45:27 +00:00
Merge pull request #543 from Dispatcharr/Auto-disable-new-categories
This commit is contained in:
commit
85fdfedabe
6 changed files with 183 additions and 58 deletions
|
|
@ -136,6 +136,9 @@ class M3UAccountSerializer(serializers.ModelSerializer):
|
|||
validators=[validate_flexible_url],
|
||||
)
|
||||
enable_vod = serializers.BooleanField(required=False, write_only=True)
|
||||
auto_enable_new_groups_live = serializers.BooleanField(required=False, write_only=True)
|
||||
auto_enable_new_groups_vod = serializers.BooleanField(required=False, write_only=True)
|
||||
auto_enable_new_groups_series = serializers.BooleanField(required=False, write_only=True)
|
||||
|
||||
class Meta:
|
||||
model = M3UAccount
|
||||
|
|
@ -164,6 +167,9 @@ class M3UAccountSerializer(serializers.ModelSerializer):
|
|||
"status",
|
||||
"last_message",
|
||||
"enable_vod",
|
||||
"auto_enable_new_groups_live",
|
||||
"auto_enable_new_groups_vod",
|
||||
"auto_enable_new_groups_series",
|
||||
]
|
||||
extra_kwargs = {
|
||||
"password": {
|
||||
|
|
@ -175,23 +181,36 @@ class M3UAccountSerializer(serializers.ModelSerializer):
|
|||
def to_representation(self, instance):
|
||||
data = super().to_representation(instance)
|
||||
|
||||
# Parse custom_properties to get VOD preference
|
||||
# Parse custom_properties to get VOD preference and auto_enable_new_groups settings
|
||||
custom_props = instance.custom_properties or {}
|
||||
|
||||
data["enable_vod"] = custom_props.get("enable_vod", False)
|
||||
data["auto_enable_new_groups_live"] = custom_props.get("auto_enable_new_groups_live", True)
|
||||
data["auto_enable_new_groups_vod"] = custom_props.get("auto_enable_new_groups_vod", True)
|
||||
data["auto_enable_new_groups_series"] = custom_props.get("auto_enable_new_groups_series", True)
|
||||
return data
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
# Handle enable_vod preference
|
||||
# Handle enable_vod preference and auto_enable_new_groups settings
|
||||
enable_vod = validated_data.pop("enable_vod", None)
|
||||
auto_enable_new_groups_live = validated_data.pop("auto_enable_new_groups_live", None)
|
||||
auto_enable_new_groups_vod = validated_data.pop("auto_enable_new_groups_vod", None)
|
||||
auto_enable_new_groups_series = validated_data.pop("auto_enable_new_groups_series", None)
|
||||
|
||||
# Get existing custom_properties
|
||||
custom_props = instance.custom_properties or {}
|
||||
|
||||
# Update preferences
|
||||
if enable_vod is not None:
|
||||
# Get existing custom_properties
|
||||
custom_props = instance.custom_properties or {}
|
||||
|
||||
# Update VOD preference
|
||||
custom_props["enable_vod"] = enable_vod
|
||||
validated_data["custom_properties"] = custom_props
|
||||
if auto_enable_new_groups_live is not None:
|
||||
custom_props["auto_enable_new_groups_live"] = auto_enable_new_groups_live
|
||||
if auto_enable_new_groups_vod is not None:
|
||||
custom_props["auto_enable_new_groups_vod"] = auto_enable_new_groups_vod
|
||||
if auto_enable_new_groups_series is not None:
|
||||
custom_props["auto_enable_new_groups_series"] = auto_enable_new_groups_series
|
||||
|
||||
validated_data["custom_properties"] = custom_props
|
||||
|
||||
# Pop out channel group memberships so we can handle them manually
|
||||
channel_group_data = validated_data.pop("channel_group", [])
|
||||
|
|
@ -225,14 +244,20 @@ class M3UAccountSerializer(serializers.ModelSerializer):
|
|||
return instance
|
||||
|
||||
def create(self, validated_data):
|
||||
# Handle enable_vod preference during creation
|
||||
# Handle enable_vod preference and auto_enable_new_groups settings during creation
|
||||
enable_vod = validated_data.pop("enable_vod", False)
|
||||
auto_enable_new_groups_live = validated_data.pop("auto_enable_new_groups_live", True)
|
||||
auto_enable_new_groups_vod = validated_data.pop("auto_enable_new_groups_vod", True)
|
||||
auto_enable_new_groups_series = validated_data.pop("auto_enable_new_groups_series", True)
|
||||
|
||||
# Parse existing custom_properties or create new
|
||||
custom_props = validated_data.get("custom_properties", {})
|
||||
|
||||
# Set VOD preference
|
||||
# Set preferences (default to True for auto_enable_new_groups)
|
||||
custom_props["enable_vod"] = enable_vod
|
||||
custom_props["auto_enable_new_groups_live"] = auto_enable_new_groups_live
|
||||
custom_props["auto_enable_new_groups_vod"] = auto_enable_new_groups_vod
|
||||
custom_props["auto_enable_new_groups_series"] = auto_enable_new_groups_series
|
||||
validated_data["custom_properties"] = custom_props
|
||||
|
||||
return super().create(validated_data)
|
||||
|
|
|
|||
|
|
@ -488,25 +488,29 @@ def process_groups(account, groups):
|
|||
}
|
||||
logger.info(f"Currently {len(existing_groups)} existing groups")
|
||||
|
||||
group_objs = []
|
||||
# Check if we should auto-enable new groups based on account settings
|
||||
account_custom_props = account.custom_properties or {}
|
||||
auto_enable_new_groups_live = account_custom_props.get("auto_enable_new_groups_live", True)
|
||||
|
||||
# Separate existing groups from groups that need to be created
|
||||
existing_group_objs = []
|
||||
groups_to_create = []
|
||||
|
||||
for group_name, custom_props in groups.items():
|
||||
logger.debug(f"Handling group for M3U account {account.id}: {group_name}")
|
||||
|
||||
if group_name not in existing_groups:
|
||||
groups_to_create.append(
|
||||
ChannelGroup(
|
||||
name=group_name,
|
||||
)
|
||||
)
|
||||
if group_name in existing_groups:
|
||||
existing_group_objs.append(existing_groups[group_name])
|
||||
else:
|
||||
group_objs.append(existing_groups[group_name])
|
||||
groups_to_create.append(ChannelGroup(name=group_name))
|
||||
|
||||
# Create new groups and fetch them back with IDs
|
||||
newly_created_group_objs = []
|
||||
if groups_to_create:
|
||||
logger.debug(f"Creating {len(groups_to_create)} groups")
|
||||
created = ChannelGroup.bulk_create_and_fetch(groups_to_create)
|
||||
logger.debug(f"Created {len(created)} groups")
|
||||
group_objs.extend(created)
|
||||
logger.info(f"Creating {len(groups_to_create)} new groups for account {account.id}")
|
||||
newly_created_group_objs = list(ChannelGroup.bulk_create_and_fetch(groups_to_create))
|
||||
logger.debug(f"Successfully created {len(newly_created_group_objs)} new groups")
|
||||
|
||||
# Combine all groups
|
||||
all_group_objs = existing_group_objs + newly_created_group_objs
|
||||
|
||||
# Get existing relationships for this account
|
||||
existing_relationships = {
|
||||
|
|
@ -536,7 +540,7 @@ def process_groups(account, groups):
|
|||
relations_to_delete.append(rel)
|
||||
logger.debug(f"Marking relationship for deletion: group '{group_name}' no longer exists in source for account {account.id}")
|
||||
|
||||
for group in group_objs:
|
||||
for group in all_group_objs:
|
||||
custom_props = groups.get(group.name, {})
|
||||
|
||||
if group.name in existing_relationships:
|
||||
|
|
@ -566,35 +570,17 @@ def process_groups(account, groups):
|
|||
else:
|
||||
logger.debug(f"xc_id unchanged for group '{group.name}' - account {account.id}")
|
||||
else:
|
||||
# Create new relationship - but check if there's an existing relationship that might have user settings
|
||||
# This can happen if the group was temporarily removed and is now back
|
||||
try:
|
||||
potential_existing = ChannelGroupM3UAccount.objects.filter(
|
||||
m3u_account=account,
|
||||
channel_group=group
|
||||
).first()
|
||||
# Create new relationship - this group is new to this M3U account
|
||||
# Use the auto_enable setting to determine if it should start enabled
|
||||
if not auto_enable_new_groups_live:
|
||||
logger.info(f"Group '{group.name}' is new to account {account.id} - creating relationship but DISABLED (auto_enable_new_groups_live=False)")
|
||||
|
||||
if potential_existing:
|
||||
# Merge with existing custom properties to preserve user settings
|
||||
existing_custom_props = potential_existing.custom_properties or {}
|
||||
|
||||
# Merge new properties with existing ones
|
||||
merged_custom_props = existing_custom_props.copy()
|
||||
merged_custom_props.update(custom_props)
|
||||
custom_props = merged_custom_props
|
||||
logger.debug(f"Merged custom properties for existing relationship: group '{group.name}' - account {account.id}")
|
||||
except Exception as e:
|
||||
logger.debug(f"Could not check for existing relationship: {str(e)}")
|
||||
# Fall back to using just the new custom properties
|
||||
pass
|
||||
|
||||
# Create new relationship
|
||||
relations_to_create.append(
|
||||
ChannelGroupM3UAccount(
|
||||
channel_group=group,
|
||||
m3u_account=account,
|
||||
custom_properties=custom_props,
|
||||
enabled=True, # Default to enabled
|
||||
enabled=auto_enable_new_groups_live,
|
||||
)
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -187,16 +187,28 @@ def batch_create_categories(categories_data, category_type, account):
|
|||
|
||||
logger.debug(f"Found {len(existing_categories)} existing categories")
|
||||
|
||||
# Check if we should auto-enable new categories based on account settings
|
||||
account_custom_props = account.custom_properties or {}
|
||||
if category_type == 'movie':
|
||||
auto_enable_new = account_custom_props.get("auto_enable_new_groups_vod", True)
|
||||
else: # series
|
||||
auto_enable_new = account_custom_props.get("auto_enable_new_groups_series", True)
|
||||
|
||||
# Create missing categories in batch
|
||||
new_categories = []
|
||||
|
||||
for name in category_names:
|
||||
if name not in existing_categories:
|
||||
# Always create new categories
|
||||
new_categories.append(VODCategory(name=name, category_type=category_type))
|
||||
else:
|
||||
# Existing category - create relationship with enabled based on auto_enable setting
|
||||
# (category exists globally but is new to this account)
|
||||
relations_to_create.append(M3UVODCategoryRelation(
|
||||
category=existing_categories[name],
|
||||
m3u_account=account,
|
||||
custom_properties={},
|
||||
enabled=auto_enable_new,
|
||||
))
|
||||
|
||||
logger.debug(f"{len(new_categories)} new categories found")
|
||||
|
|
@ -204,24 +216,68 @@ def batch_create_categories(categories_data, category_type, account):
|
|||
|
||||
if new_categories:
|
||||
logger.debug("Creating new categories...")
|
||||
created_categories = VODCategory.bulk_create_and_fetch(new_categories, ignore_conflicts=True)
|
||||
created_categories = list(VODCategory.bulk_create_and_fetch(new_categories, ignore_conflicts=True))
|
||||
|
||||
# Create relations for newly created categories with enabled based on auto_enable setting
|
||||
for cat in created_categories:
|
||||
if not auto_enable_new:
|
||||
logger.info(f"New {category_type} category '{cat.name}' created but DISABLED - auto_enable_new_groups is disabled for account {account.id}")
|
||||
|
||||
relations_to_create.append(
|
||||
M3UVODCategoryRelation(
|
||||
category=cat,
|
||||
m3u_account=account,
|
||||
custom_properties={},
|
||||
enabled=auto_enable_new,
|
||||
)
|
||||
)
|
||||
|
||||
# Convert to dictionary for easy lookup
|
||||
newly_created = {cat.name: cat for cat in created_categories}
|
||||
|
||||
relations_to_create += [
|
||||
M3UVODCategoryRelation(
|
||||
category=cat,
|
||||
m3u_account=account,
|
||||
custom_properties={},
|
||||
) for cat in newly_created.values()
|
||||
]
|
||||
|
||||
existing_categories.update(newly_created)
|
||||
|
||||
# Create missing relations
|
||||
logger.debug("Updating category account relations...")
|
||||
M3UVODCategoryRelation.objects.bulk_create(relations_to_create, ignore_conflicts=True)
|
||||
|
||||
# Delete orphaned category relationships (categories no longer in the M3U source)
|
||||
current_category_ids = set(existing_categories[name].id for name in category_names)
|
||||
existing_relations = M3UVODCategoryRelation.objects.filter(
|
||||
m3u_account=account,
|
||||
category__category_type=category_type
|
||||
).select_related('category')
|
||||
|
||||
relations_to_delete = [
|
||||
rel for rel in existing_relations
|
||||
if rel.category_id not in current_category_ids
|
||||
]
|
||||
|
||||
if relations_to_delete:
|
||||
M3UVODCategoryRelation.objects.filter(
|
||||
id__in=[rel.id for rel in relations_to_delete]
|
||||
).delete()
|
||||
logger.info(f"Deleted {len(relations_to_delete)} orphaned {category_type} category relationships for account {account.id}: {[rel.category.name for rel in relations_to_delete]}")
|
||||
|
||||
# Check if any of the deleted relationships left categories with no remaining associations
|
||||
orphaned_category_ids = []
|
||||
for rel in relations_to_delete:
|
||||
category = rel.category
|
||||
|
||||
# Check if this category has any remaining M3U account relationships
|
||||
remaining_relationships = M3UVODCategoryRelation.objects.filter(
|
||||
category=category
|
||||
).exists()
|
||||
|
||||
# If no relationships remain, it's safe to delete the category
|
||||
if not remaining_relationships:
|
||||
orphaned_category_ids.append(category.id)
|
||||
logger.debug(f"Category '{category.name}' has no remaining associations and will be deleted")
|
||||
|
||||
# Delete orphaned categories
|
||||
if orphaned_category_ids:
|
||||
VODCategory.objects.filter(id__in=orphaned_category_ids).delete()
|
||||
logger.info(f"Deleted {len(orphaned_category_ids)} orphaned {category_type} categories with no remaining associations")
|
||||
|
||||
# 🔑 Fetch all relations for this account, for all categories
|
||||
# relations = { rel.id: rel for rel in M3UVODCategoryRelation.objects
|
||||
# .filter(category__in=existing_categories.values(), m3u_account=account)
|
||||
|
|
|
|||
|
|
@ -33,7 +33,13 @@ const OptionWithTooltip = forwardRef(
|
|||
)
|
||||
);
|
||||
|
||||
const LiveGroupFilter = ({ playlist, groupStates, setGroupStates }) => {
|
||||
const LiveGroupFilter = ({
|
||||
playlist,
|
||||
groupStates,
|
||||
setGroupStates,
|
||||
autoEnableNewGroupsLive,
|
||||
setAutoEnableNewGroupsLive,
|
||||
}) => {
|
||||
const channelGroups = useChannelsStore((s) => s.channelGroups);
|
||||
const profiles = useChannelsStore((s) => s.profiles);
|
||||
const streamProfiles = useStreamProfilesStore((s) => s.profiles);
|
||||
|
|
@ -159,6 +165,16 @@ const LiveGroupFilter = ({ playlist, groupStates, setGroupStates }) => {
|
|||
</Text>
|
||||
</Alert>
|
||||
|
||||
<Checkbox
|
||||
label="Automatically enable new groups discovered on future scans"
|
||||
checked={autoEnableNewGroupsLive}
|
||||
onChange={(event) =>
|
||||
setAutoEnableNewGroupsLive(event.currentTarget.checked)
|
||||
}
|
||||
size="sm"
|
||||
description="When disabled, new groups from the M3U source will be created but disabled by default. You can enable them manually later."
|
||||
/>
|
||||
|
||||
<Flex gap="sm">
|
||||
<TextInput
|
||||
placeholder="Filter groups..."
|
||||
|
|
|
|||
|
|
@ -55,6 +55,21 @@ const M3UGroupFilter = ({ playlist = null, isOpen, onClose }) => {
|
|||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [movieCategoryStates, setMovieCategoryStates] = useState([]);
|
||||
const [seriesCategoryStates, setSeriesCategoryStates] = useState([]);
|
||||
const [autoEnableNewGroupsLive, setAutoEnableNewGroupsLive] = useState(true);
|
||||
const [autoEnableNewGroupsVod, setAutoEnableNewGroupsVod] = useState(true);
|
||||
const [autoEnableNewGroupsSeries, setAutoEnableNewGroupsSeries] =
|
||||
useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
if (!playlist) return;
|
||||
|
||||
// Initialize account-level settings
|
||||
setAutoEnableNewGroupsLive(playlist.auto_enable_new_groups_live ?? true);
|
||||
setAutoEnableNewGroupsVod(playlist.auto_enable_new_groups_vod ?? true);
|
||||
setAutoEnableNewGroupsSeries(
|
||||
playlist.auto_enable_new_groups_series ?? true
|
||||
);
|
||||
}, [playlist]);
|
||||
|
||||
useEffect(() => {
|
||||
if (Object.keys(channelGroups).length === 0) {
|
||||
|
|
@ -116,6 +131,14 @@ const M3UGroupFilter = ({ playlist = null, isOpen, onClose }) => {
|
|||
}))
|
||||
.filter((state) => state.enabled !== state.original_enabled);
|
||||
|
||||
// Update account-level settings via the proper account endpoint
|
||||
await API.updatePlaylist({
|
||||
id: playlist.id,
|
||||
auto_enable_new_groups_live: autoEnableNewGroupsLive,
|
||||
auto_enable_new_groups_vod: autoEnableNewGroupsVod,
|
||||
auto_enable_new_groups_series: autoEnableNewGroupsSeries,
|
||||
});
|
||||
|
||||
// Update group settings via API endpoint
|
||||
await API.updateM3UGroupSettings(
|
||||
playlist.id,
|
||||
|
|
@ -176,6 +199,8 @@ const M3UGroupFilter = ({ playlist = null, isOpen, onClose }) => {
|
|||
playlist={playlist}
|
||||
groupStates={groupStates}
|
||||
setGroupStates={setGroupStates}
|
||||
autoEnableNewGroupsLive={autoEnableNewGroupsLive}
|
||||
setAutoEnableNewGroupsLive={setAutoEnableNewGroupsLive}
|
||||
/>
|
||||
</Tabs.Panel>
|
||||
|
||||
|
|
@ -185,6 +210,8 @@ const M3UGroupFilter = ({ playlist = null, isOpen, onClose }) => {
|
|||
categoryStates={movieCategoryStates}
|
||||
setCategoryStates={setMovieCategoryStates}
|
||||
type="movie"
|
||||
autoEnableNewGroups={autoEnableNewGroupsVod}
|
||||
setAutoEnableNewGroups={setAutoEnableNewGroupsVod}
|
||||
/>
|
||||
</Tabs.Panel>
|
||||
|
||||
|
|
@ -194,6 +221,8 @@ const M3UGroupFilter = ({ playlist = null, isOpen, onClose }) => {
|
|||
categoryStates={seriesCategoryStates}
|
||||
setCategoryStates={setSeriesCategoryStates}
|
||||
type="series"
|
||||
autoEnableNewGroups={autoEnableNewGroupsSeries}
|
||||
setAutoEnableNewGroups={setAutoEnableNewGroupsSeries}
|
||||
/>
|
||||
</Tabs.Panel>
|
||||
</Tabs>
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import {
|
|||
Text,
|
||||
Divider,
|
||||
Box,
|
||||
Checkbox,
|
||||
} from '@mantine/core';
|
||||
import { CircleCheck, CircleX } from 'lucide-react';
|
||||
import useVODStore from '../../store/useVODStore';
|
||||
|
|
@ -19,6 +20,8 @@ const VODCategoryFilter = ({
|
|||
categoryStates,
|
||||
setCategoryStates,
|
||||
type,
|
||||
autoEnableNewGroups,
|
||||
setAutoEnableNewGroups,
|
||||
}) => {
|
||||
const categories = useVODStore((s) => s.categories);
|
||||
const [filter, setFilter] = useState('');
|
||||
|
|
@ -85,6 +88,16 @@ const VODCategoryFilter = ({
|
|||
|
||||
return (
|
||||
<Stack style={{ paddingTop: 10 }}>
|
||||
<Checkbox
|
||||
label={`Automatically enable new ${type === 'movie' ? 'movie' : 'series'} categories discovered on future scans`}
|
||||
checked={autoEnableNewGroups}
|
||||
onChange={(event) =>
|
||||
setAutoEnableNewGroups(event.currentTarget.checked)
|
||||
}
|
||||
size="sm"
|
||||
description="When disabled, new categories from the provider will be created but disabled by default. You can enable them manually later."
|
||||
/>
|
||||
|
||||
<Flex gap="sm">
|
||||
<TextInput
|
||||
placeholder="Filter categories..."
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue