mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-10-26 19:01:39 -04:00
* db: request-path library functions use the request's connection Library functions an API request reaches now take the request's Session (first argument) instead of opening their own session or cursor, so a request needs one SQLite connection. Raw SQL runs through the new `wrolpi.db.session_curs(session)`, a cursor on the Session's connection; `RequestDB.curs()` uses it. - Search: files, videos, archives, flasher, and docs search share the request connection for the total, the page of ids, and the page's models (3 connections -> 1). - Zim search, including tag-filtered search and estimates (2 -> 1). - Channel detail (`Channel.get_statistics` reads on the Channel's own Session) and the channel list. - Video detail, edit, description, captions, comments and transcode. `get_video_for_app` and `update_video` no longer commit; their handlers use `request.ctx.db.write()`, so editing a Video and reading it back is one transaction. - Tags (list, overlaps, recent), file statistics and estimates, other estimates, bulk tag preview, the download list, retry, kill and the status summary, and docs/videos/archive statistics. Not converted: file operations that refresh files inside the request (delete, rename, upload, channel and collection tag/move) commit page by page in code shared with the file worker; the cached zim entry lookup; config import. The connection-count tests now assert one connection for every endpoint they cover (no xfails left), and cover the videos and archive statistics endpoints, which had no API test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * downloads: startup retries in a write session of its own start_single_tasks still called retry_downloads() without a Session after it began requiring one, so any start after a completed refresh raised TypeError there: incomplete downloads were not reset and the download_on_startup enable after it never ran. Startup is not a request, so DownloadManager.retry_downloads_on_startup() opens and commits its own short write transaction. Tested through the method, not start_single_tasks: importing main registers its startup listeners on the shared app, and every later test in that worker then runs them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
152 lines
5.6 KiB
Python
152 lines
5.6 KiB
Python
import asyncio
|
|
import pathlib
|
|
from http import HTTPStatus
|
|
|
|
from sanic import response, Blueprint
|
|
from sanic.request import Request
|
|
from sanic_ext import validate
|
|
from sanic_ext.extensions.openapi import openapi
|
|
|
|
from wrolpi.api_utils import json_response
|
|
from wrolpi.common import logger, wrol_mode_check, get_media_directory, get_relative_to_media_directory
|
|
from wrolpi.events import Events
|
|
from wrolpi.schema import JSONErrorResponse
|
|
from wrolpi.vars import PYTEST
|
|
from . import lib
|
|
from .. import schema
|
|
from ..errors import UnknownChannel
|
|
from ..lib import save_channels_config
|
|
from ..models import Channel
|
|
|
|
channel_bp = Blueprint('Channel', url_prefix='/api/videos/channels')
|
|
|
|
logger = logger.getChild(__name__)
|
|
|
|
|
|
@channel_bp.get('/')
|
|
@openapi.description('Get a list of all Channels')
|
|
@openapi.response(HTTPStatus.OK, schema.ChannelsResponse)
|
|
async def get_channels(request: Request):
|
|
channels = await lib.get_minimal_channels(request.ctx.session)
|
|
return json_response({'channels': channels})
|
|
|
|
|
|
@channel_bp.get('/<channel_id:int>')
|
|
@openapi.description('Get a Channel')
|
|
@openapi.response(HTTPStatus.OK, schema.ChannelResponse)
|
|
def channel_get(request: Request, channel_id: int = None):
|
|
session = request.ctx.session
|
|
channel = lib.get_channel(session, channel_id=channel_id, return_dict=True)
|
|
channel.pop('info_json')
|
|
return json_response({'channel': channel})
|
|
|
|
|
|
@channel_bp.post('/')
|
|
@openapi.definition(
|
|
description='Insert a Channel',
|
|
body=schema.ChannelPostRequest,
|
|
)
|
|
@openapi.response(HTTPStatus.OK, schema.ChannelPostResponse)
|
|
@openapi.response(HTTPStatus.BAD_REQUEST, JSONErrorResponse)
|
|
@validate(schema.ChannelPostRequest)
|
|
@wrol_mode_check
|
|
def channel_post(request: Request, body: schema.ChannelPostRequest):
|
|
body.directory = get_media_directory() / body.directory
|
|
|
|
# A write block: creating a Channel reads (the URL/name/directory conflict check) before it
|
|
# INSERTs, and a deferred transaction fails that lock upgrade *instantly* with "database is
|
|
# locked" if anything else wrote in between -- which is what a user creating a Channel while
|
|
# background workers saved configs hit. The block also commits before the refresh below is
|
|
# fired, so the refresh does not wait on a lock this request still holds.
|
|
with request.ctx.db.write() as session:
|
|
channel = lib.create_channel(session, data=body, return_dict=False)
|
|
channel_id, channel_name = channel.id, channel.name
|
|
|
|
# Refresh the videos asynchronously
|
|
Channel.refresh_files(channel_id)
|
|
|
|
Events.send_created(f'Created Channel: {channel_name}')
|
|
|
|
return response.json({'success': 'Channel created successfully'}, HTTPStatus.CREATED,
|
|
{'Location': f'/api/videos/channels/{channel_id}'})
|
|
|
|
|
|
@channel_bp.put('/<channel_id:int>')
|
|
@openapi.definition(
|
|
description='Update a Channel',
|
|
body=schema.ChannelPutRequest,
|
|
)
|
|
@openapi.response(HTTPStatus.NO_CONTENT)
|
|
@openapi.response(HTTPStatus.BAD_REQUEST, JSONErrorResponse)
|
|
@validate(schema.ChannelPutRequest)
|
|
@wrol_mode_check
|
|
async def channel_update(request: Request, channel_id: int, body: schema.ChannelPutRequest):
|
|
session = request.ctx.session
|
|
body.directory = pathlib.Path(body.directory) if body.directory else None
|
|
channel = await lib.update_channel(session, data=body, channel_id=channel_id)
|
|
save_channels_config.activate_switch()
|
|
return response.raw('', HTTPStatus.NO_CONTENT,
|
|
headers={'Location': f'/api/videos/channels/{channel.id}'})
|
|
|
|
|
|
@channel_bp.delete('/<channel_id:int>')
|
|
@openapi.description('Delete a Channel')
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
@wrol_mode_check
|
|
def channel_delete(request: Request, channel_id: int):
|
|
session = request.ctx.session
|
|
channel = lib.delete_channel(session, channel_id=channel_id)
|
|
Events.send_deleted(f'Deleted Channel: {channel["name"]}')
|
|
return response.raw('', HTTPStatus.NO_CONTENT)
|
|
|
|
|
|
@channel_bp.post('/refresh/<channel_id:int>')
|
|
@openapi.description('Refresh all files in the Channel directory')
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
@wrol_mode_check
|
|
def channel_refresh(request: Request, channel_id: int):
|
|
session = request.ctx.session
|
|
channel: Channel = lib.get_channel(session, channel_id=channel_id, return_dict=False)
|
|
if not channel:
|
|
raise UnknownChannel()
|
|
|
|
channel.refresh_files(channel.id)
|
|
directory = get_relative_to_media_directory(channel.directory)
|
|
Events.send_directory_refresh(f'Refreshing: {directory}')
|
|
return response.empty()
|
|
|
|
|
|
@channel_bp.post('/<channel_id:int>/tag')
|
|
@openapi.definition(
|
|
description='Tag/untag a Channel with a single tag',
|
|
body=schema.ChannelTagRequest,
|
|
)
|
|
@openapi.response(HTTPStatus.NO_CONTENT)
|
|
@openapi.response(HTTPStatus.BAD_REQUEST, JSONErrorResponse)
|
|
@validate(schema.ChannelTagRequest)
|
|
@wrol_mode_check
|
|
async def channel_tag(request: Request, channel_id: int, body: schema.ChannelTagRequest):
|
|
session = request.ctx.session
|
|
directory = None
|
|
if body.directory:
|
|
directory = pathlib.Path(body.directory)
|
|
directory = get_media_directory() / directory
|
|
|
|
await lib.tag_channel(session, body.tag_name, directory, channel_id)
|
|
|
|
return response.raw('', HTTPStatus.NO_CONTENT)
|
|
|
|
|
|
@channel_bp.post('/search')
|
|
@openapi.definition(
|
|
description='Search Channels',
|
|
body=schema.ChannelSearchRequest,
|
|
)
|
|
@validate(schema.ChannelSearchRequest)
|
|
async def channel_search(request: Request, body: schema.ChannelSearchRequest):
|
|
session = request.ctx.session
|
|
channels = await lib.search_channels(session, body.tag_names)
|
|
ret = dict(
|
|
channels=channels,
|
|
)
|
|
return json_response(ret)
|