From 75fb9c435189d918228b4df25178a0ae460d5bdf Mon Sep 17 00:00:00 2001 From: Roland Date: Sun, 2 Apr 2023 11:47:16 -0600 Subject: [PATCH] A download will be created when a Channel is created. --- modules/videos/channel/lib.py | 15 +++++---------- modules/videos/channel/test/test_api.py | 17 +++++++++++++++++ modules/videos/models.py | 10 +++++++++- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/modules/videos/channel/lib.py b/modules/videos/channel/lib.py index 0dc4891d..c36c3cb4 100644 --- a/modules/videos/channel/lib.py +++ b/modules/videos/channel/lib.py @@ -161,18 +161,13 @@ def create_channel(session: Session, data: schema.ChannelPostRequest, return_dic except APIError as e: raise ValidationError() from e - channel = Channel( - name=data.name, - url=data.url or None, - match_regex=data.match_regex, - directory=get_media_directory() / data.directory, - download_frequency=data.download_frequency, - source_id=data.source_id, - ) + channel = Channel() session.add(channel) + session.flush([channel]) + # Apply the changes now that we've OK'd them + channel.update(data.__dict__) + session.commit() - session.flush() - session.refresh(channel) return channel.dict() if return_dict else channel diff --git a/modules/videos/channel/test/test_api.py b/modules/videos/channel/test/test_api.py index 7a903108..8a8c44aa 100644 --- a/modules/videos/channel/test/test_api.py +++ b/modules/videos/channel/test/test_api.py @@ -416,6 +416,23 @@ def test_channel_crud(test_session, test_client, test_directory, test_download_m assert response.status_code == HTTPStatus.NOT_FOUND +def test_channel_creation_with_download(test_client, test_session, test_directory): + """A Download is created when a Channel is created.""" + new_channel = dict( + directory=str(test_directory), + name='Download this one', + url='https://example.com/channel1', + download_frequency=60, + ) + request, response = test_client.post('/api/videos/channels', content=json.dumps(new_channel)) + assert response.status_code == HTTPStatus.CREATED + + download = test_session.query(Download).one() + assert download.url == 'https://example.com/channel1' + assert download.downloader == 'video_channel' + assert download.sub_downloader == 'video' + + def test_change_channel_url(test_client, test_session, test_download_manager, download_channel, test_download_manager_config): # Change the Channel's URL. diff --git a/modules/videos/models.py b/modules/videos/models.py index 77f3179d..7ab0b3e4 100644 --- a/modules/videos/models.py +++ b/modules/videos/models.py @@ -343,6 +343,10 @@ class Channel(ModelHelper, Base): # Get the download before we change the URL. download = self.get_download() + # URL should not be empty string. + if 'url' in data: + data['url'] = data['url'] or None + for key, value in data.items(): setattr(self, key, value) @@ -362,8 +366,12 @@ class Channel(ModelHelper, Base): # Keep next_download if available. download.next_download = download.next_download or download_manager.calculate_next_download(download, session) + download.sub_downloader = 'video' elif not download and self.download_frequency and self.url: - download = Download(frequency=self.download_frequency, url=self.url, downloader='video_channel') + download = Download(frequency=self.download_frequency, url=self.url, + downloader='video_channel', + sub_downloader='video', + ) session.add(download) session.flush() download.next_download = download_manager.calculate_next_download(download, session)