Ensuring domain config saves when tagging/untagging a domain.

This commit is contained in:
Roland 2025-12-06 09:27:40 -07:00 • committed by lrnselfreliance
parent 16d24a588d
commit 47044cdd1f
17 changed files with 651 additions and 114 deletions

View file

@ -123,15 +123,8 @@ class DomainsConfig(ConfigFile):
collection_data = collection_data.copy()
collection_data['kind'] = 'domain'
# Warn if tag_name provided without directory
tag_name = collection_data.get('tag_name')
directory = collection_data.get('directory')
if tag_name and not directory:
logger.warning(
f"Domain collection '{name}' has tag_name '{tag_name}' "
f"but no directory - tags require a directory. Tag will be ignored."
)
collection_data.pop('tag_name', None)
# Note: Collections can now be tagged even without a directory
# Tags enable UI search/filtering for directory-less collections
valid_data_list.append(collection_data)
except Exception as e:

View file

@ -20,22 +20,6 @@ async def test_create_domain_collection_with_directory(test_session, test_direct
test_session.commit()
assert collection.directory == domain_dir
assert collection.can_be_tagged is True
@pytest.mark.asyncio
async def test_create_unrestricted_domain_collection(test_session):
"""Unrestricted domain collection (no directory) cannot be tagged."""
collection = Collection(
name='example.com',
kind='domain',
directory=None # Unrestricted
)
test_session.add(collection)
test_session.commit()
assert collection.directory is None
assert collection.can_be_tagged is False
@pytest.mark.asyncio
@ -63,8 +47,8 @@ async def test_tag_domain_collection_with_directory(async_client, test_session,
@pytest.mark.asyncio
async def test_cannot_tag_unrestricted_domain_collection(async_client, test_session, tag_factory):
"""Domain collection without directory cannot be tagged."""
async def test_tag_unrestricted_domain_collection(async_client, test_session, tag_factory):
"""Domain collection without directory can be tagged for UI searchability."""
collection = Collection(
name='example.com',
kind='domain',
@ -74,10 +58,14 @@ async def test_cannot_tag_unrestricted_domain_collection(async_client, test_sess
tag = await tag_factory(name='News')
test_session.commit()
assert collection.can_be_tagged is False
# Collections can now be tagged even without a directory
collection.set_tag('News')
test_session.commit()
with pytest.raises(ValueError, match='Cannot tag domain collection.*without a directory'):
collection.set_tag('News')
assert collection.tag is not None
assert collection.tag.name == 'News'
# Directory should still be None
assert collection.directory is None
@pytest.mark.asyncio
@ -169,13 +157,11 @@ collections:
assert 'example.com' in str(collection.directory)
assert collection.tag is not None
assert collection.tag.name == 'News'
assert collection.can_be_tagged is True
@pytest.mark.asyncio
async def test_domain_config_warns_tag_without_directory(test_session, test_directory, tag_factory, caplog,
async_client):
"""Config warns when tag_name provided without directory."""
async def test_domain_config_with_tag_without_directory(test_session, test_directory, tag_factory, async_client):
"""Config can have tag_name without directory - tag enables UI search."""
from modules.archive.lib import DomainsConfig
# Create config file with tag but no directory
@ -195,18 +181,16 @@ collections:
domains_config = DomainsConfig()
domains_config.import_config(config_file)
# Verify collection created without tag
# Verify collection created with tag but no directory
collection = test_session.query(Collection).filter_by(
name='example.com',
kind='domain'
).one()
assert collection.directory is None
assert collection.tag is None # Tag ignored
assert collection.can_be_tagged is False
# Check warning was logged
assert "tags require a directory" in caplog.text
# Tag is now applied even without directory - enables UI search
assert collection.tag is not None
assert collection.tag.name == 'News'
@pytest.mark.asyncio
@ -268,7 +252,6 @@ async def test_get_or_create_domain_collection_with_directory(test_session, test
assert collection.name == 'test.com'
assert collection.kind == 'domain'
assert collection.directory == domain_dir
assert collection.can_be_tagged is True
@pytest.mark.asyncio
@ -311,3 +294,62 @@ async def test_get_archive_destination_unrestricted(test_session):
expected = get_archive_directory() / 'example.com'
assert destination == expected
@pytest.mark.asyncio
async def test_tag_domain_without_directory_saves_config(test_session, test_directory, tag_factory, await_switches):
"""Tagging domain collection without directory should still save config."""
from modules.archive.lib import domains_config
from wrolpi.collections.lib import tag_collection
import yaml
# Create domain collection without directory
collection = Collection(name='example.com', kind='domain', directory=None)
test_session.add(collection)
await tag_factory(name='News')
test_session.commit()
# Tag using library function (no directory)
tag_collection(collection.id, tag_name='News', directory=None, session=test_session)
test_session.commit()
# Wait for switches to process
await await_switches()
# Verify config was saved (file created by switch, just read it)
config_file = domains_config.get_file()
data = yaml.safe_load(config_file.read_text())
domain_entry = next((c for c in data.get('collections', []) if c['name'] == 'example.com'), None)
assert domain_entry is not None, "Domain collection not found in config"
assert domain_entry.get('tag_name') == 'News', "Tag name not saved in config"
@pytest.mark.asyncio
async def test_untag_domain_without_directory_saves_config(test_session, test_directory, tag_factory, await_switches):
"""Untagging domain collection without directory should still save config."""
from modules.archive.lib import domains_config
from wrolpi.collections.lib import tag_collection
import yaml
# Create domain collection without directory, already tagged
collection = Collection(name='example.com', kind='domain', directory=None)
test_session.add(collection)
tag = await tag_factory(name='News')
collection.tag = tag
test_session.commit()
# Untag using library function (tag_name=None)
tag_collection(collection.id, tag_name=None, directory=None, session=test_session)
test_session.commit()
# Wait for switches to process
await await_switches()
# Verify config was saved without tag (file created by switch, just read it)
config_file = domains_config.get_file()
data = yaml.safe_load(config_file.read_text())
domain_entry = next((c for c in data.get('collections', []) if c['name'] == 'example.com'), None)
assert domain_entry is not None, "Domain collection not found in config"
assert domain_entry.get('tag_name') is None, "Tag should be removed from config"

View file

@ -283,6 +283,8 @@ async def tag_channel(tag_name: str | None, directory: pathlib.Path | None, chan
save_channels_config.activate_switch()
else:
logger.info(f'Tagging {channel} with {tag_name}')
# Always save config when tag changes, even without directory
save_channels_config.activate_switch()
@optional_session

View file

@ -146,3 +146,56 @@ async def test_tag_channel_existing(async_client, test_session, test_directory,
with test_channels_config.open() as fh:
config = yaml.load(fh, Loader=yaml.Loader)
assert config['channels'][0]['tag_name'] == tag.name
@pytest.mark.asyncio
async def test_tag_channel_without_directory_saves_config(
test_session, test_directory, channel_factory, tag_factory, test_channels_config, await_switches
):
"""Tagging channel without providing directory should still save config."""
channel_directory = test_directory / 'videos' / 'Channel Name'
channel = channel_factory(name='Channel Name', directory=channel_directory)
test_session.commit()
await await_switches()
tag = await tag_factory(name='Tech')
# Tag without providing directory (directory=None)
await lib.tag_channel(tag.name, None, channel.id, test_session)
test_session.commit()
await await_switches()
# Channel's tag should be saved to the config even without directory
with test_channels_config.open() as fh:
config = yaml.load(fh, Loader=yaml.Loader)
channel_entry = next((c for c in config.get('channels', []) if c['name'] == 'Channel Name'), None)
assert channel_entry is not None, "Channel not found in config"
assert channel_entry.get('tag_name') == 'Tech', "Tag name not saved in config"
@pytest.mark.asyncio
async def test_untag_channel_without_directory_saves_config(
test_session, test_directory, channel_factory, tag_factory, test_channels_config, await_switches
):
"""Untagging channel without providing directory should still save config."""
channel_directory = test_directory / 'videos' / 'Channel Name'
channel = channel_factory(name='Channel Name', directory=channel_directory)
tag = await tag_factory(name='Tech')
channel.set_tag(tag.name)
test_session.commit()
await await_switches()
# Verify tag is set
assert channel.tag is not None
# Untag without providing directory (tag_name=None, directory=None)
await lib.tag_channel(None, None, channel.id, test_session)
test_session.commit()
await await_switches()
# Channel's tag should be removed in the config
with test_channels_config.open() as fh:
config = yaml.load(fh, Loader=yaml.Loader)
channel_entry = next((c for c in config.get('channels', []) if c['name'] == 'Channel Name'), None)
assert channel_entry is not None, "Channel not found in config"
assert channel_entry.get('tag_name') is None, "Tag should be removed from config"