Moving Channel attachment logic to recurring_download method because only recurring downloads can have Channels.

This commit is contained in:
Roland 2024-08-02 09:44:06 -06:00
parent c3f518060e
commit 3906223844
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
3 changed files with 12 additions and 16 deletions

View file

@ -110,8 +110,8 @@ async def test_create_channel_download(test_session, simple_channel):
test_session.flush()
settings = dict(destination=str(simple_channel.directory))
download_manager.create_download('https://example.com/2', RSSDownloader.name, test_session,
sub_downloader_name=VideoDownloader.name, settings=settings)
download_manager.recurring_download('https://example.com/2', 1, RSSDownloader.name, test_session,
sub_downloader_name=VideoDownloader.name, settings=settings)
download = test_session.query(Download).one()
assert download.settings['destination'] == str(simple_channel.directory)
download = test_session.query(Download).one()

View file

@ -181,9 +181,9 @@ def test_link_channel_and_downloads_migration(test_async_client, test_session, c
"""Test the 8d0d81bc9c34_channel_channel_downloads.py migration."""
# A simple Channel which has a download for its URL.
channel1 = channel_factory(url='https://example.com/channel1')
download1 = Download(url='https://example.com/channel1', downloader=ChannelDownloader.name, frequency=1)
# A Channel which has two Downloads. One for it's URL, another which is an RSS feed in its directory.
channel2 = channel_factory(url='https://example.com/channel2')
download1 = Download(url='https://example.com/channel1', downloader=ChannelDownloader.name, frequency=1)
download2a = Download(url='https://example.com/channel2/rss', downloader=RSSDownloader.name, frequency=1,
settings=dict(destination=str(channel2.directory)))
download2b = Download(url='https://example.com/channel2', downloader=ChannelDownloader.name, frequency=1,

View file

@ -253,12 +253,6 @@ class Download(ModelHelper, Base): # noqa
return download
raise UnknownDownload(f'Cannot find Download with URL: {url}')
@staticmethod
@optional_session
def get_by_channel_id(channel_id: int, session: Session = None) -> Optional['Download']:
download = session.query(Download).filter(Download.channel_id == channel_id).one_or_none()
return download
class Downloader:
name: str = None
@ -537,13 +531,6 @@ class DownloadManager:
download.sub_downloader = sub_downloader_name
download.settings = settings if settings is not None else download.settings
from modules.videos.models import Channel
if channel := Channel.get_by_url(url=download.url, session=session):
download.channel_id = channel.id
if destination := (download.settings or dict()).get('destination'):
if channel := Channel.get_by_path(destination, session):
download.channel_id = channel.id
downloads.append(download)
logger.debug(f'Created {len(downloads)} new downloads')
@ -582,6 +569,15 @@ class DownloadManager:
reset_attempts=reset_attempts, sub_downloader_name=sub_downloader_name,
settings=settings)
download.frequency = frequency
# Only recurring Downloads can be Channel Downloads.
from modules.videos.models import Channel
if channel := Channel.get_by_url(url=download.url, session=session):
download.channel_id = channel.id
if destination := (download.settings or dict()).get('destination'):
if channel := Channel.get_by_path(destination, session):
download.channel_id = channel.id
session.commit()
return download