From 2b0b161dad7f46ac8f3b6038c6bcf532876b2fdf Mon Sep 17 00:00:00 2001 From: Roland Date: Thu, 25 Dec 2025 15:42:35 -0700 Subject: [PATCH] Changing archive special directory default to include domain tag. --- modules/archive/lib.py | 2 +- modules/archive/models.py | 7 +++++++ modules/archive/test/test_models.py | 4 ++-- wrolpi/collections/test/test_models.py | 8 ++++---- wrolpi/common.py | 2 +- wrolpi/schema.py | 2 +- wrolpi/test/test_config_api.py | 2 +- wrolpi/test/test_root_api.py | 4 ++-- 8 files changed, 19 insertions(+), 12 deletions(-) diff --git a/modules/archive/lib.py b/modules/archive/lib.py index e6ee4d81..9a7cd4db 100644 --- a/modules/archive/lib.py +++ b/modules/archive/lib.py @@ -378,7 +378,7 @@ class ArchiveFiles: def get_archive_directory() -> pathlib.Path: archive_destination = get_wrolpi_config().archive_destination - variables = dict(domain='', year='', month='', day='') + variables = dict(domain='', domain_tag='', year='', month='', day='') archive_destination = archive_destination % variables archive_directory = get_media_directory() / archive_destination return archive_directory diff --git a/modules/archive/models.py b/modules/archive/models.py index 4472f6f2..acb292af 100644 --- a/modules/archive/models.py +++ b/modules/archive/models.py @@ -390,6 +390,7 @@ class Archive(Base, ModelHelper): This uses the configured archive_destination template with variables: - domain: the domain name from the Collection + - domain_tag: the tag name from the Collection (empty string if no tag) - year, month, day: current date Returns: @@ -397,9 +398,15 @@ class Archive(Base, ModelHelper): """ archive_destination = get_wrolpi_config().archive_destination + # Get the tag name from the collection if it exists + domain_tag = '' + if self.collection and self.collection.tag: + domain_tag = self.collection.tag.name + now_ = now() variables = dict( domain=self.domain or 'unknown', + domain_tag=domain_tag, year=now_.year, month=now_.month, day=now_.day, diff --git a/modules/archive/test/test_models.py b/modules/archive/test/test_models.py index 4a74aae1..4b9985e2 100644 --- a/modules/archive/test/test_models.py +++ b/modules/archive/test/test_models.py @@ -51,11 +51,11 @@ async def test_archive_download_destination(async_client, test_session, test_dir assert str(archive.download_directory) == str(test_directory / 'archive/wrolpi.org') # Year of download is supported. - wrolpi_config.archive_destination = 'archives/%(domain)s/%(year)s' + wrolpi_config.archive_destination = 'archives/%(domain_tag)s/%(domain)s/%(year)s' assert str(archive.download_directory) == str(test_directory / 'archives/wrolpi.org/2000') # More download date is supported. - wrolpi_config.archive_destination = 'archive/%(domain)s/%(year)s/%(month)s/%(day)s' + wrolpi_config.archive_destination = 'archive/%(domain_tag)s/%(domain)s/%(year)s/%(month)s/%(day)s' assert str(archive.download_directory) == str(test_directory / 'archive/wrolpi.org/2000/1/2') diff --git a/wrolpi/collections/test/test_models.py b/wrolpi/collections/test/test_models.py index 8a886542..6bbcd1ca 100644 --- a/wrolpi/collections/test/test_models.py +++ b/wrolpi/collections/test/test_models.py @@ -59,13 +59,13 @@ def test_collection_format_destination_domain(test_session, test_directory): test_session.flush() destination = collection.format_destination() - # Should use archive_destination template: archive/%(domain)s + # Should use archive_destination template: archive/%(domain_tag)s/%(domain)s + # With no tag, pathlib normalizes archive//example.com to archive/example.com assert destination == test_directory / 'archive' / 'example.com' - # With tag - default template doesn't include %(domain_tag)s, so tag has no effect + # With tag - default template includes %(domain_tag)s destination_with_tag = collection.format_destination(tag_name='Tech') - # Default archive_destination is 'archive/%(domain)s' (no tag placeholder) - assert destination_with_tag == test_directory / 'archive' / 'example.com' + assert destination_with_tag == test_directory / 'archive' / 'Tech' / 'example.com' @pytest.mark.asyncio diff --git a/wrolpi/common.py b/wrolpi/common.py index 66ddae03..3afe15be 100644 --- a/wrolpi/common.py +++ b/wrolpi/common.py @@ -784,7 +784,7 @@ def get_config_by_file_name(file_name: str) -> ConfigFile: class WROLPiConfig(ConfigFile): file_name = 'wrolpi.yaml' default_config = dict( - archive_destination='archive/%(domain)s', + archive_destination='archive/%(domain_tag)s/%(domain)s', download_on_startup=True, download_timeout=0, hotspot_device='wlan0', diff --git a/wrolpi/schema.py b/wrolpi/schema.py index 2583f201..3bdbb71d 100644 --- a/wrolpi/schema.py +++ b/wrolpi/schema.py @@ -32,7 +32,7 @@ class SettingsResponse: throttle_status: str version: str wrol_mode: bool - archive_destination: str = 'archive/%(domain)s' + archive_destination: str = 'archive/%(domain_tag)s/%(domain)s' map_destination: str = 'map' videos_destination: str = 'videos/%(channel_tag)s/%(channel_name)s' zims_destination: str = 'zims' diff --git a/wrolpi/test/test_config_api.py b/wrolpi/test/test_config_api.py index 6c00b398..ccb0b6a0 100644 --- a/wrolpi/test/test_config_api.py +++ b/wrolpi/test/test_config_api.py @@ -57,7 +57,7 @@ async def test_configs_crud(async_client, test_session, test_tags_config, tag_fa request, response = await async_client.get('/api/config?file_name=wrolpi.yaml') assert response.status_code == HTTPStatus.OK wrolpi_config = response.json['config'] - assert wrolpi_config['archive_destination'] == 'archive/%(domain)s' + assert wrolpi_config['archive_destination'] == 'archive/%(domain_tag)s/%(domain)s' # Change config value. wrolpi_config['archive_destination'] = 'some new value' body = dict(config=wrolpi_config) diff --git a/wrolpi/test/test_root_api.py b/wrolpi/test/test_root_api.py index 82e87832..40788ee7 100644 --- a/wrolpi/test/test_root_api.py +++ b/wrolpi/test/test_root_api.py @@ -702,7 +702,7 @@ async def test_settings_special_directories(async_client, test_wrolpi_config): """Maintainer can change special directories.""" request, response = await async_client.get('/api/settings') assert response.status_code == HTTPStatus.OK - assert response.json['archive_destination'] == 'archive/%(domain)s' + assert response.json['archive_destination'] == 'archive/%(domain_tag)s/%(domain)s' assert response.json['map_destination'] == 'map' assert response.json['videos_destination'] == 'videos/%(channel_tag)s/%(channel_name)s' assert response.json['zims_destination'] == 'zims' @@ -730,7 +730,7 @@ async def test_settings_special_directories(async_client, test_wrolpi_config): data = {'archive_destination': ''} request, response = await async_client.patch('/api/settings', content=json.dumps(data)) assert response.status_code == HTTPStatus.NO_CONTENT - assert get_wrolpi_config().archive_destination == 'archive/%(domain)s' + assert get_wrolpi_config().archive_destination == 'archive/%(domain_tag)s/%(domain)s' @pytest.mark.asyncio