diff --git a/modules/archive/lib.py b/modules/archive/lib.py index 2c271228..ba90469e 100644 --- a/modules/archive/lib.py +++ b/modules/archive/lib.py @@ -1018,10 +1018,12 @@ def domain_download_links(session: Session) -> Dict[int, int]: links = dict() downloads = session.query(Download.id, Download.url, Download.downloader, Download.sub_downloader, - Download.settings) \ + Download.destination, Download.settings) \ .filter(Download.frequency.isnot(None), Download.collection_id.is_(None)) - for id_, url, downloader, sub_downloader, settings in downloads: - if destination := (settings or dict()).get('destination'): + for id_, url, downloader, sub_downloader, destination, settings in downloads: + # An old Download kept its destination in its settings. + destination = str(destination) if destination else (settings or dict()).get('destination') + if destination: for collection_id, directory, prefix in directories: if destination == directory or destination.startswith(prefix): links[id_] = collection_id diff --git a/modules/archive/test/test_lib.py b/modules/archive/test/test_lib.py index 316f58b1..2d6379e7 100644 --- a/modules/archive/test/test_lib.py +++ b/modules/archive/test/test_lib.py @@ -1277,6 +1277,30 @@ def test_link_domain_and_downloads(test_session, test_download_manager): assert download4.collection_id is None # no frequency = no link +def test_link_domain_and_downloads_by_destination(test_session, test_directory, test_download_manager): + """A Download whose destination column is in a domain's directory is linked to that domain (the destination was + moved out of the settings).""" + from wrolpi.downloader import Download + from modules.archive.lib import link_domain_and_downloads + + collection = Collection(name='example.com', kind='domain', directory=test_directory / 'archive/example.com') + test_session.add(collection) + exact = Download(url='https://other.com/rss', downloader='rss', sub_downloader='archive', frequency=86400, + destination=test_directory / 'archive/example.com') + below = Download(url='https://other.com/rss2', downloader='rss', sub_downloader='archive', frequency=86400, + destination=test_directory / 'archive/example.com/2025/01') + similar = Download(url='https://other.com/rss3', downloader='rss', sub_downloader='archive', frequency=86400, + destination=test_directory / 'archive/example.com.au') + test_session.add_all([exact, below, similar]) + test_session.commit() + + link_domain_and_downloads(test_session) + + assert exact.collection_id == collection.id + assert below.collection_id == collection.id + assert similar.collection_id is None + + @skip_ci @pytest.mark.asyncio async def test_archive_download_uses_domain_collection_directory(async_client, test_session, test_directory, fake_now,