From 529426e411c1e500f9120d5a6ca9768386249123 Mon Sep 17 00:00:00 2001 From: Markbeep Date: Sun, 30 Mar 2025 15:16:00 +0200 Subject: [PATCH] fix not being able to disable indexers --- app/internal/indexers/abstract.py | 31 ++++++++++++++++++--------- app/internal/indexers/indexer_util.py | 16 ++++++++++++-- app/internal/indexers/mam.py | 7 ++++++ app/routers/settings.py | 27 +++++++++++++++++------ 4 files changed, 62 insertions(+), 19 deletions(-) diff --git a/app/internal/indexers/abstract.py b/app/internal/indexers/abstract.py index 1e34565..d7c8273 100644 --- a/app/internal/indexers/abstract.py +++ b/app/internal/indexers/abstract.py @@ -17,6 +17,27 @@ class SessionContainer(BaseModel, arbitrary_types_allowed=True): class AbstractIndexer[T: Configurations](ABC): name: str + @staticmethod + @abstractmethod + async def get_configurations( + container: SessionContainer, + ) -> T: + """ + Returns a list of configuration options that will be configurable on the frontend. + """ + pass + + @abstractmethod + async def is_active( + self, + container: SessionContainer, + configurations: Any, + ) -> bool: + """ + Returns true if the indexer is active and can be used. + """ + pass + @abstractmethod async def setup( self, @@ -33,16 +54,6 @@ class AbstractIndexer[T: Configurations](ABC): """ pass - @staticmethod - @abstractmethod - async def get_configurations( - container: SessionContainer, - ) -> T: - """ - Returns a list of configuration options that will be configurable on the frontend. - """ - pass - @abstractmethod async def is_matching_source( self, source: ProwlarrSource, container: SessionContainer diff --git a/app/internal/indexers/indexer_util.py b/app/internal/indexers/indexer_util.py index 097bdc9..7421b59 100644 --- a/app/internal/indexers/indexer_util.py +++ b/app/internal/indexers/indexer_util.py @@ -21,7 +21,10 @@ class IndexerContext(BaseModel, arbitrary_types_allowed=True): async def get_indexer_contexts( - container: SessionContainer, *, check_required: bool = True + container: SessionContainer, + *, + check_required: bool = True, + return_disabled: bool = False, ) -> list[IndexerContext]: contexts: list[IndexerContext] = [] for Indexer in indexers: @@ -37,9 +40,18 @@ async def get_indexer_contexts( container.session, check_required=check_required, ) + + indexer = Indexer() + + if not return_disabled and not await indexer.is_active( + container, valued_configuration + ): + logger.debug("Indexer %s is disabled", Indexer.name) + continue + contexts.append( IndexerContext( - indexer=Indexer(), + indexer=indexer, configuration=filtered_configuration, valued=valued_configuration, ) diff --git a/app/internal/indexers/mam.py b/app/internal/indexers/mam.py index bea2b81..64d189a 100644 --- a/app/internal/indexers/mam.py +++ b/app/internal/indexers/mam.py @@ -48,6 +48,13 @@ class MamIndexer(AbstractIndexer[MamConfigurations]): ) -> MamConfigurations: return MamConfigurations() + async def is_active( + self, + container: SessionContainer, + configurations: ValuedMamConfigurations, + ) -> bool: + return configurations.mam_active + async def setup( self, request: BookRequest, diff --git a/app/routers/settings.py b/app/routers/settings.py index b287229..beb376f 100644 --- a/app/routers/settings.py +++ b/app/routers/settings.py @@ -1,4 +1,5 @@ import json +import logging import uuid from typing import Annotated, Any, Optional, cast @@ -30,6 +31,8 @@ from app.util.templates import template_response from app.util.time import Minute from app.util.toast import ToastException +logger = logging.getLogger(__name__) + router = APIRouter(prefix="/settings") @@ -720,6 +723,7 @@ async def read_indexers( contexts = await get_indexer_contexts( SessionContainer(session=session, client_session=client_session), check_required=False, + return_disabled=True, ) return template_response( @@ -747,6 +751,7 @@ async def update_indexers( contexts = await get_indexer_contexts( SessionContainer(session=session, client_session=client_session), check_required=False, + return_disabled=True, ) updated_context: Optional[IndexerContext] = None @@ -760,14 +765,22 @@ async def update_indexers( form_values = await request.form() - for key, value in form_values.items(): - if key in updated_context.configuration and type(value) is str: - if updated_context.configuration[key].type is bool: - indexer_configuration_cache.set( - session, key, "true" if value == "on" else "" - ) + for key, context in updated_context.configuration.items(): + value = form_values.get(key) + if value is None: # forms do not include false checkboxes + if context.type is bool: + value = False else: - indexer_configuration_cache.set(session, key, str(value)) + logger.error( + "Missing value for '%s' while trying to update indexer", key + ) + continue + if context.type is bool: + indexer_configuration_cache.set( + session, key, "true" if value == "on" else "" + ) + else: + indexer_configuration_cache.set(session, key, str(value)) flush_prowlarr_cache()