Creating channel directory if requested.

This commit is contained in:
Roland 2022-01-15 13:23:57 -07:00
parent 862909fe7e
commit 4a2fb577da
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
6 changed files with 45 additions and 16 deletions

View file

@ -4,9 +4,8 @@ from http import HTTPStatus
from sanic import response, Blueprint
from sanic.request import Request
from wrolpi.common import logger, wrol_mode_check, get_relative_to_media_directory, make_media_directory, \
from wrolpi.common import logger, wrol_mode_check, make_media_directory, \
get_media_directory
from wrolpi.errors import UnknownDirectory
from wrolpi.root_api import json_response
from wrolpi.schema import validate_doc, JSONErrorResponse
from .lib import get_minimal_channels, delete_channel, update_channel, get_channel, create_channel
@ -53,16 +52,10 @@ def channel_get(_: Request, link: str):
)
@wrol_mode_check
def channel_post(_: Request, data: dict):
try:
# Channel directory is relative to the media directory. Channel directory may not be in "videos" directory!
data['directory'] = get_media_directory() / data['directory']
except UnknownDirectory:
if data['mkdir']:
make_media_directory(data['directory'])
data['directory'] = get_relative_to_media_directory(data['directory'])
else:
raise
print(data['directory'])
# Channel directory is relative to the media directory. Channel directory may not be in "videos" directory!
data['directory'] = get_media_directory() / data['directory']
if data.get('mkdir') is True:
make_media_directory(data['directory'])
if download_frequency := data.get('download_frequency'):
try:

View file

@ -613,6 +613,7 @@ def test_download_channel_no_refresh(test_session, download_channel, video_downl
"""
A Channel cannot be downloaded until it has been refreshed.
"""
def check_refreshed(expected: bool):
channel = test_session.query(Channel).one()
assert channel.refreshed == expected
@ -625,3 +626,26 @@ def test_download_channel_no_refresh(test_session, download_channel, video_downl
video_download_manager.do_downloads_sync()
check_refreshed(True)
def test_channel_post_directory(test_session, test_client, test_directory):
"""
A Channel can be created with or without an existing directory.
"""
# Channel can be created with a missing directory.
data = dict(name='foo', directory='foo')
request, response = test_client.post('/api/videos/channels', content=json.dumps(data))
assert response.status_code == HTTPStatus.CREATED
directory = test_session.query(Channel).filter_by(link='foo').one().directory.path
assert (test_directory / 'foo') == directory
assert not directory.is_dir()
assert directory.is_absolute()
# Channel can be created and have its directory be created.
data = dict(name='bar', directory='bar', mkdir=True)
request, response = test_client.post('/api/videos/channels', content=json.dumps(data))
assert response.status_code == HTTPStatus.CREATED
directory = test_session.query(Channel).filter_by(link='bar').one().directory.path
assert (test_directory / 'bar') == directory
assert directory.is_dir()
assert directory.is_absolute()

View file

@ -0,0 +1,9 @@
def test_delete_channel_no_url(test_session, test_client, channel_factory):
"""
A Channel can be deleted even if it has no URL.
"""
channel = channel_factory()
channel.url = None
test_session.commit()
channel.delete_with_videos()

View file

@ -294,7 +294,7 @@ class Channel(ModelHelper, Base):
session = Session.object_session(self)
# Delete the video records, but not the video files!
session.query(Video).filter_by(channel_id=self.id).delete()
if download := self.get_download():
if self.url and (download := self.get_download()):
session.delete(download)
session.delete(self)