mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-04 21:59:58 -05:00
* Add Playlists: ordered, mixed-type collections A playlist (Collection kind='playlist') is a manually-curated, ordered list that mixes item types. Squash of the playlists feature branch (24 commits). Data model & API: - CollectionItem is polymorphic via item_kind: 'file' (FileGroup), 'zim' (zim_id + zim_entry), or 'url' (link to anything the WROLPi browser opens, e.g. /map?lat=&lon=&z=). CHECK constraint enforces exactly one kind; per-kind unique constraints; migration a7b1c2d3e4f5. - Endpoints: create playlist; add/remove/reorder items (idempotent adds: 201 new, 200 existing); GET returns the ordered items. All mutations wrol_mode_check'd. Config (source of truth): - playlists.yaml (PlaylistsConfig) stores each playlist's metadata AND ordered items (file by relative path, zim by path+entry, url inline); round-trips a DB wipe. Supports backup import (DB_CONFIG_FILE_NAMES). - The redundant generic CollectionsConfig (collections.yaml) is deleted: channels/domains own their configs, authors/subjects are re-derived. On-disk sync (sync_playlists_directory switch): - Each playlist materializes into the configurable Playlists Directory (playlists_destination, Settings UI input, excluded from indexing) with position-prefixed entries: NNNN_<file> hard links (copy fallback across filesystems) and NNNN_<title>.html stubs for zim/url items. - Tagged playlists are namespaced like channels: <playlists>/<tag>/<name>; a tag change moves the directory and prunes the empty old tag dir. - Deletion safety: managed files are only removed when they are our marked stubs or still have another hard link (st_nlink > 1) -- never a last copy. Stray loose files at the playlists root are removed; stub URLs are scheme-allowlisted (no javascript:/data:). Frontend: - /playlists list page (CollectionTable, name filter, New Playlist modal, Tag column) between Files and Zim in the nav. - View-only detail page (Name link) and edit page (Edit button): themed table with model icons/colors + poster thumbnails, items link to their model page (video/archive/doc) or preview, up/down reorder, remove, add-URL form, single-tag selector; all editing disabled in WROL Mode. - Reusable green "Add to Playlist" button on Video/Archive/Doc detail pages, the FilePreview toolbar, Zim search results, and Map pins (added as a /map URL item with a map-marker icon). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * Playlist edit page parity: rename, custom directory, tag modal move suggestion Match the ChannelEditPage/DomainEditPage UX on the playlist edit page: Back/View buttons, an edit form (name, description, directory), and the shared CollectionTagModal with the violet "Tag" button. The tag modal now suggests moving the playlist to its managed tag location (playlists/<tag>/<name>); accepting the suggestion stores directory=None so the sync keeps auto-managing the location. Tag buttons on the Channel and Domain edit pages are now violet to match. Backend: update_collection learns rename (with per-kind validation and duplicate checks) and playlist custom directories (cleanup of the abandoned directory, config round-trip, commit-before-switch to avoid the background sync racing pre-commit state). tag_collection's duplicated untag/tag branches are merged, and the per-kind config-save dispatch is consolidated into _activate_config_save(). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Deduplicate Collection error handling and Tag creation Delete the duplicate UnknownCollection in wrolpi/collections/errors.py and re-export the canonical one from wrolpi/errors.py, which carries status_code=NOT_FOUND (the local copy inherited 500). Replace the repeated query/raise pattern in reorganize.py with Collection.find_by_id. Add Tag.get_or_create_tag and use it for collection tagging; flush the new Tag before returning so callers can read .id immediately -- previously tagging a collection with a brand-new tag name set tag_id=None. Regression test included. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
617 lines
24 KiB
Python
617 lines
24 KiB
Python
"""
|
|
Collection API Endpoints
|
|
|
|
Unified REST API for all collection types (domains, channels, playlists, etc.).
|
|
"""
|
|
from http import HTTPStatus
|
|
|
|
from sanic import Request, Blueprint, response
|
|
from sanic_ext.extensions.openapi import openapi
|
|
|
|
from wrolpi.api_utils import json_response
|
|
from wrolpi.common import wrol_mode_check
|
|
from wrolpi.errors import ValidationError
|
|
from wrolpi.schema import JSONErrorResponse
|
|
from . import lib, schema
|
|
from .errors import UnknownCollection
|
|
|
|
# Create blueprint
|
|
collection_bp = Blueprint('Collection', url_prefix='/api/collections')
|
|
|
|
|
|
@collection_bp.get('/')
|
|
@openapi.summary('Get all collections')
|
|
@openapi.parameter('kind', str, 'query', description='Filter by collection kind (e.g., domain, channel)',
|
|
required=False)
|
|
@openapi.response(HTTPStatus.OK, description="List of collections with metadata")
|
|
async def get_collections_endpoint(request: Request):
|
|
"""
|
|
Get all collections, optionally filtered by kind.
|
|
|
|
This unified endpoint works for all collection types. Use the 'kind' query parameter
|
|
to filter by collection type (e.g., ?kind=domain or ?kind=channel).
|
|
|
|
Examples:
|
|
GET /api/collections - Get all collections
|
|
GET /api/collections?kind=domain - Get only domain collections
|
|
GET /api/collections?kind=channel - Get only channel collections
|
|
|
|
Returns:
|
|
- collections: List of collection objects
|
|
- totals: Count of collections
|
|
- metadata: UI metadata (columns, fields, routes, messages) if kind is specified
|
|
"""
|
|
kind = request.args.get('kind')
|
|
session = request.ctx.session
|
|
|
|
collections = lib.get_collections(session, kind=kind)
|
|
|
|
response_data = {
|
|
'collections': collections,
|
|
'totals': {'collections': len(collections)}
|
|
}
|
|
|
|
return json_response(response_data)
|
|
|
|
|
|
@collection_bp.post('/')
|
|
@openapi.definition(
|
|
summary='Create a collection (defaults to a playlist)',
|
|
body=schema.CollectionCreateRequest,
|
|
validate=True,
|
|
)
|
|
@openapi.response(HTTPStatus.CREATED, description="Collection created")
|
|
@openapi.response(HTTPStatus.BAD_REQUEST, JSONErrorResponse)
|
|
@wrol_mode_check
|
|
async def create_collection_endpoint(request: Request, body: schema.CollectionCreateRequest):
|
|
"""Create a new Collection. Defaults to kind='playlist' (a manually-curated, ordered list)."""
|
|
session = request.ctx.session
|
|
try:
|
|
collection = lib.create_collection(
|
|
session, name=body.name, description=body.description, kind=body.kind or 'playlist')
|
|
except ValidationError as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.BAD_REQUEST)
|
|
|
|
data = lib.get_collection_with_stats(session, collection.id)
|
|
return json_response({'collection': data}, status=HTTPStatus.CREATED)
|
|
|
|
|
|
@collection_bp.post('/<collection_id:int>/items')
|
|
@openapi.definition(
|
|
summary='Add an item (file, zim, or url) to a collection',
|
|
body=schema.AddItemRequest,
|
|
validate=True,
|
|
)
|
|
@openapi.response(HTTPStatus.CREATED, description="Item added")
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
@openapi.response(HTTPStatus.BAD_REQUEST, JSONErrorResponse)
|
|
@wrol_mode_check
|
|
async def add_collection_item_endpoint(request: Request, collection_id: int, body: schema.AddItemRequest):
|
|
"""Append (or insert at ``position``) a file/zim/url item into the collection."""
|
|
session = request.ctx.session
|
|
try:
|
|
item, created = lib.add_collection_item(
|
|
session, collection_id, item_kind=body.item_kind,
|
|
file_group_id=body.file_group_id, zim_id=body.zim_id, zim_entry=body.zim_entry,
|
|
url=body.url, title=body.title, position=body.position)
|
|
except UnknownCollection as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.NOT_FOUND)
|
|
except ValidationError as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.BAD_REQUEST)
|
|
|
|
# 201 when a new item was created; 200 when the item already existed (idempotent add).
|
|
status = HTTPStatus.CREATED if created else HTTPStatus.OK
|
|
return json_response({'item': item.dict()}, status=status)
|
|
|
|
|
|
@collection_bp.delete('/<collection_id:int>/items/<item_id:int>')
|
|
@openapi.summary('Remove an item from a collection')
|
|
@openapi.response(HTTPStatus.NO_CONTENT, description="Item removed")
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
@wrol_mode_check
|
|
async def remove_collection_item_endpoint(request: Request, collection_id: int, item_id: int):
|
|
"""Remove a single item by its id and close the ordering gap."""
|
|
session = request.ctx.session
|
|
try:
|
|
removed = lib.remove_collection_item(session, collection_id, item_id)
|
|
except UnknownCollection as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.NOT_FOUND)
|
|
|
|
if not removed:
|
|
return json_response({'error': f'Item {item_id} not found in collection {collection_id}'},
|
|
status=HTTPStatus.NOT_FOUND)
|
|
return response.raw('', HTTPStatus.NO_CONTENT)
|
|
|
|
|
|
@collection_bp.put('/<collection_id:int>/items/order')
|
|
@openapi.definition(
|
|
summary='Reorder a collection\'s items',
|
|
body=schema.ReorderItemsRequest,
|
|
validate=True,
|
|
)
|
|
@openapi.response(HTTPStatus.OK, description="Items reordered")
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
@openapi.response(HTTPStatus.BAD_REQUEST, JSONErrorResponse)
|
|
@wrol_mode_check
|
|
async def reorder_collection_items_endpoint(request: Request, collection_id: int,
|
|
body: schema.ReorderItemsRequest):
|
|
"""Set the item order from a full list of item ids (used by drag-and-drop)."""
|
|
session = request.ctx.session
|
|
try:
|
|
lib.reorder_collection_items(session, collection_id, body.item_ids)
|
|
except UnknownCollection as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.NOT_FOUND)
|
|
except ValueError as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.BAD_REQUEST)
|
|
|
|
data = lib.get_collection_with_stats(session, collection_id)
|
|
return json_response({'collection': data})
|
|
|
|
|
|
@collection_bp.get('/<collection_id:int>')
|
|
@openapi.summary('Get a single collection by ID')
|
|
@openapi.response(HTTPStatus.OK, description="Collection details with statistics")
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
async def get_collection_endpoint(request: Request, collection_id: int):
|
|
"""
|
|
Get details for a single collection including type-specific statistics.
|
|
|
|
Returns collection metadata plus statistics specific to the collection type:
|
|
- Domain collections: includes url_count and size
|
|
- Channel collections: includes video_count and size
|
|
- Other types: includes item_count and total_size
|
|
"""
|
|
session = request.ctx.session
|
|
try:
|
|
collection_data = lib.get_collection_with_stats(session, collection_id)
|
|
return json_response({'collection': collection_data})
|
|
except UnknownCollection as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.NOT_FOUND)
|
|
|
|
|
|
@collection_bp.put('/<collection_id:int>')
|
|
@openapi.definition(
|
|
summary='Update a collection',
|
|
body=schema.CollectionUpdateRequest,
|
|
validate=True,
|
|
)
|
|
@openapi.response(HTTPStatus.OK, description="Collection updated successfully")
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
@openapi.response(HTTPStatus.BAD_REQUEST, JSONErrorResponse)
|
|
@wrol_mode_check
|
|
async def put_collection_endpoint(request: Request, collection_id: int, body: schema.CollectionUpdateRequest):
|
|
"""
|
|
Update collection properties (directory, tag, description).
|
|
|
|
This endpoint works for all collection types. You can update:
|
|
- directory: Set or clear the collection's directory restriction
|
|
- tag_name: Set or clear the collection's tag (requires directory)
|
|
- description: Set or update the collection's description
|
|
|
|
Note: To clear a field, pass an empty string. To leave unchanged, omit the field.
|
|
"""
|
|
session = request.ctx.session
|
|
try:
|
|
collection = lib.update_collection(
|
|
session,
|
|
collection_id=collection_id,
|
|
directory=body.directory,
|
|
tag_name=body.tag_name,
|
|
description=body.description,
|
|
name=body.name,
|
|
)
|
|
|
|
# Return updated collection data
|
|
collection_data = lib.get_collection_with_stats(session, collection_id)
|
|
return json_response({'collection': collection_data})
|
|
|
|
except UnknownCollection as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.NOT_FOUND)
|
|
except Exception as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.BAD_REQUEST)
|
|
|
|
|
|
@collection_bp.post('/<collection_id:int>/refresh')
|
|
@openapi.summary('Refresh files in collection directory')
|
|
@openapi.response(HTTPStatus.OK, description="Collection refresh started")
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
@openapi.response(HTTPStatus.BAD_REQUEST, JSONErrorResponse)
|
|
@wrol_mode_check
|
|
async def refresh_collection_endpoint(_: Request, collection_id: int):
|
|
"""
|
|
Queue a refresh for a collection's directory.
|
|
|
|
This scans the collection's directory for new or modified files and updates
|
|
the database accordingly. Only works for directory-restricted collections.
|
|
|
|
The refresh happens asynchronously in the background.
|
|
"""
|
|
try:
|
|
lib.refresh_collection(collection_id, send_events=True)
|
|
return json_response({'message': 'Collection refresh started'})
|
|
|
|
except UnknownCollection as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.NOT_FOUND)
|
|
except Exception as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.BAD_REQUEST)
|
|
|
|
|
|
@collection_bp.post('/<collection_id:int>/tag')
|
|
@openapi.definition(
|
|
summary='Tag a collection and optionally move files',
|
|
body=schema.CollectionTagRequest,
|
|
validate=True,
|
|
)
|
|
@openapi.response(HTTPStatus.OK, schema.CollectionTagResponse, description="Collection tagged successfully")
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
@openapi.response(HTTPStatus.BAD_REQUEST, JSONErrorResponse)
|
|
@wrol_mode_check
|
|
async def tag_collection_endpoint(request: Request, collection_id: int, body: schema.CollectionTagRequest):
|
|
"""
|
|
Tag a collection and optionally move its files to a new directory.
|
|
|
|
Tagging a collection:
|
|
1. Creates or assigns the specified tag
|
|
2. Optionally moves the collection to a new directory
|
|
3. Updates the collection's metadata
|
|
|
|
If no directory is specified, the collection must already have one.
|
|
"""
|
|
session = request.ctx.session
|
|
try:
|
|
result = await lib.tag_collection(
|
|
session,
|
|
collection_id=collection_id,
|
|
tag_name=body.tag_name,
|
|
directory=body.directory
|
|
)
|
|
return json_response(result)
|
|
|
|
except UnknownCollection as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.NOT_FOUND)
|
|
except Exception as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.BAD_REQUEST)
|
|
|
|
|
|
@collection_bp.post('/<collection_id:int>/tag_info')
|
|
@openapi.definition(
|
|
summary='Get tag information for a collection',
|
|
body=schema.CollectionTagInfoRequest,
|
|
validate=True,
|
|
)
|
|
@openapi.response(HTTPStatus.OK, schema.CollectionTagInfoResponse, description="Tag info retrieved successfully")
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
async def get_tag_info_endpoint(request: Request, collection_id: int, body: schema.CollectionTagInfoRequest):
|
|
"""
|
|
Get information about tagging a collection with a specific tag.
|
|
|
|
Returns the suggested directory path and checks for conflicts with existing collections.
|
|
Domain collections cannot share directories with other domain collections,
|
|
but can share with channel collections.
|
|
|
|
This is useful for showing users the suggested directory before they commit to tagging.
|
|
"""
|
|
session = request.ctx.session
|
|
try:
|
|
tag_info = lib.get_tag_info(
|
|
session,
|
|
collection_id=collection_id,
|
|
tag_name=body.tag_name
|
|
)
|
|
return json_response(tag_info)
|
|
|
|
except UnknownCollection as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.NOT_FOUND)
|
|
|
|
|
|
@collection_bp.delete('/<collection_id:int>')
|
|
@openapi.definition(
|
|
summary='Delete a collection',
|
|
)
|
|
@openapi.response(HTTPStatus.NO_CONTENT, description="Collection deleted successfully")
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
@wrol_mode_check
|
|
async def delete_collection_endpoint(request: Request, collection_id: int):
|
|
"""
|
|
Delete a collection and orphan its child items.
|
|
|
|
For domain collections:
|
|
- Orphans child Archives (sets collection_id to NULL)
|
|
- Deletes the Collection record
|
|
- Archives remain in the database but are no longer associated with a domain
|
|
- Triggers domain config save
|
|
"""
|
|
session = request.ctx.session
|
|
try:
|
|
collection = lib.delete_collection(session, collection_id=collection_id)
|
|
from wrolpi.events import Events
|
|
Events.send_deleted(f'Deleted {collection["kind"]} collection: {collection["name"]}')
|
|
return response.raw('', HTTPStatus.NO_CONTENT)
|
|
|
|
except UnknownCollection as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.NOT_FOUND)
|
|
|
|
|
|
@collection_bp.post('/search')
|
|
@openapi.definition(
|
|
summary='Search collections',
|
|
body=schema.CollectionSearchRequest,
|
|
validate=True,
|
|
)
|
|
@openapi.response(HTTPStatus.OK, description="Search results")
|
|
async def search_collections_endpoint(request: Request, body: schema.CollectionSearchRequest):
|
|
"""
|
|
Search collections by kind, tags, and name.
|
|
|
|
Supports filtering by:
|
|
- kind: Collection type (domain, channel, etc.)
|
|
- tag_names: List of tag names (returns collections with any of these tags)
|
|
- search_str: Search string for collection names (case-insensitive partial match)
|
|
|
|
All filters are optional and can be combined.
|
|
"""
|
|
session = request.ctx.session
|
|
collections = lib.search_collections(
|
|
session,
|
|
kind=body.kind,
|
|
tag_names=body.tag_names if body.tag_names else None,
|
|
search_str=body.search_str
|
|
)
|
|
|
|
return json_response({
|
|
'collections': collections,
|
|
'totals': {'collections': len(collections)}
|
|
})
|
|
|
|
|
|
@collection_bp.get('/<collection_id:int>/reorganize/preview')
|
|
@openapi.summary('Preview collection reorganization')
|
|
@openapi.response(HTTPStatus.OK, schema.ReorganizationPreviewResponse, description="Reorganization preview")
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
@openapi.response(HTTPStatus.BAD_REQUEST, JSONErrorResponse)
|
|
async def preview_reorganization_endpoint(request: Request, collection_id: int):
|
|
"""
|
|
Get preview with sample of files to be renamed and conflict detection.
|
|
|
|
Returns a preview of what would happen if the collection is reorganized,
|
|
including any destination conflicts that need to be resolved before
|
|
reorganization can proceed.
|
|
|
|
Returns:
|
|
- collection_id: The collection ID
|
|
- collection_name: The collection name
|
|
- total_files: Total FileGroups in collection
|
|
- files_needing_move: Count needing reorganization
|
|
- sample_moves: ~10 example moves [{old_path, new_path}, ...]
|
|
- new_file_format: The format template that will be applied
|
|
- current_file_format: The format currently stored on collection
|
|
- conflicts: List of destination conflicts with file details
|
|
- has_conflicts: Boolean flag indicating if conflicts exist
|
|
"""
|
|
from wrolpi.collections.reorganize import get_reorganization_preview
|
|
|
|
session = request.ctx.session
|
|
try:
|
|
preview = get_reorganization_preview(collection_id, session)
|
|
return json_response({
|
|
'collection_id': preview.collection_id,
|
|
'collection_name': preview.collection_name,
|
|
'total_files': preview.total_files,
|
|
'files_needing_move': preview.files_needing_move,
|
|
'sample_moves': preview.sample_moves,
|
|
'new_file_format': preview.new_file_format,
|
|
'current_file_format': preview.current_file_format,
|
|
'conflicts': preview.conflicts or [],
|
|
'has_conflicts': preview.has_conflicts,
|
|
})
|
|
except UnknownCollection as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.NOT_FOUND)
|
|
except ValueError as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.BAD_REQUEST)
|
|
|
|
|
|
@collection_bp.get('/<collection_id:int>/reorganize/conflicts')
|
|
@openapi.summary('Get conflict details with ffprobe processing')
|
|
@openapi.response(HTTPStatus.OK, description="Conflict details for resolution UI")
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
@openapi.response(HTTPStatus.BAD_REQUEST, JSONErrorResponse)
|
|
async def get_conflicts_endpoint(request: Request, collection_id: int):
|
|
"""
|
|
Get detailed conflict information for resolution UI.
|
|
|
|
Processes ffprobe for any videos missing it to ensure accurate
|
|
quality rankings (including audio stream bonus).
|
|
|
|
Returns:
|
|
- conflicts: List of conflict details with updated quality rankings
|
|
"""
|
|
from wrolpi.collections.reorganize import get_conflict_details_with_ffprobe
|
|
|
|
session = request.ctx.session
|
|
try:
|
|
conflicts = await get_conflict_details_with_ffprobe(collection_id, session)
|
|
session.commit()
|
|
return json_response({'conflicts': conflicts})
|
|
except UnknownCollection as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.NOT_FOUND)
|
|
except ValueError as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.BAD_REQUEST)
|
|
|
|
|
|
@collection_bp.post('/<collection_id:int>/reorganize')
|
|
@openapi.summary('Execute collection reorganization')
|
|
@openapi.response(HTTPStatus.OK, schema.ReorganizationExecuteResponse, description="Reorganization started")
|
|
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
|
|
@openapi.response(HTTPStatus.BAD_REQUEST, JSONErrorResponse)
|
|
@wrol_mode_check
|
|
async def execute_reorganization_endpoint(request: Request, collection_id: int):
|
|
"""
|
|
Execute reorganization.
|
|
|
|
Moves files to match the current file_name_format configuration.
|
|
The operation runs in the background via FileWorker.
|
|
|
|
Returns:
|
|
- job_id: For tracking progress via the status endpoint
|
|
- message: Status message
|
|
"""
|
|
from wrolpi.collections.reorganize import execute_reorganization
|
|
|
|
session = request.ctx.session
|
|
try:
|
|
job_id = execute_reorganization(collection_id, session)
|
|
if job_id:
|
|
return json_response({
|
|
'job_id': job_id,
|
|
'message': 'Reorganization started'
|
|
})
|
|
else:
|
|
return json_response({
|
|
'job_id': '',
|
|
'message': 'No files need reorganization'
|
|
})
|
|
except UnknownCollection as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.NOT_FOUND)
|
|
except ValueError as e:
|
|
return json_response({'error': str(e)}, status=HTTPStatus.BAD_REQUEST)
|
|
|
|
|
|
@collection_bp.get('/<collection_id:int>/reorganize/status/<job_id:str>')
|
|
@openapi.summary('Get reorganization status')
|
|
@openapi.response(HTTPStatus.OK, schema.ReorganizationStatusResponse, description="Reorganization status")
|
|
async def get_reorganization_status_endpoint(_: Request, collection_id: int, job_id: str):
|
|
"""
|
|
Get status of in-flight reorganization.
|
|
|
|
Returns:
|
|
- status: 'pending' | 'running' | 'complete' | 'failed'
|
|
- total: Total files to move
|
|
- completed: Files moved so far
|
|
- percent: Completion percentage
|
|
- error: Error message if failed
|
|
"""
|
|
from wrolpi.collections.reorganize import get_reorganization_status
|
|
|
|
status = get_reorganization_status(job_id)
|
|
return json_response(status)
|
|
|
|
|
|
# ============================================================================
|
|
# Batch Reorganization Endpoints
|
|
# ============================================================================
|
|
|
|
|
|
@collection_bp.get('/reorganize/channels')
|
|
@openapi.summary('List channels needing batch reorganization')
|
|
@openapi.response(HTTPStatus.OK, schema.BatchReorganizationListResponse,
|
|
description="List of channels needing reorganization")
|
|
async def list_channels_needing_reorganization_endpoint(request: Request):
|
|
"""
|
|
List all channels that need reorganization because their file_name_format
|
|
differs from the current config.
|
|
|
|
Returns:
|
|
- collections: List of channel info with sample moves
|
|
- total_collections: Total count needing reorganization
|
|
- total_files_needing_move: Sum of all files needing reorganization
|
|
- new_file_format: The config format that will be applied
|
|
"""
|
|
from wrolpi.collections.reorganize import get_collections_needing_reorganization
|
|
|
|
session = request.ctx.session
|
|
result = get_collections_needing_reorganization('channel', session)
|
|
return json_response(result)
|
|
|
|
|
|
@collection_bp.get('/reorganize/domains')
|
|
@openapi.summary('List domains needing batch reorganization')
|
|
@openapi.response(HTTPStatus.OK, schema.BatchReorganizationListResponse,
|
|
description="List of domains needing reorganization")
|
|
async def list_domains_needing_reorganization_endpoint(request: Request):
|
|
"""
|
|
List all domains that need reorganization because their file_name_format
|
|
differs from the current config.
|
|
|
|
Returns:
|
|
- collections: List of domain info with sample moves
|
|
- total_collections: Total count needing reorganization
|
|
- total_files_needing_move: Sum of all files needing reorganization
|
|
- new_file_format: The config format that will be applied
|
|
"""
|
|
from wrolpi.collections.reorganize import get_collections_needing_reorganization
|
|
|
|
session = request.ctx.session
|
|
result = get_collections_needing_reorganization('domain', session)
|
|
return json_response(result)
|
|
|
|
|
|
@collection_bp.post('/reorganize/channels')
|
|
@openapi.summary('Execute batch reorganization of all channels')
|
|
@openapi.response(HTTPStatus.OK, schema.BatchReorganizationExecuteResponse,
|
|
description="Batch reorganization started")
|
|
@wrol_mode_check
|
|
async def execute_batch_reorganization_channels_endpoint(request: Request):
|
|
"""
|
|
Execute batch reorganization of all channels that need it.
|
|
|
|
Processes channels sequentially. If any channel fails, the batch stops
|
|
and reports which channel failed.
|
|
|
|
Returns:
|
|
- batch_job_id: For tracking progress via the batch status endpoint
|
|
- message: Status message
|
|
- collection_count: Number of channels to process
|
|
"""
|
|
from wrolpi.collections.reorganize import execute_batch_reorganization
|
|
|
|
session = request.ctx.session
|
|
result = execute_batch_reorganization('channel', session)
|
|
return json_response(result)
|
|
|
|
|
|
@collection_bp.post('/reorganize/domains')
|
|
@openapi.summary('Execute batch reorganization of all domains')
|
|
@openapi.response(HTTPStatus.OK, schema.BatchReorganizationExecuteResponse,
|
|
description="Batch reorganization started")
|
|
@wrol_mode_check
|
|
async def execute_batch_reorganization_domains_endpoint(request: Request):
|
|
"""
|
|
Execute batch reorganization of all domains that need it.
|
|
|
|
Processes domains sequentially. If any domain fails, the batch stops
|
|
and reports which domain failed.
|
|
|
|
Returns:
|
|
- batch_job_id: For tracking progress via the batch status endpoint
|
|
- message: Status message
|
|
- collection_count: Number of domains to process
|
|
"""
|
|
from wrolpi.collections.reorganize import execute_batch_reorganization
|
|
|
|
session = request.ctx.session
|
|
result = execute_batch_reorganization('domain', session)
|
|
return json_response(result)
|
|
|
|
|
|
@collection_bp.get('/reorganize/batch/status/<batch_job_id:str>')
|
|
@openapi.summary('Get batch reorganization status')
|
|
@openapi.response(HTTPStatus.OK, schema.BatchReorganizationStatusResponse,
|
|
description="Batch reorganization status")
|
|
async def get_batch_reorganization_status_endpoint(_: Request, batch_job_id: str):
|
|
"""
|
|
Get status of a batch reorganization job.
|
|
|
|
Returns:
|
|
- status: 'pending' | 'running' | 'complete' | 'failed' | 'unknown'
|
|
- total_collections: Total collections in batch
|
|
- completed_collections: Number completed so far
|
|
- current_collection: Info about currently processing collection
|
|
- overall_percent: Overall completion percentage
|
|
- completed: List of completed collection info
|
|
- failed_collection: Info about failed collection (if any)
|
|
- error: Error message if failed
|
|
"""
|
|
from wrolpi.collections.reorganize import get_batch_reorganization_status
|
|
|
|
status = get_batch_reorganization_status(batch_job_id)
|
|
return json_response(status)
|