A download will be created when a Channel is created.

This commit is contained in:
Roland 2023-04-02 11:47:16 -06:00
parent 06dd8a168e
commit 75fb9c4351
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
3 changed files with 31 additions and 11 deletions

View file

@ -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

View file

@ -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.

View file

@ -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)