X-Git-Url: https://plomlompom.com/repos/feed.xml?a=blobdiff_plain;f=plomtask%2Fdb.py;h=13cdaef5b9c7d3e992f8c92730a9979b9eee2d73;hb=HEAD;hp=b3f1db00986b1142f5f31be34060864840ab5bdc;hpb=21df71ef1fde304b158da5989692c01f463515b5;p=plomtask diff --git a/plomtask/db.py b/plomtask/db.py index b3f1db0..13cdaef 100644 --- a/plomtask/db.py +++ b/plomtask/db.py @@ -4,7 +4,7 @@ from os import listdir from os.path import isfile from difflib import Differ from sqlite3 import connect as sql_connect, Cursor, Row -from typing import Any, Self, TypeVar, Generic +from typing import Any, Self, TypeVar, Generic, Callable from plomtask.exceptions import HandledException, NotFoundException from plomtask.dating import valid_date @@ -235,11 +235,13 @@ class BaseModel(Generic[BaseModelId]): to_save: list[str] = [] to_save_versioned: list[str] = [] to_save_relations: list[tuple[str, str, str, int]] = [] + add_to_dict: list[str] = [] id_: None | BaseModelId cache_: dict[BaseModelId, Self] to_search: list[str] = [] can_create_by_id = False _exists = True + sorters: dict[str, Callable[..., Any]] = {} def __init__(self, id_: BaseModelId | None) -> None: if isinstance(id_, int) and id_ < 1: @@ -295,6 +297,9 @@ class BaseModel(Generic[BaseModelId]): for rel in getattr(self, attr_name): l += [rel.as_dict_into_reference(library)] d[attr_name] = l + for k in self.add_to_dict: + d[k] = [x.as_dict_into_reference(library) + for x in getattr(self, k)] return d def as_dict_into_reference(self, @@ -326,6 +331,27 @@ class BaseModel(Generic[BaseModelId]): assert isinstance(as_dict['id'], (int, str)) return as_dict['id'] + @classmethod + def name_lowercase(cls) -> str: + """Convenience method to return cls' name in lowercase.""" + return cls.__name__.lower() + + @classmethod + def sort_by(cls, seq: list[Any], sort_key: str, default: str = 'title' + ) -> str: + """Sort cls list by cls.sorters[sort_key] (reverse if '-'-prefixed).""" + reverse = False + if len(sort_key) > 1 and '-' == sort_key[0]: + sort_key = sort_key[1:] + reverse = True + if sort_key not in cls.sorters: + sort_key = default + sorter: Callable[..., Any] = cls.sorters[sort_key] + seq.sort(key=sorter, reverse=reverse) + if reverse: + sort_key = f'-{sort_key}' + return sort_key + # cache management # (we primarily use the cache to ensure we work on the same object in # memory no matter where and how we retrieve it, e.g. we don't want