Add validation to Load/Process and Preview actions, update to v0.5.0d

Changes:
1. Extract validation logic into _validate_plugin_settings helper method:
   - Returns (has_errors, validation_results, token) tuple
   - Validates API connection, profiles, groups, thresholds, etc.
   - Reusable across multiple action methods

2. Update validate_settings_action:
   - Now calls _validate_plugin_settings helper
   - Formats results for UI display

3. Add validation to load_process_channels_action:
   - Validates settings before loading channels
   - Prevents processing with invalid profiles or groups
   - Returns clear error messages if validation fails

4. Add validation to preview_changes_action:
   - Validates settings before previewing changes
   - Ensures settings are still valid when preview is run
   - Returns clear error messages if validation fails

5. Update version to 0.5.0d

Benefits:
- Prevents users from proceeding with invalid settings
- Provides immediate feedback on configuration errors
- Ensures channel profiles and groups exist before processing
- Improves user experience with clear error messages
- Maintains DRY principle with shared validation logic
This commit is contained in:
Claude
2025-11-09 18:12:59 +00:00
parent 5e5d3ed49d
commit c2f2e0c03a

View File

@@ -31,7 +31,7 @@ class Plugin:
"""Dispatcharr Stream-Mapparr Plugin""" """Dispatcharr Stream-Mapparr Plugin"""
name = "Stream-Mapparr" name = "Stream-Mapparr"
version = "0.5.0b" version = "0.5.0d"
description = "🎯 Automatically add matching streams to channels based on name similarity and quality precedence with enhanced fuzzy matching" description = "🎯 Automatically add matching streams to channels based on name similarity and quality precedence with enhanced fuzzy matching"
# Settings rendered by UI # Settings rendered by UI
@@ -752,10 +752,16 @@ class Plugin:
LOGGER.error(traceback.format_exc()) LOGGER.error(traceback.format_exc())
return {"status": "error", "message": str(e)} return {"status": "error", "message": str(e)}
def validate_settings_action(self, settings, logger): def _validate_plugin_settings(self, settings, logger):
"""Validate all plugin settings including profiles, groups, and API connection.""" """
Helper method to validate plugin settings.
Returns:
Tuple of (has_errors: bool, validation_results: list, token: str or None)
"""
validation_results = [] validation_results = []
has_errors = False has_errors = False
token = None
try: try:
# 1. Validate API Connection # 1. Validate API Connection
@@ -765,10 +771,7 @@ class Plugin:
validation_results.append(f"❌ API Connection: FAILED - {error}") validation_results.append(f"❌ API Connection: FAILED - {error}")
has_errors = True has_errors = True
# Cannot continue without API access # Cannot continue without API access
return { return has_errors, validation_results, token
"status": "error",
"message": "Validation failed:\n\n" + "\n".join(validation_results)
}
else: else:
validation_results.append("✅ API Connection: SUCCESS") validation_results.append("✅ API Connection: SUCCESS")
@@ -906,31 +909,42 @@ class Plugin:
else: else:
validation_results.append(" Ignore Tags: None configured") validation_results.append(" Ignore Tags: None configured")
# Build summary message # Return validation results
if has_errors: return has_errors, validation_results, token
message = "Validation completed with errors:\n\n" + "\n".join(validation_results)
message += "\n\nPlease fix the errors above before proceeding."
return {"status": "error", "message": message}
else:
message = "All settings validated successfully!\n\n" + "\n".join(validation_results)
message += "\n\nYou can now proceed with 'Load/Process Channels'."
return {"status": "success", "message": message}
except Exception as e: except Exception as e:
logger.error(f"[Stream-Mapparr] Error validating settings: {str(e)}") logger.error(f"[Stream-Mapparr] Error validating settings: {str(e)}")
validation_results.append(f"❌ Unexpected error during validation: {str(e)}") validation_results.append(f"❌ Unexpected error during validation: {str(e)}")
return { has_errors = True
"status": "error", return has_errors, validation_results, token
"message": "Validation failed:\n\n" + "\n".join(validation_results)
} def validate_settings_action(self, settings, logger):
"""Validate all plugin settings including profiles, groups, and API connection."""
has_errors, validation_results, token = self._validate_plugin_settings(settings, logger)
# Build summary message
if has_errors:
message = "Validation completed with errors:\n\n" + "\n".join(validation_results)
message += "\n\nPlease fix the errors above before proceeding."
return {"status": "error", "message": message}
else:
message = "All settings validated successfully!\n\n" + "\n".join(validation_results)
message += "\n\nYou can now proceed with 'Load/Process Channels'."
return {"status": "success", "message": message}
def load_process_channels_action(self, settings, logger): def load_process_channels_action(self, settings, logger):
"""Load and process channels from specified profile and groups.""" """Load and process channels from specified profile and groups."""
try: try:
# Get API token # Validate settings before proceeding
token, error = self._get_api_token(settings, logger) logger.info("[Stream-Mapparr] Validating settings before loading channels...")
if error: has_errors, validation_results, token = self._validate_plugin_settings(settings, logger)
return {"status": "error", "message": error}
if has_errors:
message = "Cannot load channels - validation failed:\n\n" + "\n".join(validation_results)
message += "\n\nPlease fix the errors above before proceeding."
return {"status": "error", "message": message}
logger.info("[Stream-Mapparr] Settings validated successfully, proceeding with channel load...")
profile_names_str = settings.get("profile_name", "").strip() profile_names_str = settings.get("profile_name", "").strip()
selected_groups_str = settings.get("selected_groups", "").strip() selected_groups_str = settings.get("selected_groups", "").strip()
@@ -1224,6 +1238,17 @@ class Plugin:
} }
try: try:
# Validate settings before previewing
logger.info("[Stream-Mapparr] Validating settings before previewing changes...")
has_errors, validation_results, token = self._validate_plugin_settings(settings, logger)
if has_errors:
message = "Cannot preview changes - validation failed:\n\n" + "\n".join(validation_results)
message += "\n\nPlease fix the errors above before proceeding."
return {"status": "error", "message": message}
logger.info("[Stream-Mapparr] Settings validated successfully, proceeding with preview...")
# Load channel data from channels.json # Load channel data from channels.json
channels_data = self._load_channels_data(logger) channels_data = self._load_channels_data(logger)