wrolpi/wrolpi/collections/models.py
lrnselfreliance 3ea8eb81f6
Model reprs read only loaded state (#819)
A repr runs inside log calls. Reading a relationship or an expired column there runs a query, and
on an object whose session is gone it raises DetachedInstanceError instead of logging. Archive's
repr also stat'ed its SingleFile file on disk.

`wrolpi.common.loaded()` follows attributes through state that is already loaded (primary keys
from the identity key) and never runs SQL. Every model repr uses it; with everything loaded the
output is unchanged, except Video and Archive now show the FileGroup's primary path.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 16:47:32 -06:00

1312 lines
53 KiB
Python

import asyncio
import errno
import os
import pathlib
import shutil
from typing import Optional, List
from sqlalchemy import Column, Integer, String, ForeignKey, Text, DateTime, Index, UniqueConstraint, func, BigInteger, \
or_, CheckConstraint
from sqlalchemy.orm import Mapped, relationship, Session, joinedload, selectinload
from wrolpi import flags
from wrolpi.common import loaded, Base, ModelHelper, logger, get_media_directory, get_relative_to_media_directory, \
unique_by_predicate, TRACE_LEVEL
from wrolpi.db import serializer
from wrolpi.downloader import Download, save_downloads_config
from wrolpi.errors import ValidationError
from wrolpi.events import Events
from wrolpi.files.lib import delete_directory, remove_files_in_ignored_directories
from wrolpi.files.models import FileGroup
from wrolpi.files.worker import file_worker
from wrolpi.media_path import MediaPathType
from wrolpi.tags import Tag
from wrolpi.tags import save_tags_config
from .errors import UnknownCollection
from .types import collection_type_registry
logger = logger.getChild(__name__)
__all__ = ['Collection', 'CollectionItem']
def validate_collection_directory(directory: pathlib.Path) -> pathlib.Path:
"""Validate and normalize a collection directory path.
Args:
directory: Directory path (relative or absolute)
Returns:
Normalized absolute path under media directory
Raises:
ValidationError: If absolute path is outside media directory, or the path contains ".."
"""
media_directory = get_media_directory()
directory = pathlib.Path(directory)
if '..' in directory.parts:
# Would leave the media directory.
raise ValidationError(f'Collection directory cannot contain "..": {directory}')
if not directory.is_absolute():
# Relative path - make absolute under media directory
directory = media_directory / directory
else:
# Absolute path - must be under media directory
try:
directory.relative_to(media_directory)
except ValueError:
raise ValidationError(
f"Collection directory must be under media directory {media_directory}, "
f"but got {directory}"
)
return directory
def _move_directory(source: pathlib.Path, destination: pathlib.Path):
"""Move `source` to `destination`, which must not exist, or be an empty directory."""
if destination.is_dir() and not destination.is_symlink():
destination.rmdir() # Raises if it is not empty.
elif destination.exists() or destination.is_symlink():
raise FileExistsError(f'Cannot move: {destination} already exists')
try:
os.rename(source, destination)
except OSError as e:
if e.errno != errno.EXDEV:
raise
# Another filesystem.
shutil.move(source, destination)
class Collection(ModelHelper, Base):
"""
A Collection is a grouping of FileGroups (videos, archives, docs, etc).
Collections can be:
- Directory-restricted: Only contains files within a specific directory tree
- Unrestricted: Contains files from anywhere in the media library
Collections maintain order through the CollectionItem junction table.
"""
__tablename__ = 'collection'
__table_args__ = (
UniqueConstraint('directory', name='uq_collection_directory'),
UniqueConstraint('name', 'kind', name='uq_collection_name_kind'),
Index('idx_collection_kind', 'kind'),
Index('idx_collection_item_count', 'item_count'),
Index('idx_collection_total_size', 'total_size'),
)
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
description = Column(Text)
# Type of collection to keep separation of kinds (e.g., videos, archives, books, etc.)
kind = Column(String, nullable=False, server_default='channel', default='channel')
# Optional directory restriction - if set, only files in this directory tree can be added
directory: Mapped[Optional[pathlib.Path]] = Column(MediaPathType, nullable=True)
# Optional tag relationship (similar to Channel)
tag_id = Column(Integer, ForeignKey('tag.id'))
# Joined: one narrow row, read by the Collection's (and its Channel's) `tag_name`.
tag = relationship('Tag', primaryjoin='Collection.tag_id==Tag.id', lazy='joined')
# Stores the file_name_format used when files were last organized
# Used to detect when reorganization is needed after config changes
file_format = Column(String, nullable=True)
created_date = Column(DateTime, server_default=func.now(), nullable=False)
# Columns updated by triggers (similar to Channel)
item_count = Column(Integer, default=0, nullable=False)
total_size = Column(BigInteger, default=0, nullable=False)
# Relationship to items (ordered)
items: Mapped[List['CollectionItem']] = relationship(
'CollectionItem',
primaryjoin='Collection.id==CollectionItem.collection_id',
back_populates='collection',
order_by='CollectionItem.position',
cascade='all, delete-orphan'
)
# Relationship to downloads
downloads: Mapped[List['Download']] = relationship(
'Download',
primaryjoin='Download.collection_id==Collection.id',
back_populates='collection',
)
def __repr__(self):
return f'<Collection id={loaded(self, "id")} name={repr(loaded(self, "name"))} ' \
f'directory={loaded(self, "directory")} items={loaded(self, "item_count")}>'
@property
def tag_name(self) -> str | None:
return self.tag.name if self.tag else None
@property
def needs_reorganization(self) -> bool:
"""Returns True if collection's file_format differs from current config.
When a user changes the file_name_format in their config, existing files
remain in their old locations. This property detects that mismatch so the
UI can offer to reorganize files.
Returns True when:
- file_format is None (collection has never been synced to config)
- file_format differs from current config format
"""
current = self._get_current_file_format()
if not current:
return False
if not self.file_format:
# Never been reorganized - needs to sync to current config
return True
return current != self.file_format
def _get_current_file_format(self) -> str | None:
"""Get current file_name_format from config based on collection kind."""
if self.kind == 'channel':
from modules.videos.lib import get_videos_downloader_config
return get_videos_downloader_config().file_name_format
elif self.kind == 'domain':
from modules.archive.lib import get_archive_downloader_config
return get_archive_downloader_config().file_name_format
return None
@property
def location(self) -> str:
"""The href of the collection in the App."""
return f'/collections/{self.id}'
@property
def is_directory_restricted(self) -> bool:
"""Returns True if this collection is restricted to a specific directory."""
return self.directory is not None
@staticmethod
def is_valid_domain_name(name: str) -> bool:
"""
Validate that a name is a valid domain format.
A valid domain must:
- Be a string
- Contain at least one "." (e.g., "example.com")
- Not start or end with "."
Examples:
Valid: "example.com", "sub.example.com", "a.b.c"
Invalid: "example", "example.", ".example", "."
Args:
name: The domain name to validate
Returns:
True if valid domain format, False otherwise
"""
# Use the registry for validation
return collection_type_registry.validate('domain', name)
def to_config(self) -> dict:
"""
Export this Collection's metadata to a config dict.
Only includes the minimum data necessary for reconstruction.
"""
config = {
'name': self.name,
'kind': self.kind,
}
if self.description:
config['description'] = self.description
if self.directory:
# Store relative path for portability across different media directories
try:
config['directory'] = str(get_relative_to_media_directory(self.directory))
except ValueError:
# Directory outside media directory, store as-is
config['directory'] = str(self.directory)
config['tag_name'] = self.tag_name
if self.file_format:
config['file_format'] = self.file_format
# Include downloads if any exist
if self.downloads:
config['downloads'] = [
{'url': d.url, 'frequency': d.frequency}
for d in self.downloads
]
return config
def get_or_create_download(self, session: Session, url: str, frequency: int,
reset_attempts: bool = False,
downloader_name: str = None,
sub_downloader_name: str = None) -> 'Download':
"""Get or create a Download for this Collection.
Args:
session: Database session
url: The URL to download
frequency: Seconds between re-downloading
reset_attempts: Reset download attempts if True
downloader_name: Name of the downloader to use
sub_downloader_name: Name of the sub-downloader for child downloads
Returns:
The Download instance
Raises:
InvalidDownload: If url or frequency is missing
"""
# Local imports to avoid circular import: collections -> downloader -> collections
from wrolpi.downloader import Download, download_manager
from wrolpi.errors import InvalidDownload
if not url:
raise InvalidDownload('Cannot get Download without url')
if not frequency:
raise InvalidDownload('Download for Collection must have a frequency')
download = Download.get_by_url(session, url)
if not download:
download = download_manager.recurring_download(
session, url, frequency, downloader_name,
sub_downloader_name=sub_downloader_name,
destination=str(self.directory) if self.directory else None,
reset_attempts=reset_attempts,
)
if reset_attempts:
download.attempts = 0
# Through the relationship, so `self.downloads` stays current if it is loaded.
download.collection = self
return download
@staticmethod
def from_config(session: Session, data: dict) -> 'Collection':
"""
Create or update a Collection from config data.
If the collection has a directory, auto-populate with FileGroups from that directory.
If the collection has no directory, items are managed manually (not from config).
Args:
session: Database session
data: Config dict containing collection metadata
Returns:
The created or updated Collection
Raises:
ValueError: If required fields are missing
"""
name = data.get('name')
if not name:
raise ValueError('Collection config must have a name')
description = data.get('description')
directory = data.get('directory')
tag_name = data.get('tag_name')
file_format = data.get('file_format')
kind = (data.get('kind') or 'channel').strip()
# Validate known kinds using the registry (allows extensible kinds).
if not collection_type_registry.is_registered(kind):
logger.warning(f"Unknown collection kind '{kind}', defaulting to 'channel'")
kind = 'channel'
# Validate collection name using the type registry
if not collection_type_registry.validate(kind, name):
description = collection_type_registry.get_description(kind) or "Invalid name format"
raise ValueError(f'Invalid {kind} name for collection: {repr(name)}. {description}')
# Convert directory to absolute path if provided, with validation
if directory:
directory = validate_collection_directory(directory)
# Try to find existing collection:
# - If directory is provided, resolve by unique directory
# - Otherwise, fall back to first match by (name, kind)
if directory:
collection = Collection.get_by_path(session, directory)
else:
collection = session.query(Collection).filter_by(name=name, kind=kind).first()
if collection:
# Update existing collection
logger.debug(f'Updating collection from config: {name}')
collection.description = description
collection.directory = directory
collection.kind = kind
collection.file_format = file_format
if tag_name:
tag = Tag.get_by_name(session, tag_name)
if tag:
collection.tag = tag
else:
logger.warning(f'Tag {repr(tag_name)} not found for collection {repr(name)}')
collection.tag = None
else:
collection.tag = None
else:
# Create new collection
logger.info(f'Creating new collection from config: {name}')
collection = Collection(
name=name,
description=description,
directory=directory,
kind=kind,
file_format=file_format,
)
if tag_name:
tag = Tag.get_by_name(session, tag_name)
if tag:
collection.tag = tag
else:
logger.warning(f'Tag {repr(tag_name)} not found for collection {repr(name)}')
session.add(collection)
session.flush([collection])
# If directory-restricted, populate with FileGroups from that directory
if collection.directory and collection.directory.is_dir():
logger.info(f'Populating collection {repr(name)} from directory {collection.directory}')
collection.populate_from_directory(session)
elif collection.directory and not collection.directory.is_dir():
logger.warning(f'Collection directory does not exist: {collection.directory}')
return collection
@staticmethod
def batch_from_config(session: Session, data_list: List[dict]) -> List['Collection']:
"""
Create or update multiple Collections from config data in batch.
More efficient than calling from_config() in a loop because it pre-fetches
existing collections and tags in bulk queries.
NOTE: This method does NOT call populate_from_directory(). For channel collections,
use claim_videos_for_channels() after batch_from_config() to assign videos to channels.
Args:
session: Database session
data_list: List of config dicts containing collection metadata
Returns:
List of created or updated Collections
"""
if not data_list:
return []
# Step 1: Validate and prepare directories
directories = []
for data in data_list:
directory = data.get('directory')
if directory:
directories.append(str(validate_collection_directory(directory)))
# Step 2: Pre-fetch existing collections by directory in ONE query
existing_by_directory = {}
if directories:
existing = session.query(Collection).filter(
Collection.directory.in_(directories)
).all()
existing_by_directory = {str(c.directory): c for c in existing}
# Step 3: Pre-fetch existing collections by (name, kind) for ALL collections
# This handles cases where directory changed or directory-less collections
# A flat IN on name stays within SQLite's expression-tree depth limit (an OR'd
# (name AND kind) pair per collection exceeds it past ~1000 collections); the
# exact (name, kind) match is applied in Python.
name_kind_pairs = {(d['name'], d.get('kind', 'channel')) for d in data_list}
existing_by_name_kind = {}
if name_kind_pairs:
names = {name for name, _ in name_kind_pairs}
existing = session.query(Collection).filter(Collection.name.in_(names)).all()
existing_by_name_kind = {(c.name, c.kind): c for c in existing
if (c.name, c.kind) in name_kind_pairs}
# Step 4: Pre-fetch all tags in ONE query
tag_names = {d['tag_name'] for d in data_list if d.get('tag_name')}
tags_by_name = {}
if tag_names:
tags = session.query(Tag).filter(Tag.name.in_(tag_names)).all()
tags_by_name = {t.name: t for t in tags}
# Step 5: Process collections
collections = []
new_collections = []
for data in data_list:
name = data.get('name')
if not name:
raise ValueError('Collection config must have a name')
description = data.get('description')
directory = data.get('directory')
tag_name = data.get('tag_name')
file_format = data.get('file_format')
kind = (data.get('kind') or 'channel').strip()
# Validate known kinds using the registry.
if not collection_type_registry.is_registered(kind):
logger.warning(f"Unknown collection kind '{kind}', defaulting to 'channel'")
kind = 'channel'
# Validate collection name using the type registry
if not collection_type_registry.validate(kind, name):
description_msg = collection_type_registry.get_description(kind) or "Invalid name format"
raise ValueError(f'Invalid {kind} name for collection: {repr(name)}. {description_msg}')
# Convert directory to absolute path if provided
if directory:
directory = validate_collection_directory(directory)
directory_str = str(directory)
else:
directory_str = None
# Find existing collection from pre-fetched data
# Priority: 1) by directory (if provided), 2) by (name, kind) as fallback
collection = None
if directory_str:
collection = existing_by_directory.get(directory_str)
# Always check by (name, kind) as fallback - handles directory changes
if not collection:
collection = existing_by_name_kind.get((name, kind))
if collection:
# Update existing collection
logger.debug(f'Updating collection from config: {name}')
collection.description = description
collection.directory = directory
collection.kind = kind
collection.file_format = file_format
if tag_name:
tag = tags_by_name.get(tag_name)
if tag:
collection.tag = tag
else:
logger.warning(f'Tag {repr(tag_name)} not found for collection {repr(name)}')
collection.tag = None
else:
collection.tag = None
else:
# Create new collection
logger.info(f'Creating new collection from config: {name}')
collection = Collection(
name=name,
description=description,
directory=directory,
kind=kind,
file_format=file_format,
)
if tag_name:
tag = tags_by_name.get(tag_name)
if tag:
collection.tag = tag
else:
logger.warning(f'Tag {repr(tag_name)} not found for collection {repr(name)}')
new_collections.append(collection)
collections.append(collection)
# Step 6: Batch add new collections
if new_collections:
session.add_all(new_collections)
# Single flush for all collections
session.flush()
return collections
def populate_from_directory(self, session: Session):
"""
Populate this collection with all FileGroups in the collection's directory.
Only works for directory-restricted collections.
"""
if not self.is_directory_restricted:
logger.warning(f'Cannot populate unrestricted collection from directory: {self}')
return
if not self.directory.is_dir():
logger.warning(f'Cannot populate collection, directory does not exist: {self.directory}')
return
# Find all FileGroups in this directory tree
# Use indexed directory column for efficient lookup
directory_str = str(self.directory)
file_groups = session.query(FileGroup).filter(
or_(
FileGroup.directory == directory_str,
FileGroup.directory.like(f'{directory_str}/%')
)
).all()
if not file_groups:
logger.debug(f'No FileGroups found in {self.directory}')
return
# Get existing items to avoid duplicates
existing_fg_ids = {item.file_group_id for item in self.items}
# Add new FileGroups
new_file_groups = [fg for fg in file_groups if fg.id not in existing_fg_ids]
if new_file_groups:
self.add_file_groups(session, new_file_groups)
logger.info(f'Added {len(new_file_groups)} FileGroups to collection {repr(self.name)}')
def validate_file_group(self, file_group: FileGroup) -> bool:
"""
Check if a FileGroup can be added to this Collection.
Returns True if the FileGroup is valid for this collection:
- If directory-restricted, the file must be in the directory tree
- Otherwise, any file is valid
"""
if not self.is_directory_restricted:
return True
# Check if the file_group's primary_path is within the collection's directory
try:
file_path = file_group.primary_path
if file_path:
# Check if file is in directory tree
file_path.relative_to(self.directory)
return True
except (ValueError, AttributeError):
# relative_to raises ValueError if path is not relative
return False
return False
def add_file_group(self, file_group: FileGroup, position: Optional[int] = None,
session: Session = None) -> 'CollectionItem':
"""
Add a FileGroup to this Collection.
Args:
file_group: The FileGroup to add
position: Optional position in the collection (None = append to end)
session: Database session
Returns:
The created CollectionItem
Raises:
ValueError: If the file_group cannot be added to this collection
"""
session = session or Session.object_session(self)
if not self.validate_file_group(file_group):
raise ValueError(
f'FileGroup {file_group.id} at {file_group.primary_path} '
f'cannot be added to collection "{self.name}" (directory restriction: {self.directory})'
)
# Check if already exists
existing = session.query(CollectionItem).filter_by(
collection_id=self.id,
file_group_id=file_group.id
).first()
if existing:
logger.warning(f'FileGroup {file_group.id} already in Collection {self.id}')
return existing
# Determine position
if position is None:
# Append to end
max_position = session.query(func.max(CollectionItem.position)).filter_by(
collection_id=self.id
).scalar()
position = (max_position or 0) + 1
else:
# Insert at specific position - need to shift existing items
self._shift_positions(session, position, shift_by=1)
item = CollectionItem(
collection_id=self.id,
file_group_id=file_group.id,
position=position
)
session.add(item)
session.flush([item])
return item
def add_file_groups(self, session: Session, file_groups: List[FileGroup]) -> List['CollectionItem']:
"""
Add multiple FileGroups to this Collection in batch.
More efficient than calling add_file_group in a loop.
Args:
session: Database session
file_groups: List of FileGroups to add
Returns:
List of created CollectionItems
"""
# Validate all file groups first
for fg in file_groups:
if not self.validate_file_group(fg):
raise ValueError(
f'FileGroup {fg.id} at {fg.primary_path} '
f'cannot be added to collection "{self.name}" (directory restriction: {self.directory})'
)
# Get existing items to avoid duplicates
existing_fg_ids = {item.file_group_id for item in self.items}
new_file_groups = [fg for fg in file_groups if fg.id not in existing_fg_ids]
if not new_file_groups:
logger.debug(f'All FileGroups already in Collection {self.id}')
return []
# Get starting position
max_position = session.query(func.max(CollectionItem.position)).filter_by(
collection_id=self.id
).scalar()
position = (max_position or 0) + 1
# Create items in batch
items = []
for fg in new_file_groups:
item = CollectionItem(
collection_id=self.id,
file_group_id=fg.id,
position=position
)
items.append(item)
position += 1
session.add_all(items)
session.flush(items)
logger.debug(f'Added {len(items)} FileGroups to Collection {self.id}')
return items
def remove_file_group(self, session: Session, file_group_id: int):
"""Remove a FileGroup from this Collection."""
item = session.query(CollectionItem).filter_by(
collection_id=self.id,
file_group_id=file_group_id
).first()
if item:
position = item.position
session.delete(item)
session.flush()
# Shift remaining items down
self._shift_positions(session, position + 1, shift_by=-1)
def remove_file_groups(self, session: Session, file_group_ids: List[int]):
"""
Remove multiple FileGroups from this Collection in batch.
More efficient than calling remove_file_group in a loop.
Args:
session: Database session
file_group_ids: List of FileGroup IDs to remove
"""
if not file_group_ids:
return
# Delete all items at once
deleted = session.query(CollectionItem).filter(
CollectionItem.collection_id == self.id,
CollectionItem.file_group_id.in_(file_group_ids)
).delete(synchronize_session=False)
logger.debug(f'Removed {deleted} FileGroups from Collection {self.id}')
# Resequence positions to close gaps (optional, but keeps positions clean)
items = session.query(CollectionItem).filter_by(
collection_id=self.id
).order_by(CollectionItem.position).all()
for idx, item in enumerate(items, start=1):
item.position = idx
session.flush(items)
def reorder_item(self, file_group_id: int, new_position: int, session: Session = None):
"""Move an item to a new position in the collection."""
session = session or Session.object_session(self)
item = session.query(CollectionItem).filter_by(
collection_id=self.id,
file_group_id=file_group_id
).first()
if not item:
raise ValueError(f'FileGroup {file_group_id} not in Collection {self.id}')
old_position = item.position
if old_position == new_position:
return
# Remove item from old position
session.query(CollectionItem).filter(
CollectionItem.collection_id == self.id,
CollectionItem.position > old_position
).update({'position': CollectionItem.position - 1})
# Make space at new position
session.query(CollectionItem).filter(
CollectionItem.collection_id == self.id,
CollectionItem.position >= new_position
).update({'position': CollectionItem.position + 1})
# Update item position
item.position = new_position
session.flush()
def _shift_positions(self, session: Session, from_position: int, shift_by: int):
"""Helper to shift positions of items."""
session.query(CollectionItem).filter(
CollectionItem.collection_id == self.id,
CollectionItem.position >= from_position
).update({'position': CollectionItem.position + shift_by})
session.flush()
def _next_position(self, session: Session) -> int:
"""Return the position to use when appending a new item to the end."""
max_position = session.query(func.max(CollectionItem.position)).filter_by(
collection_id=self.id
).scalar()
return (max_position or 0) + 1
def _place_item(self, session: Session, item: 'CollectionItem', position: Optional[int]):
"""Assign ``position`` to ``item`` (appending or inserting with a shift) and persist it."""
if position is None:
item.position = self._next_position(session)
else:
self._shift_positions(session, position, shift_by=1)
item.position = position
session.add(item)
session.flush([item])
def add_url(self, session: Session, url: str, title: Optional[str] = None,
position: Optional[int] = None) -> 'CollectionItem':
"""Add a `url` item (a link the WROLPi browser can open). Idempotent per URL."""
if not url:
raise ValueError('A url item requires a url')
existing = session.query(CollectionItem).filter_by(collection_id=self.id, url=url).first()
if existing:
logger.warning(f'URL {url!r} already in Collection {self.id}')
return existing
item = CollectionItem(collection_id=self.id, item_kind='url', url=url, title=title)
self._place_item(session, item, position)
return item
def add_zim_entry(self, session: Session, zim_id: int, zim_entry: str, title: Optional[str] = None,
position: Optional[int] = None) -> 'CollectionItem':
"""Add a `zim` item (a single article within a Zim file). Idempotent per (zim, entry)."""
# Local import to avoid a circular import (zim -> tags -> collections).
from modules.zim.models import Zim
zim = session.query(Zim).filter_by(id=zim_id).one_or_none()
if not zim:
raise ValueError(f'No Zim with id {zim_id}')
# Validate the entry exists within the Zim.
try:
zim.find_entry(zim_entry)
except Exception as e:
raise ValueError(f'Zim entry not found: {zim_entry!r}') from e
existing = session.query(CollectionItem).filter_by(
collection_id=self.id, zim_id=zim_id, zim_entry=zim_entry).first()
if existing:
logger.warning(f'Zim entry {zim_entry!r} already in Collection {self.id}')
return existing
item = CollectionItem(collection_id=self.id, item_kind='zim', zim_id=zim_id,
zim_entry=zim_entry, title=title)
self._place_item(session, item, position)
return item
def remove_item(self, session: Session, item_id: int) -> bool:
"""Remove an item by its CollectionItem id (any item_kind). Returns True if removed."""
item = session.query(CollectionItem).filter_by(collection_id=self.id, id=item_id).first()
if not item:
return False
position = item.position
session.delete(item)
session.flush()
# Close the gap left behind.
self._shift_positions(session, position + 1, shift_by=-1)
return True
def reorder_items(self, session: Session, ordered_item_ids: List[int]):
"""Set item order from a full list of CollectionItem ids (for drag-and-drop reordering).
Any items not present in ``ordered_item_ids`` are appended after, keeping their prior order.
@raise ValueError: if an id does not belong to this collection.
"""
items = session.query(CollectionItem).filter_by(collection_id=self.id).all()
by_id = {i.id: i for i in items}
for item_id in ordered_item_ids:
if item_id not in by_id:
raise ValueError(f'Item {item_id} is not in Collection {self.id}')
position = 1
seen = set()
for item_id in ordered_item_ids:
if item_id in seen:
# Ignore duplicate ids so positions stay contiguous (no gaps).
continue
by_id[item_id].position = position
seen.add(item_id)
position += 1
# Preserve any unlisted items after the explicitly-ordered ones.
for item in sorted(items, key=lambda i: i.position):
if item.id not in seen:
item.position = position
position += 1
session.flush()
def get_items(self, session: Session, limit: Optional[int] = None, offset: int = 0) -> List['CollectionItem']:
"""Get items in this collection, ordered by position."""
query = session.query(CollectionItem).filter_by(
collection_id=self.id
).order_by(CollectionItem.position)
if offset:
query = query.offset(offset)
if limit:
query = query.limit(limit)
return query.all()
@staticmethod
def get_by_id(session: Session, id_: int) -> Optional['Collection']:
"""Attempt to find a Collection with the provided id. Returns None if not found."""
return session.query(Collection).filter_by(id=id_).one_or_none()
@staticmethod
def find_by_id(session: Session, id_: int) -> 'Collection':
"""Find a Collection with the provided id, raises an exception if not found.
@raise UnknownCollection: if the collection cannot be found
"""
if collection := Collection.get_by_id(session, id_):
return collection
raise UnknownCollection(f'Cannot find Collection with id {id_}')
@staticmethod
def get_by_path(session: Session, path: pathlib.Path) -> Optional['Collection']:
"""Find a Collection by its directory path. Returns None if not found."""
if not path:
return None
path = pathlib.Path(path) if isinstance(path, str) else path
path = str(path.absolute()) if path.is_absolute() else str(get_media_directory() / path)
return session.query(Collection).filter_by(directory=path).one_or_none()
@staticmethod
def find_by_path(session: Session, path: pathlib.Path) -> 'Collection':
"""Find a Collection by its directory path, raises an exception if not found.
@raise UnknownCollection: if the collection cannot be found
"""
if collection := Collection.get_by_path(session, path):
return collection
raise UnknownCollection(f'Cannot find Collection with directory {path}')
@serializer
def dict(self) -> dict:
"""Return dictionary representation."""
d = super(Collection, self).dict()
d['tag_name'] = self.tag_name
# Directory may be outside media root; mirror to_config behavior
if self.directory:
try:
d['directory'] = self.directory.relative_to(get_media_directory())
except ValueError:
d['directory'] = self.directory
else:
d['directory'] = None
d['is_directory_restricted'] = self.is_directory_restricted
d['item_count'] = self.item_count
d['total_size'] = self.total_size
d['kind'] = self.kind
return d
# --- Tagging and moving helpers ---
def set_tag(self, tag_id_or_name: int | str | None) -> Optional[Tag]:
"""Assign or clear the Tag on this Collection. Mirrors Channel.set_tag semantics.
All collections can be tagged regardless of directory status. Tags enable
UI search/filtering even for directory-less collections.
"""
session = Session.object_session(self)
if tag_id_or_name is None:
self.tag = None
elif isinstance(tag_id_or_name, int):
self.tag = Tag.find_by_id(session, tag_id_or_name)
elif isinstance(tag_id_or_name, str):
self.tag = Tag.find_by_name(session, tag_id_or_name)
self.tag_id = self.tag.id if self.tag else None
return self.tag
@staticmethod
def json_options() -> tuple:
"""Loader options for a query whose Collections will be serialized: `__json__` lists the Downloads
(without their yt-dlp info_json, which is not serialized)."""
return selectinload(Collection.downloads).defer(Download.info_json),
@serializer
def __json__(self) -> dict:
"""Return JSON-serializable dict for API responses.
Returns directory as a string relative to media directory if applicable.
"""
# Get directory as string, relative to media directory
directory_str = None
if self.directory:
try:
directory_str = str(get_relative_to_media_directory(self.directory))
except ValueError:
# Directory is not relative to media directory, return as-is
directory_str = str(self.directory)
elif self.kind == 'playlist':
# A playlist without an explicit directory lives at the managed location
# (<playlists>/<tag>/<name>). Report that location so the directory is known even
# before the sync creates it; the DB keeps None so the sync auto-manages it.
from wrolpi.collections.sync import get_playlists_directory, _default_playlist_subdir
directory_str = str(get_relative_to_media_directory(
_default_playlist_subdir(get_playlists_directory(), self)))
return {
'id': self.id,
'name': self.name,
'kind': self.kind,
'directory': directory_str,
'description': self.description,
'tag_name': self.tag.name if self.tag else None,
'item_count': self.item_count,
'total_size': self.total_size,
'downloads': self.downloads,
'file_format': self.file_format,
'needs_reorganization': self.needs_reorganization,
}
def format_directory(self, tag_name: Optional[str]) -> pathlib.Path:
"""Compute the destination directory for this Collection given a tag name.
For video-style collections (kind == 'channel'), reuse videos destination formatting:
videos/<Tag>/<Collection Name>
For domain collections (kind == 'domain'), use the configured archive directory:
archive/<Tag>/<Domain Name>
@deprecated: Use format_destination() instead for new code.
"""
# Delegate to format_destination for consistency
return self.format_destination(tag_name)
def format_destination(self, tag_name: Optional[str] = None) -> pathlib.Path:
"""Format the collection's destination path using config template.
Uses videos_destination for kind='channel', archive_destination for kind='domain'.
Supports unified variables that work for both kinds.
Args:
tag_name: Optional tag name to include in the path
Returns:
Absolute path where this collection's files should be stored
"""
from wrolpi.common import get_wrolpi_config, escape_file_name
if self.kind == 'repo':
# Local import to avoid circular import: collections -> repos -> collections
from modules.repos.lib import format_collection_destination
return format_collection_destination(self, tag_name)
config = get_wrolpi_config()
if self.kind == 'channel':
template = config.videos_destination
elif self.kind == 'domain':
template = config.archive_destination
else:
template = '%(kind)s/%(tag)s/%(name)s'
escaped_name = escape_file_name(self.name) if self.name else ''
variables = dict(
# Unified variables
name=escaped_name,
tag=tag_name or '',
kind=self.kind or '',
# Backward compatibility aliases for videos
channel_name=escaped_name,
channel_tag=tag_name or '',
# Backward compatibility aliases for archives
domain=escaped_name,
domain_tag=tag_name or '',
)
destination = template % variables
return get_media_directory() / destination.lstrip('/')
def get_or_set_directory(self, session: Session, tag_name: Optional[str] = None) -> pathlib.Path:
"""Get collection's directory, or format and save it on first use.
- If collection has directory: return it
- If no directory: format from template, save to collection, return it
This ensures all files for a collection go to the same place, even if
the template config changes later.
Args:
session: Database session for committing changes
tag_name: Optional tag name to use when formatting (defaults to collection's tag)
Returns:
Absolute path to the collection's directory
"""
if self.directory:
# Return absolute path (joining with media_directory handles both relative and absolute paths)
return self.directory if self.directory.is_absolute() else get_media_directory() / self.directory
# First use - format and save
tag_name = tag_name or (self.tag.name if self.tag else None)
destination = self.format_destination(tag_name)
self.directory = destination # Store absolute path; MediaPathType handles conversion
session.commit()
# Trigger config save for persistence
if self.kind == 'domain':
from modules.archive.lib import save_domains_config
save_domains_config.activate_switch()
elif self.kind == 'channel':
from modules.videos.lib import save_channels_config
save_channels_config.activate_switch()
elif self.kind == 'repo':
from modules.repos.lib import save_repos_config
save_repos_config.activate_switch()
return destination
async def move_collection(self, directory: pathlib.Path, session: Session, send_events: bool = False,
with_files: bool = True):
"""Move all files under this Collection's directory to a new directory.
Also updates download destinations for downloads associated with this collection.
If with_files=False, only updates the directory reference without moving files.
Use this when files have already been moved by a parent directory rename.
"""
if not directory.is_dir():
raise FileNotFoundError(f'Destination directory does not exist: {directory}')
if not self.directory:
raise RuntimeError('Cannot move an unrestricted Collection (no directory set)')
old_directory = self.directory
def change_download_destinations(from_directory: pathlib.Path, to_directory: pathlib.Path):
"""Update download destinations from one directory to another."""
downloads = list(self.downloads)
downloads.extend(Download.get_all_by_destination(session, from_directory))
downloads = unique_by_predicate(downloads, lambda i: i.id)
if __debug__ and logger.isEnabledFor(TRACE_LEVEL):
logger.trace(f'move_collection: updating {len(downloads)} download destinations')
for download in downloads:
download.destination = to_directory
session.flush(downloads)
self.directory = directory
with flags.file_worker_busy:
# Update download destinations before moving files.
change_download_destinations(old_directory, directory)
session.commit()
# Move the contents of the Collection directory into the destination directory.
logger.info(f'Moving Collection {repr(self.name)} from {repr(str(old_directory))}')
# Save configs before move - move triggers imports of configs.
save_downloads_config.activate_switch()
save_tags_config.activate_switch()
if with_files:
try:
if not old_directory.exists():
# Old directory does not exist; refresh both
file_worker.queue_refresh([old_directory, directory])
if send_events:
Events.send_file_move_completed(f'Collection {repr(self.name)} was moved (directory missing)')
elif self.kind == 'repo':
# A Repo's files are not FileGroups (repos/ is ignored), so its clone is moved as one directory,
# and indexed only as a refresh would index it.
from modules.repos.lib import repo_directory_error
if error := repo_directory_error(directory):
raise ValidationError(f'Cannot move the Repo: {error}')
await asyncio.to_thread(_move_directory, old_directory, directory)
if refresh := remove_files_in_ignored_directories([old_directory, directory]):
file_worker.queue_refresh(refresh)
if send_events:
Events.send_file_move_completed(f'Collection {repr(self.name)} was moved')
else:
files_to_move = list(old_directory.iterdir())
if __debug__ and logger.isEnabledFor(TRACE_LEVEL):
logger.trace(f'move_collection: moving {len(files_to_move)} items from {old_directory}')
if files_to_move:
job_id = file_worker.queue_move(directory, files_to_move)
await file_worker.wait_for_job(job_id)
if send_events:
Events.send_file_move_completed(f'Collection {repr(self.name)} was moved')
except Exception as e:
logger.error(f'Collection move failed! Reverting changes...', exc_info=e)
# Revert download destinations
change_download_destinations(directory, old_directory)
self.directory = old_directory
self.flush(session)
if send_events:
Events.send_file_move_failed(f'Moving Collection {self.name} has failed')
raise
finally:
session.commit()
# The move keeps each moved directory (e.g. a repo's `.git`), even when emptied; delete the old
# directory when only empty directories remain.
if old_directory.exists() and not any(not i.is_dir() for i in old_directory.rglob('*')):
for directory in sorted(old_directory.rglob('*'), key=lambda i: len(i.parts), reverse=True):
delete_directory(directory)
delete_directory(old_directory)
else:
# Files already moved, just send event
if send_events:
Events.send_file_move_completed(f'Collection {repr(self.name)} directory updated')
class CollectionItem(ModelHelper, Base):
"""
An ordered item within a Collection.
An item is one of three kinds (``item_kind``):
- ``file``: references a FileGroup (video, archive, ebook, image, doc, ...).
- ``zim``: references a single Zim article (``zim_id`` + ``zim_entry`` path), like TagZimEntry.
- ``url``: an arbitrary URL the WROLPi browser can open (e.g. a map location ``/map?lat=&lon=``).
Exactly one kind's columns are populated; enforced by a CHECK constraint.
"""
__tablename__ = 'collection_item'
ITEM_KINDS = ('file', 'zim', 'url')
id = Column(Integer, primary_key=True)
collection_id = Column(Integer, ForeignKey('collection.id', ondelete='CASCADE'), nullable=False)
# Which kind of item this is.
item_kind = Column(String, nullable=False, server_default='file', default='file')
# `file` items: a FileGroup.
file_group_id = Column(Integer, ForeignKey('file_group.id', ondelete='CASCADE'), nullable=True)
# `zim` items: a single article within a Zim file.
zim_id = Column(Integer, ForeignKey('zim.id', ondelete='CASCADE'), nullable=True)
zim_entry = Column(Text, nullable=True)
# `url` items: any URL.
url = Column(Text, nullable=True)
# Display title (used for `url`/`zim` items, and as the on-disk stem; optional for `file`).
title = Column(Text, nullable=True)
# Position in the collection (for ordering)
position = Column(Integer, nullable=False, default=0)
# When this item was added
added_date = Column(DateTime, server_default=func.now(), nullable=False)
# Relationships
collection = relationship('Collection', back_populates='items')
file_group: Mapped[FileGroup] = relationship('FileGroup')
zim = relationship('Zim')
# Indexes for performance
__table_args__ = (
Index('idx_collection_item_collection_id', 'collection_id'),
Index('idx_collection_item_collection_position', 'collection_id', 'position'),
Index('idx_collection_item_file_group_id', 'file_group_id'),
Index('idx_collection_item_position', 'position'),
# A FileGroup/Zim-article/URL appears at most once per collection. NULLs are distinct in
# SQLite unique constraints, so each constraint only applies to items of that kind.
UniqueConstraint('collection_id', 'file_group_id', name='uq_collection_file_group'),
UniqueConstraint('collection_id', 'zim_id', 'zim_entry', name='uq_collection_zim_entry'),
UniqueConstraint('collection_id', 'url', name='uq_collection_url'),
# Exactly one kind's columns are populated.
CheckConstraint(
"(item_kind = 'file' AND file_group_id IS NOT NULL AND zim_id IS NULL AND url IS NULL)"
" OR (item_kind = 'zim' AND zim_id IS NOT NULL AND zim_entry IS NOT NULL"
" AND file_group_id IS NULL AND url IS NULL)"
" OR (item_kind = 'url' AND url IS NOT NULL AND file_group_id IS NULL AND zim_id IS NULL)",
name='ck_collection_item_kind',
),
)
def __repr__(self):
return (f'<CollectionItem collection={loaded(self, "collection_id")} kind={loaded(self, "item_kind")} '
f'position={loaded(self, "position")}>')
@staticmethod
def json_options() -> tuple:
"""Loader options for a query whose items will be serialized (`dict()`): each reads its FileGroup and the
FileGroup's Tags, or its Zim."""
return joinedload(CollectionItem.file_group).selectinload(FileGroup.tag_files), joinedload(CollectionItem.zim)
@serializer
def dict(self) -> dict:
"""Return dictionary representation."""
d = super(CollectionItem, self).dict()
if self.item_kind == 'file' and self.file_group:
d['file_group'] = self.file_group.__json__()
elif self.item_kind == 'zim' and self.zim:
d['zim'] = {'id': self.zim_id, 'entry': self.zim_entry, 'path': str(self.zim.path)}
return d
def to_config(self) -> Optional[dict]:
"""Serialize this item for playlists.yaml.
Files/Zims are referenced by their media-relative path so they survive a DB rebuild.
Returns None if the item cannot currently be represented (e.g. missing relationship).
"""
if self.item_kind == 'file' and self.file_group:
try:
ref = str(get_relative_to_media_directory(self.file_group.primary_path))
except ValueError:
ref = str(self.file_group.primary_path)
data = {'file': ref}
elif self.item_kind == 'zim' and self.zim:
try:
ref = str(get_relative_to_media_directory(self.zim.path))
except ValueError:
ref = str(self.zim.path)
data = {'zim': ref, 'entry': self.zim_entry}
elif self.item_kind == 'url':
data = {'url': self.url}
else:
return None
if self.title:
data['title'] = self.title
return data