Add sorting mechanism post collection update

e.g. sort some collections strictly on release date
(if collection author is not capable of sorting them ...)
master
Felix Stupp 2 years ago
parent f51cbc31c3
commit 321847446b
Signed by: zocker
GPG Key ID: 93E1BD26F6B02FB7

@ -3,11 +3,14 @@ from __future__ import annotations
from datetime import datetime, timedelta from datetime import datetime, timedelta
import logging import logging
import math import math
from typing import Optional, TypeVar from typing import Any, Callable, Mapping, Optional, TypeVar
from pony import orm
from ...models import ( from ...models import (
CollectionUriMapping, CollectionUriMapping,
MediaCollection, MediaCollection,
MediaCollectionLink,
MediaElement, MediaElement,
) )
from ..generic import ExtractedData, ExtractionError, GeneralExtractor from ..generic import ExtractedData, ExtractionError, GeneralExtractor
@ -79,3 +82,20 @@ class CollectionExtractor(GeneralExtractor[MediaCollection, T]):
f"Add to collection {collection.title!r} media {uri!r} (Season {season}, Episode {episode})" f"Add to collection {collection.title!r} media {uri!r} (Season {season}, Episode {episode})"
) )
return element return element
def _sort_episodes(self, coll: MediaCollection):
sorting_methods: Mapping[int, Callable[[MediaCollectionLink], Any]] = {
1: lambda l: l.element.release_date,
}
method = sorting_methods.get(coll.sorting_method)
if method is None:
return
logging.debug(f"Sort collection by type {coll.sorting_method}")
for index, link in enumerate(
orm.select(l for l in coll.media_links).order_by(method)
):
link.season = 0
link.episode = index + 1
def _update_hook(self, object: MediaCollection, data: ExtractedData[T]):
self._sort_episodes(object)

@ -821,6 +821,10 @@ class MediaCollection(db.Entity, UriHolder, Tagable):
bool, bool,
default=True, default=True,
) )
sorting_method: int = orm.Required(
int,
default=0,
)
pinned: bool = orm.Required( pinned: bool = orm.Required(
bool, bool,

Loading…
Cancel
Save