Adding edit form for Scrape downloader.

This commit is contained in:
Roland 2024-12-03 12:44:38 -07:00
parent bf29bdcff7
commit fe521c99f3
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
17 changed files with 314 additions and 262 deletions

View file

@ -107,34 +107,6 @@ def channel_refresh(_: Request, channel_id: int):
return response.empty()
@channel_bp.post('/<channel_id:int>/download', name='post_channel_download')
@channel_bp.put('/<channel_id:int>/download/<download_id:int>', name='put_channel_download')
@openapi.description('Create or update a Channel Download record')
@validate(schema.ChannelDownloadRequest)
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
@wrol_mode_check
async def channel_download(_: Request, channel_id: int, body: schema.ChannelDownloadRequest, download_id: int = None):
if download_id:
await lib.update_channel_download(
channel_id,
download_id,
body.url,
body.frequency,
body.settings,
)
else:
await lib.create_channel_download(
channel_id,
body.url,
body.frequency,
body.settings,
)
if download_manager.is_disabled:
# Warn the user that downloads are disabled.
Events.send_downloads_disabled('Channel download created. But downloads are disabled.')
return response.empty()
@channel_bp.post('/<channel_id:int>/tag')
@openapi.definition(
description='Tag/untag a Channel with a single tag',

View file

@ -1,4 +1,3 @@
import asyncio
import json
import pathlib
import shutil
@ -10,6 +9,7 @@ import mock
import pytest
from modules.videos.common import get_videos_directory
from modules.videos.conftest import simple_channel
from modules.videos.downloader import ChannelDownloader
from modules.videos.lib import save_channels_config, get_channels_config
from modules.videos.models import Channel, Video
@ -71,7 +71,7 @@ def test_channel_no_download_frequency(test_client, test_session, test_directory
# Update the Channel with a frequency.
new_download = {'urls': [simple_channel.url], 'frequency': 10, 'downloader': ChannelDownloader.name}
request, response = test_client.post('/api/download', json=new_download)
assert response.status_code == HTTPStatus.NO_CONTENT, response.json
assert response.status_code == HTTPStatus.CREATED, response.json
# Download is scheduled. Download is related to Channel.
assert len(download_manager.get_downloads(test_session)) == 1
@ -410,46 +410,47 @@ async def test_get_channel_videos_pagination(test_session, simple_channel, video
@pytest.mark.asyncio
async def test_create_channel_download_api(async_client, test_session, simple_channel, tag_factory):
tag = await tag_factory()
async def test_channel_download_id(async_client, test_session, tag_factory, simple_channel, test_download_manager,
test_downloader):
tag = await tag_factory()
# Create Download.
body = {'url': 'https://example.com/1', 'frequency': 42, 'settings': {'title_include': ''}}
request, response = await async_client.post(
f'/api/videos/channels/{simple_channel.id}/download', json=body)
assert response.status_code == HTTPStatus.NO_CONTENT, response.content
download: Download = test_session.query(Download).one()
assert download.url == 'https://example.com/1' and not download.settings.get('tag_names')
assert download.settings.get('title_include') is None
# A recurring Download can be displayed on a Channel's page.
body = dict(
urls=['https://example.com/channel1'],
tag_names=[tag.name],
downloader=test_downloader.name,
settings=dict(channel_id=simple_channel.id),
frequency=120,
)
request, response = await async_client.post(f'/api/download', json=body)
assert response.status_code == HTTPStatus.CREATED
download = test_session.query(Download).one()
assert download.url == 'https://example.com/channel1'
assert download.channel_id == simple_channel.id
# Create another Download. Previous Downloads is untouched.
body = {'url': 'https://example.com/2', 'frequency': 55, 'settings': {'tag_names': [tag.name]}}
request, response = await async_client.post(
f'/api/videos/channels/{simple_channel.id}/download', json=body)
assert response.status_code == HTTPStatus.NO_CONTENT, response.content
download1, download2 = test_session.query(Download).order_by(Download.url).all()
assert download1.url == 'https://example.com/1' and not download1.settings.get('tag_names')
assert download2.url == 'https://example.com/2' and download2.settings.get('tag_names') == [tag.name, ]
test_session.flush(simple_channel)
assert len(simple_channel.downloads) == 2
# Change frequency of first Download.
body = {'url': 'https://example.com/1', 'frequency': 123, 'settings': {'title_include': 'foo,bar'}}
request, response = await async_client.put(
f'/api/videos/channels/{simple_channel.id}/download/{download1.id}', json=body)
assert response.status_code == HTTPStatus.NO_CONTENT, response.content
download1, download2 = test_session.query(Download).order_by(Download.url).all()
assert download1.url == 'https://example.com/1'
assert download1.settings == {'title_include': 'foo,bar'}
assert download2.url == 'https://example.com/2' and download2.settings.get('tag_names') == [tag.name, ]
test_session.flush(simple_channel)
assert len(simple_channel.downloads) == 2
# Deleting Download does not delete Channel.
request, response = await async_client.delete(f'/api/download/{download1.id}')
# A Channel relationship can be removed from a Download.
body = dict(
urls=['https://example.com/channel1'],
tag_names=[tag.name],
downloader=test_downloader.name,
settings=dict(),
frequency=240,
)
request, response = await async_client.put(f'/api/download/{download.id}', json=body)
assert response.status_code == HTTPStatus.NO_CONTENT
assert test_session.query(Download).count() == 1
assert test_session.query(Channel).count() == 1
download = test_session.query(Download).one()
assert download.channel_id is None
assert download.frequency == 240
# A once-Download cannot be associated with a Channel.
body = dict(
urls=['https://example.com/channel1'],
tag_names=[tag.name],
downloader=test_downloader.name,
settings=dict(channel_id=simple_channel.id),
)
request, response = await async_client.put(f'/api/download/{download.id}', json=body)
assert response.status_code == HTTPStatus.BAD_REQUEST
@pytest.mark.asyncio

View file

@ -151,7 +151,7 @@ def test_api_download(test_session, test_client, test_directory):
content = dict(urls=['https://example.com/video1', ], downloader='video', destination='dest',
settings=dict(excluded_urls='example.com'))
request, response = test_client.post('/api/download', content=json.dumps(content))
assert response.status_code == HTTPStatus.NO_CONTENT
assert response.status_code == HTTPStatus.CREATED
download = test_session.query(Download).one()
assert download.url == 'https://example.com/video1'
@ -167,15 +167,14 @@ async def test_api_download_video_tags(test_session, async_client, tag_factory):
tag1 = await tag_factory()
tag2 = await tag_factory()
content = dict(urls=['https://example.com/video1', ], downloader='video',
settings={'tag_names': [tag1.name, tag2.name]})
content = dict(urls=['https://example.com/video1', ], downloader='video', tag_names=[tag1.name, tag2.name])
request, response = await async_client.post('/api/download', content=json.dumps(content))
assert response.status_code == HTTPStatus.NO_CONTENT
assert response.status_code == HTTPStatus.CREATED
download = test_session.query(Download).one()
assert download.url == 'https://example.com/video1'
assert download.downloader == 'video'
assert download.settings['tag_names'] == [tag1.name, tag2.name]
assert download.tag_names == [tag1.name, tag2.name]
@pytest.mark.asyncio

View file

@ -453,7 +453,7 @@ async def test_download_playlist(test_session, test_directory, mock_video_extrac
@pytest.mark.asyncio
async def test_channel_download_crud(test_session, async_client, assert_downloads, tag_factory):
"""Test creatin"""
"""Test creating more complex Channel Download."""
one, two = await tag_factory(), await tag_factory()
download = {
'urls': ['https://www.youtube.com/@wrolpi/videos'],
@ -476,10 +476,11 @@ async def test_channel_download_crud(test_session, async_client, assert_download
}
request, response = await async_client.post('/api/download', json=download)
assert response.status == HTTPStatus.NO_CONTENT
assert response.status == HTTPStatus.CREATED, request.body
download = test_session.query(Download).one()
assert download.url == 'https://www.youtube.com/@wrolpi/videos'
assert download.destination is None
assert download.tag_names == ['one', 'two']
assert download.downloader == 'video_channel'
assert download.sub_downloader == 'video'

View file

@ -5,7 +5,7 @@ from typing import Optional, List, Dict
@dataclass
class ZimSearchRequest:
search_str: Optional[str] = None
tag_names: List[str] = field(default_factory=lambda: list())
tag_names: List[str] = field(default_factory=list)
offset: Optional[int] = 0
limit: Optional[int] = 10
@ -98,5 +98,5 @@ class OutdatedZims:
@dataclass
class SearchEstimateRequest:
search_str: Optional[str] = None
tag_names: List[str] = field(default_factory=lambda: list())
tag_names: List[str] = field(default_factory=list)
any_tag: bool = False