From abff69dad34e28ab25568d51398d8c41a1bdfa77 Mon Sep 17 00:00:00 2001 From: Roland Date: Wed, 7 Oct 2026 13:38:58 -0600 Subject: [PATCH] videos: refuse Channel changes until channels.yaml is imported A Channel created through the API before channels.yaml was imported (e.g. while WROLPi starts) could not be saved, and the import which followed deleted it, and its Downloads, as "not in the config". Creating, changing, tagging and deleting a Channel (through the Channel or Collection API) now wait for the import (503, "still starting"). Co-Authored-By: Claude Opus 5.5 --- modules/videos/channel/lib.py | 13 +++++- modules/videos/test/test_channels_config.py | 44 +++++++++++++++++++++ wrolpi/collections/lib.py | 4 ++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/modules/videos/channel/lib.py b/modules/videos/channel/lib.py index f32d31c1..b4e983e2 100644 --- a/modules/videos/channel/lib.py +++ b/modules/videos/channel/lib.py @@ -17,12 +17,19 @@ from wrolpi.tags import Tag from .. import schema from ..common import check_for_channel_conflicts from ..errors import UnknownChannel -from ..lib import save_channels_config +from ..lib import save_channels_config, get_channels_config from ..models import Channel, Video logger = logger.getChild(__name__) +def check_channels_config_imported(): + """Raise ConfigNotImported until channels.yaml is imported; the import would undo a change made before it.""" + # A test may have no channels config. + if config := get_channels_config(): + config.check_imported() + + async def get_minimal_channels(session: Session) -> List[dict]: """ Get the minimum amount of information necessary about all channels. @@ -112,6 +119,7 @@ def get_channel(session: Session, *, channel_id: int = None, source_id: str = No async def update_channel(session: Session, *, data: schema.ChannelPutRequest, channel_id: int) -> Channel: """Update a Channel's DB record""" + check_channels_config_imported() channel = Channel.find_by_id(session, channel_id) # Verify that the URL/Name/directory aren't taken @@ -149,6 +157,7 @@ def create_channel(session: Session, data: schema.ChannelPostRequest, return_dic """ Create a new Channel. Check for conflicts with existing Channels. """ + check_channels_config_imported() try: # Verify that the URL/Name/directory aren't taken check_for_channel_conflicts( @@ -189,6 +198,7 @@ def create_channel(session: Session, data: schema.ChannelPostRequest, return_dic def delete_channel(session: Session, *, channel_id: int): + check_channels_config_imported() channel = Channel.find_by_id(session, channel_id) channel_dict = channel.dict() @@ -292,6 +302,7 @@ async def tag_channel(session: Session, tag_name: str | None, directory: pathlib """Add a Tag to a Channel, or remove a Tag from a Channel if no `tag_name` is provided. Move the Channel to the new directory, if provided.""" + check_channels_config_imported() if directory and flags.file_worker_busy.is_set(): raise FileWorkerConflict('Refusing to move channel while FileWorker is busy') diff --git a/modules/videos/test/test_channels_config.py b/modules/videos/test/test_channels_config.py index dbd53ca4..c1bf085d 100644 --- a/modules/videos/test/test_channels_config.py +++ b/modules/videos/test/test_channels_config.py @@ -6,6 +6,9 @@ Tests cover: 2. Upgrade - config file exists with data 3. Edge cases - empty config, missing config with DB data """ +import json +from http import HTTPStatus + import pytest import yaml @@ -322,3 +325,44 @@ class TestChannelsConfigEdgeCases: # Verify file_format restored test_session.refresh(channel) assert channel.collection.file_format == '%(upload_date)s_%(title)s.%(ext)s' + + +@pytest.mark.asyncio +async def test_channel_changes_wait_for_config_import(test_session, test_directory, async_client, channel_factory, + test_channels_config): + """Until channels.yaml is imported (e.g. while WROLPi starts), Channels cannot change; the import would undo the + change (a new Channel would be deleted, with its Downloads).""" + channel = channel_factory(name='Kept') + test_session.commit() + config = get_channels_config() + config.dump_config() + assert config.get_file().is_file() + config.successful_import = False + + cid, collection_id = channel.id, channel.collection_id + requests = [ + ('post', '/api/videos/channels', dict(name='New', directory='videos/new', url='https://example.com/new')), + ('put', f'/api/videos/channels/{cid}', dict(name='Changed', directory=str(channel.directory))), + ('post', f'/api/videos/channels/{cid}/tag', dict(tag_name='Tagged')), + ('delete', f'/api/videos/channels/{cid}', None), + ('put', f'/api/collections/{collection_id}', dict(description='changed')), + ('post', f'/api/collections/{collection_id}/tag', dict(tag_name='Tagged')), + ('delete', f'/api/collections/{collection_id}', None), + ] + for method, url, body in requests: + kwargs = dict(content=json.dumps(body)) if body else {} + request, response = await getattr(async_client, method)(url, **kwargs) + assert response.status_code == HTTPStatus.SERVICE_UNAVAILABLE, (method, url, response.json) + assert response.json['code'] == 'CONFIG_NOT_IMPORTED' + + test_session.expire_all() + channel = test_session.query(Channel).one() + assert channel.name == 'Kept' + assert channel.tag_name is None + assert channel.collection.description is None + + # Once imported, Channels can change again. + config.import_config() + request, response = await async_client.put(f'/api/collections/{collection_id}', + content=json.dumps(dict(description='x'))) + assert response.status_code == HTTPStatus.OK, response.json diff --git a/wrolpi/collections/lib.py b/wrolpi/collections/lib.py index 990136ea..001e2320 100644 --- a/wrolpi/collections/lib.py +++ b/wrolpi/collections/lib.py @@ -49,6 +49,10 @@ def check_config_imported(kind: str): # Local import to avoid circular import: collections -> archive -> collections from modules.archive.lib import get_domains_config get_domains_config().check_imported() + elif kind == 'channel': + # Local import to avoid circular import: collections -> videos -> collections + from modules.videos.channel.lib import check_channels_config_imported + check_channels_config_imported() def _activate_config_save(kind: str):