X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=plomtask%2Fdb.py;h=b5461a507e9612e2593643e6d6e198779e2fc456;hb=2337eb37f27fdc60e3cb000052f00218d815c49f;hp=548381e72e5c473047d7b90cb7f55ea1627b0ca6;hpb=85c260d1416340274b954175a69563868f481bc5;p=plomtask diff --git a/plomtask/db.py b/plomtask/db.py index 548381e..b5461a5 100644 --- a/plomtask/db.py +++ b/plomtask/db.py @@ -6,6 +6,7 @@ from difflib import Differ from sqlite3 import connect as sql_connect, Cursor, Row from typing import Any, Self, TypeVar, Generic from plomtask.exceptions import HandledException, NotFoundException +from plomtask.dating import valid_date EXPECTED_DB_VERSION = 4 MIGRATIONS_DIR = 'migrations' @@ -354,6 +355,29 @@ class BaseModel(Generic[BaseModelId]): items[item.id_] = item return list(items.values()) + @classmethod + def by_date_range_with_limits(cls: type[BaseModelInstance], + db_conn: DatabaseConnection, + date_range: tuple[str, str], + date_col: str = 'day' + ) -> tuple[list[BaseModelInstance], str, + str]: + """Return list of Days in database within (open) date_range interval. + + If no range values provided, defaults them to 'yesterday' and + 'tomorrow'. Knows to properly interpret these and 'today' as value. + """ + start_str = date_range[0] if date_range[0] else 'yesterday' + end_str = date_range[1] if date_range[1] else 'tomorrow' + start_date = valid_date(start_str) + end_date = valid_date(end_str) + items = [] + sql = f'SELECT id FROM {cls.table_name} ' + sql += f'WHERE {date_col} >= ? AND {date_col} <= ?' + for row in db_conn.exec(sql, (start_date, end_date)): + items += [cls.by_id(db_conn, row[0])] + return items, start_date, end_date + @classmethod def matching(cls: type[BaseModelInstance], db_conn: DatabaseConnection, pattern: str) -> list[BaseModelInstance]: