From: Christian Heller Date: Wed, 12 Jun 2024 06:20:33 +0000 (+0200) Subject: Improve InputsParser tests. X-Git-Url: https://plomlompom.com/repos/?a=commitdiff_plain;h=HEAD;hp=80491fac3c476788d90010812c9ba0b95701e09b;p=plomtask Improve InputsParser tests. --- diff --git a/.pylintrc b/.pylintrc index 82e4d96..b4814d1 100644 --- a/.pylintrc +++ b/.pylintrc @@ -1,3 +1,3 @@ [BASIC] init-hook='import sys; sys.path.append(".")' -good-names-rgxs=(test_)?do_(GET|POST)(_[a-z]+)?,test_[A-Z]+ +good-names-rgxs=.*_?do_(GET|POST)(_[a-z]+)?,test_[A-Z]+ diff --git a/migrations/0_init.sql b/migrations/0_init.sql new file mode 100644 index 0000000..b2979a5 --- /dev/null +++ b/migrations/0_init.sql @@ -0,0 +1,112 @@ +CREATE TABLE condition_descriptions ( + parent INTEGER NOT NULL, + timestamp TEXT NOT NULL, + description TEXT NOT NULL, + PRIMARY KEY (parent, timestamp), + FOREIGN KEY (parent) REFERENCES conditions(id) +); +CREATE TABLE condition_titles ( + parent INTEGER NOT NULL, + timestamp TEXT NOT NULL, + title TEXT NOT NULL, + PRIMARY KEY (parent, timestamp), + FOREIGN KEY (parent) REFERENCES conditions(id) +); +CREATE TABLE conditions ( + id INTEGER PRIMARY KEY, + is_active BOOLEAN NOT NULL +); +CREATE TABLE days ( + id TEXT PRIMARY KEY, + comment TEXT NOT NULL +); +CREATE TABLE process_conditions ( + process INTEGER NOT NULL, + condition INTEGER NOT NULL, + PRIMARY KEY (process, condition), + FOREIGN KEY (process) REFERENCES processes(id), + FOREIGN KEY (condition) REFERENCES conditions(id) +); +CREATE TABLE process_descriptions ( + parent INTEGER NOT NULL, + timestamp TEXT NOT NULL, + description TEXT NOT NULL, + PRIMARY KEY (parent, timestamp), + FOREIGN KEY (parent) REFERENCES processes(id) +); +CREATE TABLE process_disables ( + process INTEGER NOT NULL, + condition INTEGER NOT NULL, + PRIMARY KEY(process, condition), + FOREIGN KEY (process) REFERENCES processes(id), + FOREIGN KEY (condition) REFERENCES conditions(id) +); +CREATE TABLE process_efforts ( + parent INTEGER NOT NULL, + timestamp TEXT NOT NULL, + effort REAL NOT NULL, + PRIMARY KEY (parent, timestamp), + FOREIGN KEY (parent) REFERENCES processes(id) +); +CREATE TABLE process_enables ( + process INTEGER NOT NULL, + condition INTEGER NOT NULL, + PRIMARY KEY(process, condition), + FOREIGN KEY (process) REFERENCES processes(id), + FOREIGN KEY (condition) REFERENCES conditions(id) +); +CREATE TABLE process_steps ( + id INTEGER PRIMARY KEY, + owner INTEGER NOT NULL, + step_process INTEGER NOT NULL, + parent_step INTEGER, + FOREIGN KEY (owner) REFERENCES processes(id), + FOREIGN KEY (step_process) REFERENCES processes(id), + FOREIGN KEY (parent_step) REFERENCES process_steps(step_id) +); +CREATE TABLE process_titles ( + parent INTEGER NOT NULL, + timestamp TEXT NOT NULL, + title TEXT NOT NULL, + PRIMARY KEY (parent, timestamp), + FOREIGN KEY (parent) REFERENCES processes(id) +); +CREATE TABLE processes ( + id INTEGER PRIMARY KEY +); +CREATE TABLE todo_children ( + parent INTEGER NOT NULL, + child INTEGER NOT NULL, + PRIMARY KEY (parent, child), + FOREIGN KEY (parent) REFERENCES todos(id), + FOREIGN KEY (child) REFERENCES todos(id) +); +CREATE TABLE todo_conditions ( + todo INTEGER NOT NULL, + condition INTEGER NOT NULL, + PRIMARY KEY(todo, condition), + FOREIGN KEY (todo) REFERENCES todos(id), + FOREIGN KEY (condition) REFERENCES conditions(id) +); +CREATE TABLE todo_disables ( + todo INTEGER NOT NULL, + condition INTEGER NOT NULL, + PRIMARY KEY(todo, condition), + FOREIGN KEY (todo) REFERENCES todos(id), + FOREIGN KEY (condition) REFERENCES conditions(id) +); +CREATE TABLE todo_enables ( + todo INTEGER NOT NULL, + condition INTEGER NOT NULL, + PRIMARY KEY(todo, condition), + FOREIGN KEY (todo) REFERENCES todos(id), + FOREIGN KEY (condition) REFERENCES conditions(id) +); +CREATE TABLE todos ( + id INTEGER PRIMARY KEY, + process INTEGER NOT NULL, + is_done BOOLEAN NOT NULL, + day TEXT NOT NULL, + FOREIGN KEY (process) REFERENCES processes(id), + FOREIGN KEY (day) REFERENCES days(id) +); diff --git a/migrations/1_add_Todo_comment.sql b/migrations/1_add_Todo_comment.sql new file mode 100644 index 0000000..0c58335 --- /dev/null +++ b/migrations/1_add_Todo_comment.sql @@ -0,0 +1 @@ +ALTER TABLE todos ADD COLUMN comment TEXT NOT NULL DEFAULT ""; diff --git a/migrations/2_add_Todo_effort.sql b/migrations/2_add_Todo_effort.sql new file mode 100644 index 0000000..0506431 --- /dev/null +++ b/migrations/2_add_Todo_effort.sql @@ -0,0 +1 @@ +ALTER TABLE todos ADD COLUMN effort REAL; diff --git a/migrations/3_add_Todo_and_Process_calendarize.sql b/migrations/3_add_Todo_and_Process_calendarize.sql new file mode 100644 index 0000000..dcd65b2 --- /dev/null +++ b/migrations/3_add_Todo_and_Process_calendarize.sql @@ -0,0 +1,2 @@ +ALTER TABLE todos ADD COLUMN calendarize BOOLEAN NOT NULL DEFAULT FALSE; +ALTER TABLE processes ADD COLUMN calendarize BOOLEAN NOT NULL DEFAULT FALSE; diff --git a/migrations/4_create_Process_blockers_Todo_blockers.sql b/migrations/4_create_Process_blockers_Todo_blockers.sql new file mode 100644 index 0000000..8e82ca1 --- /dev/null +++ b/migrations/4_create_Process_blockers_Todo_blockers.sql @@ -0,0 +1,14 @@ +CREATE TABLE process_blockers ( + process INTEGER NOT NULL, + condition INTEGER NOT NULL, + PRIMARY KEY (process, condition), + FOREIGN KEY (process) REFERENCES processes(id), + FOREIGN KEY (condition) REFERENCES conditions(id) +); +CREATE TABLE todo_blockers ( + todo INTEGER NOT NULL, + condition INTEGER NOT NULL, + PRIMARY KEY (todo, condition), + FOREIGN KEY (todo) REFERENCES todos(id), + FOREIGN KEY (condition) REFERENCES conditions(id) +); diff --git a/migrations/5_create_process_step_suppressions.sql b/migrations/5_create_process_step_suppressions.sql new file mode 100644 index 0000000..6ac4ea2 --- /dev/null +++ b/migrations/5_create_process_step_suppressions.sql @@ -0,0 +1,8 @@ +CREATE TABLE process_step_suppressions ( + process INTEGER NOT NULL, + process_step INTEGER NOT NULL, + PRIMARY KEY (process, process_step), + FOREIGN KEY (process) REFERENCES processes(id), + FOREIGN KEY (process_step) REFERENCES process_steps(id) +); + diff --git a/migrations/init_5.sql b/migrations/init_5.sql new file mode 100644 index 0000000..d539446 --- /dev/null +++ b/migrations/init_5.sql @@ -0,0 +1,137 @@ +CREATE TABLE condition_descriptions ( + parent INTEGER NOT NULL, + timestamp TEXT NOT NULL, + description TEXT NOT NULL, + PRIMARY KEY (parent, timestamp), + FOREIGN KEY (parent) REFERENCES conditions(id) +); +CREATE TABLE condition_titles ( + parent INTEGER NOT NULL, + timestamp TEXT NOT NULL, + title TEXT NOT NULL, + PRIMARY KEY (parent, timestamp), + FOREIGN KEY (parent) REFERENCES conditions(id) +); +CREATE TABLE conditions ( + id INTEGER PRIMARY KEY, + is_active BOOLEAN NOT NULL +); +CREATE TABLE days ( + id TEXT PRIMARY KEY, + comment TEXT NOT NULL +); +CREATE TABLE process_blockers ( + process INTEGER NOT NULL, + condition INTEGER NOT NULL, + PRIMARY KEY (process, condition), + FOREIGN KEY (process) REFERENCES processes(id), + FOREIGN KEY (condition) REFERENCES conditions(id) +); +CREATE TABLE process_conditions ( + process INTEGER NOT NULL, + condition INTEGER NOT NULL, + PRIMARY KEY (process, condition), + FOREIGN KEY (process) REFERENCES processes(id), + FOREIGN KEY (condition) REFERENCES conditions(id) +); +CREATE TABLE process_descriptions ( + parent INTEGER NOT NULL, + timestamp TEXT NOT NULL, + description TEXT NOT NULL, + PRIMARY KEY (parent, timestamp), + FOREIGN KEY (parent) REFERENCES processes(id) +); +CREATE TABLE process_disables ( + process INTEGER NOT NULL, + condition INTEGER NOT NULL, + PRIMARY KEY(process, condition), + FOREIGN KEY (process) REFERENCES processes(id), + FOREIGN KEY (condition) REFERENCES conditions(id) +); +CREATE TABLE process_efforts ( + parent INTEGER NOT NULL, + timestamp TEXT NOT NULL, + effort REAL NOT NULL, + PRIMARY KEY (parent, timestamp), + FOREIGN KEY (parent) REFERENCES processes(id) +); +CREATE TABLE process_enables ( + process INTEGER NOT NULL, + condition INTEGER NOT NULL, + PRIMARY KEY(process, condition), + FOREIGN KEY (process) REFERENCES processes(id), + FOREIGN KEY (condition) REFERENCES conditions(id) +); +CREATE TABLE process_step_suppressions ( + process INTEGER NOT NULL, + process_step INTEGER NOT NULL, + PRIMARY KEY (process, process_step), + FOREIGN KEY (process) REFERENCES processes(id), + FOREIGN KEY (process_step) REFERENCES process_steps(id) +); +CREATE TABLE process_steps ( + id INTEGER PRIMARY KEY, + owner INTEGER NOT NULL, + step_process INTEGER NOT NULL, + parent_step INTEGER, + FOREIGN KEY (owner) REFERENCES processes(id), + FOREIGN KEY (step_process) REFERENCES processes(id), + FOREIGN KEY (parent_step) REFERENCES process_steps(step_id) +); +CREATE TABLE process_titles ( + parent INTEGER NOT NULL, + timestamp TEXT NOT NULL, + title TEXT NOT NULL, + PRIMARY KEY (parent, timestamp), + FOREIGN KEY (parent) REFERENCES processes(id) +); +CREATE TABLE processes ( + id INTEGER PRIMARY KEY, + calendarize BOOLEAN NOT NULL DEFAULT FALSE +); +CREATE TABLE todo_blockers ( + todo INTEGER NOT NULL, + condition INTEGER NOT NULL, + PRIMARY KEY (todo, condition), + FOREIGN KEY (todo) REFERENCES todos(id), + FOREIGN KEY (condition) REFERENCES conditions(id) +); +CREATE TABLE todo_children ( + parent INTEGER NOT NULL, + child INTEGER NOT NULL, + PRIMARY KEY (parent, child), + FOREIGN KEY (parent) REFERENCES todos(id), + FOREIGN KEY (child) REFERENCES todos(id) +); +CREATE TABLE todo_conditions ( + todo INTEGER NOT NULL, + condition INTEGER NOT NULL, + PRIMARY KEY(todo, condition), + FOREIGN KEY (todo) REFERENCES todos(id), + FOREIGN KEY (condition) REFERENCES conditions(id) +); +CREATE TABLE todo_disables ( + todo INTEGER NOT NULL, + condition INTEGER NOT NULL, + PRIMARY KEY(todo, condition), + FOREIGN KEY (todo) REFERENCES todos(id), + FOREIGN KEY (condition) REFERENCES conditions(id) +); +CREATE TABLE todo_enables ( + todo INTEGER NOT NULL, + condition INTEGER NOT NULL, + PRIMARY KEY(todo, condition), + FOREIGN KEY (todo) REFERENCES todos(id), + FOREIGN KEY (condition) REFERENCES conditions(id) +); +CREATE TABLE todos ( + id INTEGER PRIMARY KEY, + process INTEGER NOT NULL, + is_done BOOLEAN NOT NULL, + day TEXT NOT NULL, + comment TEXT NOT NULL DEFAULT "", + effort REAL, + calendarize BOOLEAN NOT NULL DEFAULT FALSE, + FOREIGN KEY (process) REFERENCES processes(id), + FOREIGN KEY (day) REFERENCES days(id) +); diff --git a/plomtask/conditions.py b/plomtask/conditions.py index a6e9c97..d255927 100644 --- a/plomtask/conditions.py +++ b/plomtask/conditions.py @@ -12,6 +12,7 @@ class Condition(BaseModel[int]): table_name = 'conditions' to_save = ['is_active'] to_save_versioned = ['title', 'description'] + to_search = ['title.newest', 'description.newest'] def __init__(self, id_: int | None, is_active: bool = False) -> None: super().__init__(id_) @@ -40,7 +41,7 @@ class Condition(BaseModel[int]): if self.id_ is None: raise HandledException('cannot remove unsaved item') for item in ('process', 'todo'): - for attr in ('conditions', 'enables', 'disables'): + for attr in ('conditions', 'blockers', 'enables', 'disables'): table_name = f'{item}_{attr}' for _ in db_conn.row_where(table_name, 'condition', self.id_): raise HandledException('cannot remove Condition in use') @@ -50,6 +51,12 @@ class Condition(BaseModel[int]): class ConditionsRelations: """Methods for handling relations to Conditions, for Todo and Process.""" + def __init__(self) -> None: + self.conditions: list[Condition] = [] + self.blockers: list[Condition] = [] + self.enables: list[Condition] = [] + self.disables: list[Condition] = [] + def set_conditions(self, db_conn: DatabaseConnection, ids: list[int], target: str = 'conditions') -> None: """Set self.[target] to Conditions identified by ids.""" @@ -59,6 +66,11 @@ class ConditionsRelations: for id_ in ids: target_list += [Condition.by_id(db_conn, id_)] + def set_blockers(self, db_conn: DatabaseConnection, + ids: list[int]) -> None: + """Set self.enables to Conditions identified by ids.""" + self.set_conditions(db_conn, ids, 'blockers') + def set_enables(self, db_conn: DatabaseConnection, ids: list[int]) -> None: """Set self.enables to Conditions identified by ids.""" diff --git a/plomtask/dating.py b/plomtask/dating.py new file mode 100644 index 0000000..26b3ce3 --- /dev/null +++ b/plomtask/dating.py @@ -0,0 +1,30 @@ +"""Various utilities for handling dates.""" +from datetime import datetime, timedelta +from plomtask.exceptions import BadFormatException + +DATE_FORMAT = '%Y-%m-%d' + + +def valid_date(date_str: str) -> str: + """Validate date against DATE_FORMAT or 'today'/'yesterday'/'tomorrow. + + In any case, returns in DATE_FORMAT. + """ + if date_str == 'today': + date_str = date_in_n_days(0) + elif date_str == 'yesterday': + date_str = date_in_n_days(-1) + elif date_str == 'tomorrow': + date_str = date_in_n_days(1) + try: + dt = datetime.strptime(date_str, DATE_FORMAT) + except (ValueError, TypeError) as e: + msg = f'Given date of wrong format: {date_str}' + raise BadFormatException(msg) from e + return dt.strftime(DATE_FORMAT) + + +def date_in_n_days(n: int) -> str: + """Return in DATE_FORMAT date from today + n days.""" + date = datetime.now() + timedelta(days=n) + return date.strftime(DATE_FORMAT) diff --git a/plomtask/days.py b/plomtask/days.py index 0e07bf7..0815b9b 100644 --- a/plomtask/days.py +++ b/plomtask/days.py @@ -1,29 +1,9 @@ """Collecting Day and date-related items.""" from __future__ import annotations from datetime import datetime, timedelta -from plomtask.exceptions import BadFormatException from plomtask.db import DatabaseConnection, BaseModel - -DATE_FORMAT = '%Y-%m-%d' -MIN_RANGE_DATE = '2024-01-01' -MAX_RANGE_DATE = '2030-12-31' - - -def valid_date(date_str: str) -> str: - """Validate date against DATE_FORMAT or 'today', return in DATE_FORMAT.""" - if date_str == 'today': - date_str = todays_date() - try: - dt = datetime.strptime(date_str, DATE_FORMAT) - except (ValueError, TypeError) as e: - msg = f'Given date of wrong format: {date_str}' - raise BadFormatException(msg) from e - return dt.strftime(DATE_FORMAT) - - -def todays_date() -> str: - """Return current date in DATE_FORMAT.""" - return datetime.now().strftime(DATE_FORMAT) +from plomtask.todos import Todo +from plomtask.dating import (DATE_FORMAT, valid_date) class Day(BaseModel[str]): @@ -36,32 +16,31 @@ class Day(BaseModel[str]): super().__init__(id_) self.datetime = datetime.strptime(self.date, DATE_FORMAT) self.comment = comment + self.calendarized_todos: list[Todo] = [] def __lt__(self, other: Day) -> bool: return self.date < other.date @classmethod - def all(cls, db_conn: DatabaseConnection, - date_range: tuple[str, str] = ('', ''), - fill_gaps: bool = False) -> list[Day]: - """Return list of Days in database within (open) date_range interval. - - If no range values provided, defaults them to MIN_RANGE_DATE and - MAX_RANGE_DATE. Also knows to properly interpret 'today' as value. + def by_date_range_filled(cls, db_conn: DatabaseConnection, + start: str, end: str) -> list[Day]: + """Return days existing and non-existing between dates start/end.""" + ret = cls.by_date_range_with_limits(db_conn, (start, end), 'id') + days, start_date, end_date = ret + return cls.with_filled_gaps(days, start_date, end_date) - On fill_gaps=True, will instantiate (without saving) Days of all dates - within the date range that don't exist yet. - """ - min_date = '2024-01-01' - max_date = '2030-12-31' - start_date = valid_date(date_range[0] if date_range[0] else min_date) - end_date = valid_date(date_range[1] if date_range[1] else max_date) - days = [] - sql = 'SELECT id FROM days WHERE id >= ? AND id <= ?' - for row in db_conn.exec(sql, (start_date, end_date)): - days += [cls.by_id(db_conn, row[0])] + @classmethod + def with_filled_gaps(cls, days: list[Day], start_date: str, end_date: str + ) -> list[Day]: + """In days, fill with (un-saved) Days gaps between start/end_date.""" + if start_date > end_date: + return days days.sort() - if fill_gaps and len(days) > 1: + if start_date not in [d.date for d in days]: + days[:] = [Day(start_date)] + days + if end_date not in [d.date for d in days]: + days += [Day(end_date)] + if len(days) > 1: gapless_days = [] for i, day in enumerate(days): gapless_days += [day] @@ -69,7 +48,7 @@ class Day(BaseModel[str]): while day.next_date != days[i+1].date: day = Day(day.next_date) gapless_days += [day] - days = gapless_days + days[:] = gapless_days return days @property @@ -78,6 +57,17 @@ class Day(BaseModel[str]): assert isinstance(self.id_, str) return self.id_ + @property + def first_of_month(self) -> bool: + """Return what month self.date is part of.""" + assert isinstance(self.id_, str) + return self.id_[-2:] == '01' + + @property + def month_name(self) -> str: + """Return what month self.date is part of.""" + return self.datetime.strftime('%B') + @property def weekday(self) -> str: """Return what weekday matches self.date.""" @@ -94,3 +84,8 @@ class Day(BaseModel[str]): """Return date succeeding date of this Day.""" next_datetime = self.datetime + timedelta(days=1) return next_datetime.strftime(DATE_FORMAT) + + def collect_calendarized_todos(self, db_conn: DatabaseConnection) -> None: + """Fill self.calendarized_todos.""" + self.calendarized_todos = [t for t in Todo.by_date(db_conn, self.date) + if t.calendarize] diff --git a/plomtask/db.py b/plomtask/db.py index e4d5f6e..2ea7421 100644 --- a/plomtask/db.py +++ b/plomtask/db.py @@ -1,13 +1,21 @@ """Database management.""" from __future__ import annotations +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 plomtask.exceptions import HandledException, NotFoundException +from plomtask.dating import valid_date -PATH_DB_SCHEMA = 'scripts/init.sql' -EXPECTED_DB_VERSION = 0 +EXPECTED_DB_VERSION = 5 +MIGRATIONS_DIR = 'migrations' +FILENAME_DB_SCHEMA = f'init_{EXPECTED_DB_VERSION}.sql' +PATH_DB_SCHEMA = f'{MIGRATIONS_DIR}/{FILENAME_DB_SCHEMA}' + + +class UnmigratedDbException(HandledException): + """To identify case of unmigrated DB file.""" class DatabaseFile: # pylint: disable=too-few-public-methods @@ -17,43 +25,131 @@ class DatabaseFile: # pylint: disable=too-few-public-methods self.path = path self._check() - def remake(self) -> None: - """Create tables in self.path file as per PATH_DB_SCHEMA sql file.""" - with sql_connect(self.path) as conn: + @classmethod + def create_at(cls, path: str) -> DatabaseFile: + """Make new DB file at path.""" + with sql_connect(path) as conn: with open(PATH_DB_SCHEMA, 'r', encoding='utf-8') as f: conn.executescript(f.read()) - self._check() + conn.execute(f'PRAGMA user_version = {EXPECTED_DB_VERSION}') + return cls(path) + + @classmethod + def migrate(cls, path: str) -> DatabaseFile: + """Apply migrations from_version to EXPECTED_DB_VERSION.""" + migrations = cls._available_migrations() + from_version = cls.get_version_of_db(path) + migrations_todo = migrations[from_version+1:] + for j, filename in enumerate(migrations_todo): + with sql_connect(path) as conn: + with open(f'{MIGRATIONS_DIR}/{filename}', 'r', + encoding='utf-8') as f: + conn.executescript(f.read()) + user_version = from_version + j + 1 + with sql_connect(path) as conn: + conn.execute(f'PRAGMA user_version = {user_version}') + return cls(path) def _check(self) -> None: """Check file exists, and is of proper DB version and schema.""" - self.exists = isfile(self.path) - if self.exists: - self._validate_user_version() - self._validate_schema() + if not isfile(self.path): + raise NotFoundException + if self.user_version != EXPECTED_DB_VERSION: + raise UnmigratedDbException() + self._validate_schema() + + @staticmethod + def _available_migrations() -> list[str]: + """Validate migrations directory and return sorted entries.""" + msg_too_big = 'Migration directory points beyond expected DB version.' + msg_bad_entry = 'Migration directory contains unexpected entry: ' + msg_missing = 'Migration directory misses migration of number: ' + migrations = {} + for entry in listdir(MIGRATIONS_DIR): + if entry == FILENAME_DB_SCHEMA: + continue + toks = entry.split('_', 1) + if len(toks) < 2: + raise HandledException(msg_bad_entry + entry) + try: + i = int(toks[0]) + except ValueError as e: + raise HandledException(msg_bad_entry + entry) from e + if i > EXPECTED_DB_VERSION: + raise HandledException(msg_too_big) + migrations[i] = toks[1] + migrations_list = [] + for i in range(EXPECTED_DB_VERSION + 1): + if i not in migrations: + raise HandledException(msg_missing + str(i)) + migrations_list += [f'{i}_{migrations[i]}'] + return migrations_list - def _validate_user_version(self) -> None: - """Compare DB user_version with EXPECTED_DB_VERSION.""" + @staticmethod + def get_version_of_db(path: str) -> int: + """Get DB user_version, fail if outside expected range.""" sql_for_db_version = 'PRAGMA user_version' - with sql_connect(self.path) as conn: + with sql_connect(path) as conn: db_version = list(conn.execute(sql_for_db_version))[0][0] - if db_version != EXPECTED_DB_VERSION: - msg = f'Wrong DB version, expected '\ - f'{EXPECTED_DB_VERSION}, got {db_version}.' - raise HandledException(msg) + if db_version > EXPECTED_DB_VERSION: + msg = f'Wrong DB version, expected '\ + f'{EXPECTED_DB_VERSION}, got unknown {db_version}.' + raise HandledException(msg) + assert isinstance(db_version, int) + return db_version + + @property + def user_version(self) -> int: + """Get DB user_version.""" + return self.__class__.get_version_of_db(self.path) def _validate_schema(self) -> None: """Compare found schema with what's stored at PATH_DB_SCHEMA.""" + + def reformat_rows(rows: list[str]) -> list[str]: + new_rows = [] + for row in rows: + new_row = [] + for subrow in row.split('\n'): + subrow = subrow.rstrip() + in_parentheses = 0 + split_at = [] + for i, c in enumerate(subrow): + if '(' == c: + in_parentheses += 1 + elif ')' == c: + in_parentheses -= 1 + elif ',' == c and 0 == in_parentheses: + split_at += [i + 1] + prev_split = 0 + for i in split_at: + segment = subrow[prev_split:i].strip() + if len(segment) > 0: + new_row += [f' {segment}'] + prev_split = i + segment = subrow[prev_split:].strip() + if len(segment) > 0: + new_row += [f' {segment}'] + new_row[0] = new_row[0].lstrip() + new_row[-1] = new_row[-1].lstrip() + if new_row[-1] != ')' and new_row[-3][-1] != ',': + new_row[-3] = new_row[-3] + ',' + new_row[-2:] = [' ' + new_row[-1][:-1]] + [')'] + new_rows += ['\n'.join(new_row)] + return new_rows + sql_for_schema = 'SELECT sql FROM sqlite_master ORDER BY sql' msg_err = 'Database has wrong tables schema. Diff:\n' with sql_connect(self.path) as conn: schema_rows = [r[0] for r in conn.execute(sql_for_schema) if r[0]] - retrieved_schema = ';\n'.join(schema_rows) + ';' - with open(PATH_DB_SCHEMA, 'r', encoding='utf-8') as f: - stored_schema = f.read().rstrip() - if stored_schema != retrieved_schema: - diff_msg = Differ().compare(retrieved_schema.splitlines(), - stored_schema.splitlines()) - raise HandledException(msg_err + '\n'.join(diff_msg)) + schema_rows = reformat_rows(schema_rows) + retrieved_schema = ';\n'.join(schema_rows) + ';' + with open(PATH_DB_SCHEMA, 'r', encoding='utf-8') as f: + stored_schema = f.read().rstrip() + if stored_schema != retrieved_schema: + diff_msg = Differ().compare(retrieved_schema.splitlines(), + stored_schema.splitlines()) + raise HandledException(msg_err + '\n'.join(diff_msg)) class DatabaseConnection: @@ -76,11 +172,17 @@ class DatabaseConnection: self.conn.close() def rewrite_relations(self, table_name: str, key: str, target: int | str, - rows: list[list[Any]]) -> None: - """Rewrite relations in table_name to target, with rows values.""" + rows: list[list[Any]], key_index: int = 0) -> None: + # pylint: disable=too-many-arguments + """Rewrite relations in table_name to target, with rows values. + + Note that single rows are expected without the column and value + identified by key and target, which are inserted inside the function + at key_index. + """ self.delete_where(table_name, key, target) for row in rows: - values = tuple([target] + row) + values = tuple(row[:key_index] + [target] + row[key_index:]) q_marks = self.__class__.q_marks_from_values(values) self.exec(f'INSERT INTO {table_name} VALUES {q_marks}', values) @@ -90,6 +192,17 @@ class DatabaseConnection: return list(self.exec(f'SELECT * FROM {table_name} WHERE {key} = ?', (target,))) + # def column_where_pattern(self, + # table_name: str, + # column: str, + # pattern: str, + # keys: list[str]) -> list[Any]: + # """Return column of rows where one of keys matches pattern.""" + # targets = tuple([f'%{pattern}%'] * len(keys)) + # haystack = ' OR '.join([f'{k} LIKE ?' for k in keys]) + # sql = f'SELECT {column} FROM {table_name} WHERE {haystack}' + # return [row[0] for row in self.exec(sql, targets)] + def column_where(self, table_name: str, column: str, key: str, target: int | str) -> list[Any]: """Return column of table where key == target.""" @@ -122,9 +235,10 @@ class BaseModel(Generic[BaseModelId]): table_name = '' to_save: list[str] = [] to_save_versioned: list[str] = [] - to_save_relations: list[tuple[str, str, str]] = [] + to_save_relations: list[tuple[str, str, str, int]] = [] id_: None | BaseModelId cache_: dict[BaseModelId, Self] + to_search: list[str] = [] def __init__(self, id_: BaseModelId | None) -> None: if isinstance(id_, int) and id_ < 1: @@ -247,6 +361,49 @@ 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]: + """Return all objects whose .to_search match pattern.""" + items = cls.all(db_conn) + if pattern: + filtered = [] + for item in items: + for attr_name in cls.to_search: + toks = attr_name.split('.') + parent = item + for tok in toks: + attr = getattr(parent, tok) + parent = attr + if pattern in attr: + filtered += [item] + break + return filtered + return items + def save(self, db_conn: DatabaseConnection) -> None: """Write self to DB and cache and ensure .id_. @@ -269,11 +426,11 @@ class BaseModel(Generic[BaseModelId]): self.cache() for attr_name in self.to_save_versioned: getattr(self, attr_name).save(db_conn) - for table, column, attr_name in self.to_save_relations: + for table, column, attr_name, key_index in self.to_save_relations: assert isinstance(self.id_, (int, str)) db_conn.rewrite_relations(table, column, self.id_, [[i.id_] for i - in getattr(self, attr_name)]) + in getattr(self, attr_name)], key_index) def remove(self, db_conn: DatabaseConnection) -> None: """Remove from DB and cache, including dependencies.""" @@ -281,7 +438,7 @@ class BaseModel(Generic[BaseModelId]): raise HandledException('cannot remove unsaved item') for attr_name in self.to_save_versioned: getattr(self, attr_name).remove(db_conn) - for table, column, attr_name in self.to_save_relations: + for table, column, attr_name, _ in self.to_save_relations: db_conn.delete_where(table, column, self.id_) self.uncache() db_conn.delete_where(self.table_name, 'id', self.id_) diff --git a/plomtask/http.py b/plomtask/http.py index adac957..583203e 100644 --- a/plomtask/http.py +++ b/plomtask/http.py @@ -1,21 +1,35 @@ """Web server stuff.""" -from typing import Any, NamedTuple +from __future__ import annotations +from dataclasses import dataclass +from typing import Any +from base64 import b64encode, b64decode from http.server import BaseHTTPRequestHandler from http.server import HTTPServer from urllib.parse import urlparse, parse_qs from os.path import split as path_split from jinja2 import Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader -from plomtask.days import Day, todays_date +from plomtask.dating import date_in_n_days +from plomtask.days import Day from plomtask.exceptions import HandledException, BadFormatException, \ NotFoundException from plomtask.db import DatabaseConnection, DatabaseFile -from plomtask.processes import Process +from plomtask.processes import Process, ProcessStep, ProcessStepsNode from plomtask.conditions import Condition from plomtask.todos import Todo TEMPLATES_DIR = 'templates' +@dataclass +class TodoStepsNode: + """Collect what's useful for Todo steps tree display.""" + id_: int + todo: Todo | None + process: Process | None + children: list[TodoStepsNode] + fillable: bool = False + + class TaskServer(HTTPServer): """Variant of HTTPServer that knows .jinja as Jinja Environment.""" @@ -43,6 +57,13 @@ class InputsParser: return default return self.inputs[key][0] + def get_first_strings_starting(self, prefix: str) -> dict[str, str]: + """Retrieve dict of (first) strings at key starting with prefix.""" + ret = {} + for key in [k for k in self.inputs.keys() if k.startswith(prefix)]: + ret[key] = self.inputs[key][0] + return ret + def get_int(self, key: str) -> int: """Retrieve single/first value of key as int, error if empty.""" val = self.get_int_or_none(key) @@ -88,105 +109,300 @@ class InputsParser: class TaskHandler(BaseHTTPRequestHandler): """Handles single HTTP request.""" + # pylint: disable=too-many-public-methods server: TaskServer + conn: DatabaseConnection + _site: str + _form_data: InputsParser + _params: InputsParser def do_GET(self) -> None: """Handle any GET request.""" try: self._init_handling() - if self.site in {'calendar', 'day', 'process', 'processes', 'todo', - 'condition', 'conditions'}: - template = f'{self.site}.html' - ctx = getattr(self, f'do_GET_{self.site}')() + if hasattr(self, f'do_GET_{self._site}'): + template = f'{self._site}.html' + ctx = getattr(self, f'do_GET_{self._site}')() html = self.server.jinja.get_template(template).render(**ctx) self._send_html(html) - elif '' == self.site: + elif '' == self._site: self._redirect('/day') else: - raise NotFoundException(f'Unknown page: /{self.site}') + raise NotFoundException(f'Unknown page: /{self._site}') except HandledException as error: self._send_msg(error, code=error.http_code) finally: self.conn.close() - def do_GET_calendar(self) -> dict[str, object]: + def _do_GET_calendar(self) -> dict[str, object]: """Show Days from ?start= to ?end=.""" - start = self.params.get_str('start') - end = self.params.get_str('end') - days = Day.all(self.conn, date_range=(start, end), fill_gaps=True) - return {'start': start, 'end': end, 'days': days} + start = self._params.get_str('start') + end = self._params.get_str('end') + if not end: + end = date_in_n_days(366) + ret = Day.by_date_range_with_limits(self.conn, (start, end), 'id') + days, start, end = ret + days = Day.with_filled_gaps(days, start, end) + for day in days: + day.collect_calendarized_todos(self.conn) + today = date_in_n_days(0) + return {'start': start, 'end': end, 'days': days, 'today': today} + + def do_GET_calendar(self) -> dict[str, object]: + """Show Days from ?start= to ?end= – normal view.""" + return self._do_GET_calendar() + + def do_GET_calendar_txt(self) -> dict[str, object]: + """Show Days from ?start= to ?end= – minimalist view.""" + return self._do_GET_calendar() def do_GET_day(self) -> dict[str, object]: """Show single Day of ?date=.""" - - class ConditionListing(NamedTuple): - """Listing of Condition augmented with its enablers, disablers.""" - condition: Condition - enablers: list[Todo] - disablers: list[Todo] - - date = self.params.get_str('date', todays_date()) - top_todos = [t for t in Todo.by_date(self.conn, date) if not t.parents] - todo_trees = [t.get_undone_steps_tree() for t in top_todos] - done_trees = [] - for t in top_todos: - done_trees += t.get_done_steps_tree() - condition_listings: list[ConditionListing] = [] - for cond in Condition.all(self.conn): - enablers = Todo.enablers_for_at(self.conn, cond, date) - disablers = Todo.disablers_for_at(self.conn, cond, date) - condition_listings += [ConditionListing(cond, enablers, disablers)] + date = self._params.get_str('date', date_in_n_days(0)) + make_type = self._params.get_str('make_type') + todays_todos = Todo.by_date(self.conn, date) + total_effort = 0.0 + for todo in todays_todos: + total_effort += todo.performed_effort + conditions_present = [] + enablers_for = {} + disablers_for = {} + for todo in todays_todos: + for condition in todo.conditions + todo.blockers: + if condition not in conditions_present: + conditions_present += [condition] + enablers_for[condition.id_] = [p for p in + Process.all(self.conn) + if condition in p.enables] + disablers_for[condition.id_] = [p for p in + Process.all(self.conn) + if condition in p.disables] + seen_todos: set[int] = set() + top_nodes = [t.get_step_tree(seen_todos) + for t in todays_todos if not t.parents] return {'day': Day.by_id(self.conn, date, create=True), - 'todo_trees': todo_trees, - 'done_trees': done_trees, - 'processes': Process.all(self.conn), - 'condition_listings': condition_listings} + 'total_effort': total_effort, + 'top_nodes': top_nodes, + 'make_type': make_type, + 'enablers_for': enablers_for, + 'disablers_for': disablers_for, + 'conditions_present': conditions_present, + 'processes': Process.all(self.conn)} def do_GET_todo(self) -> dict[str, object]: """Show single Todo of ?id=.""" - id_ = self.params.get_int('id') + + def walk_process_steps(id_: int, + process_step_nodes: list[ProcessStepsNode], + steps_nodes: list[TodoStepsNode]) -> None: + for process_step_node in process_step_nodes: + id_ += 1 + node = TodoStepsNode(id_, None, process_step_node.process, []) + steps_nodes += [node] + walk_process_steps(id_, list(process_step_node.steps.values()), + node.children) + + def walk_todo_steps(id_: int, todos: list[Todo], + steps_nodes: list[TodoStepsNode]) -> None: + for todo in todos: + matched = False + for match in [item for item in steps_nodes + if item.process + and item.process == todo.process]: + match.todo = todo + matched = True + for child in match.children: + child.fillable = True + walk_todo_steps(id_, todo.children, match.children) + if not matched: + id_ += 1 + node = TodoStepsNode(id_, todo, None, []) + steps_nodes += [node] + walk_todo_steps(id_, todo.children, node.children) + + def collect_adoptables_keys(steps_nodes: list[TodoStepsNode] + ) -> set[int]: + ids = set() + for node in steps_nodes: + if not node.todo: + assert isinstance(node.process, Process) + assert isinstance(node.process.id_, int) + ids.add(node.process.id_) + ids = ids | collect_adoptables_keys(node.children) + return ids + + id_ = self._params.get_int('id') todo = Todo.by_id(self.conn, id_) - return {'todo': todo, - 'todo_candidates': Todo.by_date(self.conn, todo.date), + todo_steps = [step.todo for step in todo.get_step_tree(set()).children] + process_tree = todo.process.get_steps(self.conn, None) + steps_todo_to_process: list[TodoStepsNode] = [] + walk_process_steps(0, list(process_tree.values()), + steps_todo_to_process) + for steps_node in steps_todo_to_process: + steps_node.fillable = True + walk_todo_steps(len(steps_todo_to_process), todo_steps, + steps_todo_to_process) + adoptables: dict[int, list[Todo]] = {} + any_adoptables = [Todo.by_id(self.conn, t.id_) + for t in Todo.by_date(self.conn, todo.date) + if t != todo] + for id_ in collect_adoptables_keys(steps_todo_to_process): + adoptables[id_] = [t for t in any_adoptables + if t.process.id_ == id_] + return {'todo': todo, 'steps_todo_to_process': steps_todo_to_process, + 'adoption_candidates_for': adoptables, + 'process_candidates': Process.all(self.conn), + 'todo_candidates': any_adoptables, 'condition_candidates': Condition.all(self.conn)} + def do_GET_todos(self) -> dict[str, object]: + """Show Todos from ?start= to ?end=, of ?process=, ?comment= pattern""" + sort_by = self._params.get_str('sort_by') + start = self._params.get_str('start') + end = self._params.get_str('end') + process_id = self._params.get_int_or_none('process_id') + comment_pattern = self._params.get_str('comment_pattern') + todos = [] + ret = Todo.by_date_range_with_limits(self.conn, (start, end)) + todos_by_date_range, start, end = ret + todos = [t for t in todos_by_date_range + if comment_pattern in t.comment + and ((not process_id) or t.process.id_ == process_id)] + if sort_by == 'doneness': + todos.sort(key=lambda t: t.is_done) + elif sort_by == '-doneness': + todos.sort(key=lambda t: t.is_done, reverse=True) + elif sort_by == 'title': + todos.sort(key=lambda t: t.title_then) + elif sort_by == '-title': + todos.sort(key=lambda t: t.title_then, reverse=True) + elif sort_by == 'comment': + todos.sort(key=lambda t: t.comment) + elif sort_by == '-comment': + todos.sort(key=lambda t: t.comment, reverse=True) + elif sort_by == '-date': + todos.sort(key=lambda t: t.date, reverse=True) + else: + todos.sort(key=lambda t: t.date) + return {'start': start, 'end': end, 'process_id': process_id, + 'comment_pattern': comment_pattern, 'todos': todos, + 'all_processes': Process.all(self.conn), 'sort_by': sort_by} + def do_GET_conditions(self) -> dict[str, object]: """Show all Conditions.""" - return {'conditions': Condition.all(self.conn)} + pattern = self._params.get_str('pattern') + conditions = Condition.matching(self.conn, pattern) + sort_by = self._params.get_str('sort_by') + if sort_by == 'is_active': + conditions.sort(key=lambda c: c.is_active) + elif sort_by == '-is_active': + conditions.sort(key=lambda c: c.is_active, reverse=True) + elif sort_by == '-title': + conditions.sort(key=lambda c: c.title.newest, reverse=True) + else: + conditions.sort(key=lambda c: c.title.newest) + return {'conditions': conditions, + 'sort_by': sort_by, + 'pattern': pattern} def do_GET_condition(self) -> dict[str, object]: """Show Condition of ?id=.""" - id_ = self.params.get_int_or_none('id') - return {'condition': Condition.by_id(self.conn, id_, create=True)} + id_ = self._params.get_int_or_none('id') + c = Condition.by_id(self.conn, id_, create=True) + ps = Process.all(self.conn) + return {'condition': c, 'is_new': c.id_ is None, + 'enabled_processes': [p for p in ps if c in p.conditions], + 'disabled_processes': [p for p in ps if c in p.blockers], + 'enabling_processes': [p for p in ps if c in p.enables], + 'disabling_processes': [p for p in ps if c in p.disables]} + + def do_GET_condition_titles(self) -> dict[str, object]: + """Show title history of Condition of ?id=.""" + id_ = self._params.get_int_or_none('id') + condition = Condition.by_id(self.conn, id_) + return {'condition': condition} + + def do_GET_condition_descriptions(self) -> dict[str, object]: + """Show description historys of Condition of ?id=.""" + id_ = self._params.get_int_or_none('id') + condition = Condition.by_id(self.conn, id_) + return {'condition': condition} def do_GET_process(self) -> dict[str, object]: - """Show process of ?id=.""" - id_ = self.params.get_int_or_none('id') + """Show Process of ?id=.""" + id_ = self._params.get_int_or_none('id') process = Process.by_id(self.conn, id_, create=True) - return {'process': process, - 'steps': process.get_steps(self.conn), - 'owners': process.used_as_step_by(self.conn), - 'step_candidates': Process.all(self.conn), + title_64 = self._params.get_str('title_b64') + if title_64: + title = b64decode(title_64.encode()).decode() + process.title.set(title) + owners = process.used_as_step_by(self.conn) + for step_id in self._params.get_all_int('step_to'): + owners += [Process.by_id(self.conn, step_id)] + preset_top_step = None + for process_id in self._params.get_all_int('has_step'): + preset_top_step = process_id + return {'process': process, 'is_new': process.id_ is None, + 'preset_top_step': preset_top_step, + 'steps': process.get_steps(self.conn), 'owners': owners, + 'n_todos': len(Todo.by_process_id(self.conn, process.id_)), + 'process_candidates': Process.all(self.conn), 'condition_candidates': Condition.all(self.conn)} + def do_GET_process_titles(self) -> dict[str, object]: + """Show title history of Process of ?id=.""" + id_ = self._params.get_int_or_none('id') + process = Process.by_id(self.conn, id_) + return {'process': process} + + def do_GET_process_descriptions(self) -> dict[str, object]: + """Show description historys of Process of ?id=.""" + id_ = self._params.get_int_or_none('id') + process = Process.by_id(self.conn, id_) + return {'process': process} + + def do_GET_process_efforts(self) -> dict[str, object]: + """Show default effort history of Process of ?id=.""" + id_ = self._params.get_int_or_none('id') + process = Process.by_id(self.conn, id_) + return {'process': process} + def do_GET_processes(self) -> dict[str, object]: """Show all Processes.""" - return {'processes': Process.all(self.conn)} + pattern = self._params.get_str('pattern') + processes = Process.matching(self.conn, pattern) + sort_by = self._params.get_str('sort_by') + if sort_by == 'steps': + processes.sort(key=lambda p: len(p.explicit_steps)) + elif sort_by == '-steps': + processes.sort(key=lambda p: len(p.explicit_steps), reverse=True) + elif sort_by == 'owners': + processes.sort(key=lambda p: p.n_owners or 0) + elif sort_by == '-owners': + processes.sort(key=lambda p: p.n_owners or 0, reverse=True) + elif sort_by == 'effort': + processes.sort(key=lambda p: p.effort.newest) + elif sort_by == '-effort': + processes.sort(key=lambda p: p.effort.newest, reverse=True) + elif sort_by == '-title': + processes.sort(key=lambda p: p.title.newest, reverse=True) + else: + processes.sort(key=lambda p: p.title.newest) + return {'processes': processes, 'sort_by': sort_by, 'pattern': pattern} def do_POST(self) -> None: """Handle any POST request.""" - # pylint: disable=attribute-defined-outside-init try: self._init_handling() length = int(self.headers['content-length']) postvars = parse_qs(self.rfile.read(length).decode(), keep_blank_values=True, strict_parsing=True) - self.form_data = InputsParser(postvars) - if self.site in ('day', 'process', 'todo', 'condition'): - redir_target = getattr(self, f'do_POST_{self.site}')() + self._form_data = InputsParser(postvars) + if hasattr(self, f'do_POST_{self._site}'): + redir_target = getattr(self, f'do_POST_{self._site}')() self.conn.commit() else: - msg = f'Page not known as POST target: /{self.site}' + msg = f'Page not known as POST target: /{self._site}' raise NotFoundException(msg) self._redirect(redir_target) except HandledException as error: @@ -196,51 +412,87 @@ class TaskHandler(BaseHTTPRequestHandler): def do_POST_day(self) -> str: """Update or insert Day of date and Todos mapped to it.""" - date = self.params.get_str('date') + date = self._params.get_str('date') day = Day.by_id(self.conn, date, create=True) - day.comment = self.form_data.get_str('comment') + day.comment = self._form_data.get_str('day_comment') day.save(self.conn) - existing_todos = Todo.by_date(self.conn, date) - for process_id in self.form_data.get_all_int('new_todo'): - process = Process.by_id(self.conn, process_id) - todo = Todo(None, process, False, day.date) - todo.save(self.conn) - todo.adopt_from(existing_todos) - todo.make_missing_children(self.conn) - todo.save(self.conn) - for todo_id in self.form_data.get_all_int('done'): + make_type = self._form_data.get_str('make_type') + for process_id in sorted(self._form_data.get_all_int('new_todo')): + if 'empty' == make_type: + process = Process.by_id(self.conn, process_id) + todo = Todo(None, process, False, date) + todo.save(self.conn) + else: + Todo.create_with_children(self.conn, process_id, date) + done_ids = self._form_data.get_all_int('done') + comments = self._form_data.get_all_str('comment') + efforts = self._form_data.get_all_str('effort') + for i, todo_id in enumerate(self._form_data.get_all_int('todo_id')): todo = Todo.by_id(self.conn, todo_id) - todo.is_done = True + todo.is_done = todo_id in done_ids + if len(comments) > 0: + todo.comment = comments[i] + if len(efforts) > 0: + todo.effort = float(efforts[i]) if efforts[i] else None todo.save(self.conn) for condition in todo.enables: condition.save(self.conn) for condition in todo.disables: condition.save(self.conn) - return f'/day?date={date}' + return f'/day?date={date}&make_type={make_type}' def do_POST_todo(self) -> str: """Update Todo and its children.""" - id_ = self.params.get_int('id') - for _ in self.form_data.get_all_str('delete'): + # pylint: disable=too-many-locals + # pylint: disable=too-many-branches + id_ = self._params.get_int('id') + for _ in self._form_data.get_all_str('delete'): todo = Todo .by_id(self.conn, id_) todo.remove(self.conn) return '/' todo = Todo.by_id(self.conn, id_) - adopted_child_ids = self.form_data.get_all_int('adopt') + adopted_child_ids = self._form_data.get_all_int('adopt') + processes_to_make_full = self._form_data.get_all_int('make_full') + processes_to_make_empty = self._form_data.get_all_int('make_empty') + fill_fors = self._form_data.get_first_strings_starting('fill_for_') + for v in fill_fors.values(): + if v.startswith('make_empty_'): + processes_to_make_empty += [int(v[11:])] + elif v.startswith('make_full_'): + processes_to_make_full += [int(v[10:])] + elif v != 'ignore': + adopted_child_ids += [int(v)] + to_remove = [] for child in todo.children: + assert isinstance(child.id_, int) if child.id_ not in adopted_child_ids: - assert isinstance(child.id_, int) - child = Todo.by_id(self.conn, child.id_) - todo.remove_child(child) + to_remove += [child.id_] + for id_ in to_remove: + child = Todo.by_id(self.conn, id_) + todo.remove_child(child) for child_id in adopted_child_ids: if child_id in [c.id_ for c in todo.children]: continue child = Todo.by_id(self.conn, child_id) todo.add_child(child) - todo.set_conditions(self.conn, self.form_data.get_all_int('condition')) - todo.set_enables(self.conn, self.form_data.get_all_int('enables')) - todo.set_disables(self.conn, self.form_data.get_all_int('disables')) - todo.is_done = len(self.form_data.get_all_str('done')) > 0 + for process_id in processes_to_make_empty: + process = Process.by_id(self.conn, process_id) + made = Todo(None, process, False, todo.date) + made.save(self.conn) + todo.add_child(made) + for process_id in processes_to_make_full: + made = Todo.create_with_children(self.conn, process_id, todo.date) + todo.add_child(made) + effort = self._form_data.get_str('effort', ignore_strict=True) + todo.effort = float(effort) if effort else None + todo.set_conditions(self.conn, + self._form_data.get_all_int('condition')) + todo.set_blockers(self.conn, self._form_data.get_all_int('blocker')) + todo.set_enables(self.conn, self._form_data.get_all_int('enables')) + todo.set_disables(self.conn, self._form_data.get_all_int('disables')) + todo.is_done = len(self._form_data.get_all_str('done')) > 0 + todo.calendarize = len(self._form_data.get_all_str('calendarize')) > 0 + todo.comment = self._form_data.get_str('comment', ignore_strict=True) todo.save(self.conn) for condition in todo.enables: condition.save(self.conn) @@ -248,64 +500,133 @@ class TaskHandler(BaseHTTPRequestHandler): condition.save(self.conn) return f'/todo?id={todo.id_}' + def _do_POST_versioned_timestamps(self, cls: Any, attr_name: str) -> str: + """Update history timestamps for VersionedAttribute.""" + id_ = self._params.get_int_or_none('id') + item = cls.by_id(self.conn, id_) + attr = getattr(item, attr_name) + for k, v in self._form_data.get_first_strings_starting('at:').items(): + old = k[3:] + if old[19:] != v: + attr.reset_timestamp(old, f'{v}.0') + attr.save(self.conn) + cls_name = cls.__name__.lower() + return f'/{cls_name}_{attr_name}s?id={item.id_}' + + def do_POST_process_descriptions(self) -> str: + """Update history timestamps for Process.description.""" + return self._do_POST_versioned_timestamps(Process, 'description') + + def do_POST_process_efforts(self) -> str: + """Update history timestamps for Process.effort.""" + return self._do_POST_versioned_timestamps(Process, 'effort') + + def do_POST_process_titles(self) -> str: + """Update history timestamps for Process.title.""" + return self._do_POST_versioned_timestamps(Process, 'title') + def do_POST_process(self) -> str: """Update or insert Process of ?id= and fields defined in postvars.""" - id_ = self.params.get_int_or_none('id') - for _ in self.form_data.get_all_str('delete'): + # pylint: disable=too-many-branches + id_ = self._params.get_int_or_none('id') + for _ in self._form_data.get_all_str('delete'): process = Process.by_id(self.conn, id_) process.remove(self.conn) return '/processes' process = Process.by_id(self.conn, id_, create=True) - process.title.set(self.form_data.get_str('title')) - process.description.set(self.form_data.get_str('description')) - process.effort.set(self.form_data.get_float('effort')) + process.title.set(self._form_data.get_str('title')) + process.description.set(self._form_data.get_str('description')) + process.effort.set(self._form_data.get_float('effort')) process.set_conditions(self.conn, - self.form_data.get_all_int('condition')) - process.set_enables(self.conn, self.form_data.get_all_int('enables')) - process.set_disables(self.conn, self.form_data.get_all_int('disables')) + self._form_data.get_all_int('condition')) + process.set_blockers(self.conn, self._form_data.get_all_int('blocker')) + process.set_enables(self.conn, self._form_data.get_all_int('enables')) + process.set_disables(self.conn, + self._form_data.get_all_int('disables')) + process.calendarize = self._form_data.get_all_str('calendarize') != [] process.save(self.conn) - process.explicit_steps = [] - steps: list[tuple[int | None, int, int | None]] = [] - for step_id in self.form_data.get_all_int('steps'): - for step_process_id in self.form_data.get_all_int( - f'new_step_to_{step_id}'): - steps += [(None, step_process_id, step_id)] - if step_id not in self.form_data.get_all_int('keep_step'): + assert isinstance(process.id_, int) + steps: list[ProcessStep] = [] + for step_id in self._form_data.get_all_int('keep_step'): + if step_id not in self._form_data.get_all_int('steps'): + raise BadFormatException('trying to keep unknown step') + for step_id in self._form_data.get_all_int('steps'): + if step_id not in self._form_data.get_all_int('keep_step'): continue - step_process_id = self.form_data.get_int( + step_process_id = self._form_data.get_int( f'step_{step_id}_process_id') - parent_id = self.form_data.get_int_or_none( + parent_id = self._form_data.get_int_or_none( f'step_{step_id}_parent_id') - steps += [(step_id, step_process_id, parent_id)] - for step_process_id in self.form_data.get_all_int('new_top_step'): - steps += [(None, step_process_id, None)] + steps += [ProcessStep(step_id, process.id_, step_process_id, + parent_id)] + for step_id in self._form_data.get_all_int('steps'): + for step_process_id in self._form_data.get_all_int( + f'new_step_to_{step_id}'): + steps += [ProcessStep(None, process.id_, step_process_id, + step_id)] + new_step_title = None + for step_identifier in self._form_data.get_all_str('new_top_step'): + try: + step_process_id = int(step_identifier) + steps += [ProcessStep(None, process.id_, step_process_id, + None)] + except ValueError: + new_step_title = step_identifier + process.uncache() process.set_steps(self.conn, steps) + process.set_step_suppressions(self.conn, + self._form_data. + get_all_int('suppresses')) process.save(self.conn) - return f'/process?id={process.id_}' + owners_to_set = [] + new_owner_title = None + for owner_identifier in self._form_data.get_all_str('step_of'): + try: + owners_to_set += [int(owner_identifier)] + except ValueError: + new_owner_title = owner_identifier + process.set_owners(self.conn, owners_to_set) + params = f'id={process.id_}' + if new_step_title: + title_b64_encoded = b64encode(new_step_title.encode()).decode() + params = f'step_to={process.id_}&title_b64={title_b64_encoded}' + elif new_owner_title: + title_b64_encoded = b64encode(new_owner_title.encode()).decode() + params = f'has_step={process.id_}&title_b64={title_b64_encoded}' + return f'/process?{params}' + + def do_POST_condition_descriptions(self) -> str: + """Update history timestamps for Condition.description.""" + return self._do_POST_versioned_timestamps(Condition, 'description') + + def do_POST_condition_titles(self) -> str: + """Update history timestamps for Condition.title.""" + return self._do_POST_versioned_timestamps(Condition, 'title') def do_POST_condition(self) -> str: """Update/insert Condition of ?id= and fields defined in postvars.""" - id_ = self.params.get_int_or_none('id') - for _ in self.form_data.get_all_str('delete'): + id_ = self._params.get_int_or_none('id') + for _ in self._form_data.get_all_str('delete'): condition = Condition.by_id(self.conn, id_) condition.remove(self.conn) return '/conditions' condition = Condition.by_id(self.conn, id_, create=True) - condition.is_active = self.form_data.get_all_str('is_active') != [] - condition.title.set(self.form_data.get_str('title')) - condition.description.set(self.form_data.get_str('description')) + condition.is_active = self._form_data.get_all_str('is_active') != [] + condition.title.set(self._form_data.get_str('title')) + condition.description.set(self._form_data.get_str('description')) condition.save(self.conn) return f'/condition?id={condition.id_}' def _init_handling(self) -> None: - # pylint: disable=attribute-defined-outside-init + """Our own __init__, as we're not supposed to use the original.""" self.conn = DatabaseConnection(self.server.db) parsed_url = urlparse(self.path) - self.site = path_split(parsed_url.path)[1] + self._site = path_split(parsed_url.path)[1] params = parse_qs(parsed_url.query, strict_parsing=True) - self.params = InputsParser(params, False) + self._params = InputsParser(params, False) def _redirect(self, target: str) -> None: + """Redirect to target.""" self.send_response(302) self.send_header('Location', target) self.end_headers() diff --git a/plomtask/processes.py b/plomtask/processes.py index 1778e4f..8082c3c 100644 --- a/plomtask/processes.py +++ b/plomtask/processes.py @@ -17,33 +17,40 @@ class ProcessStepsNode: parent_id: int | None is_explicit: bool steps: dict[int, ProcessStepsNode] - seen: bool + seen: bool = False + is_suppressed: bool = False class Process(BaseModel[int], ConditionsRelations): """Template for, and metadata for, Todos, and their arrangements.""" + # pylint: disable=too-many-instance-attributes table_name = 'processes' + to_save = ['calendarize'] to_save_versioned = ['title', 'description', 'effort'] - to_save_relations = [('process_conditions', 'process', 'conditions'), - ('process_enables', 'process', 'enables'), - ('process_disables', 'process', 'disables')] - - # pylint: disable=too-many-instance-attributes - - def __init__(self, id_: int | None) -> None: - super().__init__(id_) + to_save_relations = [('process_conditions', 'process', 'conditions', 0), + ('process_blockers', 'process', 'blockers', 0), + ('process_enables', 'process', 'enables', 0), + ('process_disables', 'process', 'disables', 0), + ('process_step_suppressions', 'process', + 'suppressed_steps', 0)] + to_search = ['title.newest', 'description.newest'] + + def __init__(self, id_: int | None, calendarize: bool = False) -> None: + BaseModel.__init__(self, id_) + ConditionsRelations.__init__(self) self.title = VersionedAttribute(self, 'process_titles', 'UNNAMED') self.description = VersionedAttribute(self, 'process_descriptions', '') self.effort = VersionedAttribute(self, 'process_efforts', 1.0) self.explicit_steps: list[ProcessStep] = [] - self.conditions: list[Condition] = [] - self.enables: list[Condition] = [] - self.disables: list[Condition] = [] + self.suppressed_steps: list[ProcessStep] = [] + self.calendarize = calendarize + self.n_owners: int | None = None # only set by from_table_row @classmethod def from_table_row(cls, db_conn: DatabaseConnection, row: Row | list[Any]) -> Process: """Make from DB row, with dependencies.""" + # pylint: disable=no-member process = super().from_table_row(db_conn, row) assert isinstance(process.id_, int) for name in ('title', 'description', 'effort'): @@ -53,14 +60,19 @@ class Process(BaseModel[int], ConditionsRelations): for row_ in db_conn.row_where('process_steps', 'owner', process.id_): step = ProcessStep.from_table_row(db_conn, row_) - process.explicit_steps += [step] # pylint: disable=no-member - for name in ('conditions', 'enables', 'disables'): + process.explicit_steps += [step] + for row_ in db_conn.row_where('process_step_suppressions', 'process', + process.id_): + step = ProcessStep.by_id(db_conn, row_[1]) + process.suppressed_steps += [step] + for name in ('conditions', 'blockers', 'enables', 'disables'): table = f'process_{name}' assert isinstance(process.id_, int) for c_id in db_conn.column_where(table, 'condition', 'process', process.id_): target = getattr(process, name) target += [Condition.by_id(db_conn, c_id)] + process.n_owners = len(process.used_as_step_by(db_conn)) return process def used_as_step_by(self, db_conn: DatabaseConnection) -> list[Process]: @@ -77,23 +89,27 @@ class Process(BaseModel[int], ConditionsRelations): Process | None = None) -> dict[int, ProcessStepsNode]: """Return tree of depended-on explicit and implicit ProcessSteps.""" - def make_node(step: ProcessStep) -> ProcessStepsNode: + def make_node(step: ProcessStep, suppressed: bool) -> ProcessStepsNode: is_explicit = False if external_owner is not None: is_explicit = step.owner_id == external_owner.id_ process = self.__class__.by_id(db_conn, step.step_process_id) - step_steps = process.get_steps(db_conn, external_owner) + step_steps = {} + if not suppressed: + step_steps = process.get_steps(db_conn, external_owner) return ProcessStepsNode(process, step.parent_step_id, - is_explicit, step_steps, False) + is_explicit, step_steps, False, suppressed) def walk_steps(node_id: int, node: ProcessStepsNode) -> None: + node.seen = node_id in seen_step_ids + seen_step_ids.add(node_id) + if node.is_suppressed: + return explicit_children = [s for s in self.explicit_steps if s.parent_step_id == node_id] for child in explicit_children: assert isinstance(child.id_, int) - node.steps[child.id_] = make_node(child) - node.seen = node_id in seen_step_ids - seen_step_ids.add(node_id) + node.steps[child.id_] = make_node(child, False) for id_, step in node.steps.items(): walk_steps(id_, step) @@ -104,26 +120,27 @@ class Process(BaseModel[int], ConditionsRelations): for step in [s for s in self.explicit_steps if s.parent_step_id is None]: assert isinstance(step.id_, int) - steps[step.id_] = make_node(step) + new_node = make_node(step, step in external_owner.suppressed_steps) + steps[step.id_] = new_node for step_id, step_node in steps.items(): walk_steps(step_id, step_node) return steps - def _add_step(self, - db_conn: DatabaseConnection, - id_: int | None, - step_process_id: int, - parent_step_id: int | None) -> ProcessStep: - """Create new ProcessStep, save and add it to self.explicit_steps. + def set_step_suppressions(self, db_conn: DatabaseConnection, + step_ids: list[int]) -> None: + """Set self.suppressed_steps from step_ids.""" + assert isinstance(self.id_, int) + db_conn.delete_where('process_step_suppressions', 'process', self.id_) + self.suppressed_steps = [ProcessStep.by_id(db_conn, s) + for s in step_ids] - Also checks against step recursion. + def set_steps(self, db_conn: DatabaseConnection, + steps: list[ProcessStep]) -> None: + """Set self.explicit_steps in bulk. - The new step's parent_step_id will fall back to None either if no - matching ProcessStep is found (which can be assumed in case it was - just deleted under its feet), or if the parent step would not be - owned by the current Process. + Checks against recursion, and turns into top-level steps any of + unknown or non-owned parent. """ - def walk_steps(node: ProcessStep) -> None: if node.step_process_id == self.id_: raise BadFormatException('bad step selection causes recursion') @@ -131,31 +148,44 @@ class Process(BaseModel[int], ConditionsRelations): for step in step_process.explicit_steps: walk_steps(step) - if parent_step_id is not None: - try: - parent_step = ProcessStep.by_id(db_conn, parent_step_id) - if parent_step.owner_id != self.id_: - parent_step_id = None - except NotFoundException: - parent_step_id = None - assert isinstance(self.id_, int) - step = ProcessStep(id_, self.id_, step_process_id, parent_step_id) - walk_steps(step) - self.explicit_steps += [step] - step.save(db_conn) # NB: This ensures a non-None step.id_. - return step - - def set_steps(self, db_conn: DatabaseConnection, - steps: list[tuple[int | None, int, int | None]]) -> None: - """Set self.explicit_steps in bulk.""" assert isinstance(self.id_, int) for step in self.explicit_steps: step.uncache() self.explicit_steps = [] db_conn.delete_where('process_steps', 'owner', self.id_) - for step_tuple in steps: - self._add_step(db_conn, step_tuple[0], - step_tuple[1], step_tuple[2]) + for step in steps: + step.save(db_conn) + if step.parent_step_id is not None: + try: + parent_step = ProcessStep.by_id(db_conn, + step.parent_step_id) + if parent_step.owner_id != self.id_: + step.parent_step_id = None + except NotFoundException: + step.parent_step_id = None + walk_steps(step) + self.explicit_steps += [step] + + def set_owners(self, db_conn: DatabaseConnection, + owner_ids: list[int]) -> None: + """Re-set owners to those identified in owner_ids.""" + owners_old = self.used_as_step_by(db_conn) + losers = [o for o in owners_old if o.id_ not in owner_ids] + owners_old_ids = [o.id_ for o in owners_old] + winners = [Process.by_id(db_conn, id_) for id_ in owner_ids + if id_ not in owners_old_ids] + steps_to_remove = [] + for loser in losers: + steps_to_remove += [s for s in loser.explicit_steps + if s.step_process_id == self.id_] + for step in steps_to_remove: + step.remove(db_conn) + for winner in winners: + assert isinstance(winner.id_, int) + assert isinstance(self.id_, int) + new_step = ProcessStep(None, winner.id_, self.id_, None) + new_explicit_steps = winner.explicit_steps + [new_step] + winner.set_steps(db_conn, new_explicit_steps) def save(self, db_conn: DatabaseConnection) -> None: """Add (or re-write) self and connected items to DB.""" diff --git a/plomtask/todos.py b/plomtask/todos.py index fcb8617..0125b97 100644 --- a/plomtask/todos.py +++ b/plomtask/todos.py @@ -4,51 +4,117 @@ from dataclasses import dataclass from typing import Any from sqlite3 import Row from plomtask.db import DatabaseConnection, BaseModel -from plomtask.processes import Process +from plomtask.processes import Process, ProcessStepsNode +from plomtask.versioned_attributes import VersionedAttribute from plomtask.conditions import Condition, ConditionsRelations from plomtask.exceptions import (NotFoundException, BadFormatException, HandledException) +from plomtask.dating import valid_date @dataclass -class TodoStepsNode: +class TodoNode: """Collects what's useful to know for Todo/Condition tree display.""" - item: Todo | Condition - is_todo: bool - children: list[TodoStepsNode] + todo: Todo seen: bool - hide: bool + children: list[TodoNode] class Todo(BaseModel[int], ConditionsRelations): """Individual actionable.""" # pylint: disable=too-many-instance-attributes table_name = 'todos' - to_save = ['process_id', 'is_done', 'date'] - to_save_relations = [('todo_conditions', 'todo', 'conditions'), - ('todo_enables', 'todo', 'enables'), - ('todo_disables', 'todo', 'disables'), - ('todo_children', 'parent', 'children'), - ('todo_children', 'child', 'parents')] - - def __init__(self, id_: int | None, process: Process, - is_done: bool, date: str) -> None: - super().__init__(id_) + to_save = ['process_id', 'is_done', 'date', 'comment', 'effort', + 'calendarize'] + to_save_relations = [('todo_conditions', 'todo', 'conditions', 0), + ('todo_blockers', 'todo', 'blockers', 0), + ('todo_enables', 'todo', 'enables', 0), + ('todo_disables', 'todo', 'disables', 0), + ('todo_children', 'parent', 'children', 0), + ('todo_children', 'child', 'parents', 1)] + to_search = ['comment'] + + # pylint: disable=too-many-arguments + def __init__(self, id_: int | None, + process: Process, + is_done: bool, + date: str, comment: str = '', + effort: None | float = None, + calendarize: bool = False) -> None: + BaseModel.__init__(self, id_) + ConditionsRelations.__init__(self) if process.id_ is None: raise NotFoundException('Process of Todo without ID (not saved?)') self.process = process self._is_done = is_done - self.date = date + self.date = valid_date(date) + self.comment = comment + self.effort = effort self.children: list[Todo] = [] self.parents: list[Todo] = [] - self.conditions: list[Condition] = [] - self.enables: list[Condition] = [] - self.disables: list[Condition] = [] + self.calendarize = calendarize if not self.id_: + self.calendarize = self.process.calendarize self.conditions = self.process.conditions[:] + self.blockers = self.process.blockers[:] self.enables = self.process.enables[:] self.disables = self.process.disables[:] + @classmethod + def by_date_range(cls, db_conn: DatabaseConnection, + date_range: tuple[str, str] = ('', '')) -> list[Todo]: + """Collect Todos of Days within date_range.""" + todos, _, _ = cls.by_date_range_with_limits(db_conn, date_range) + return todos + + @classmethod + def create_with_children(cls, db_conn: DatabaseConnection, + process_id: int, date: str) -> Todo: + """Create Todo of process for date, ensure children.""" + + def key_order_func(n: ProcessStepsNode) -> int: + assert isinstance(n.process.id_, int) + return n.process.id_ + + def walk_steps(parent: Todo, step_node: ProcessStepsNode) -> Todo: + adoptables = [t for t in cls.by_date(db_conn, date) + if (t not in parent.children) + and (t != parent) + and step_node.process == t.process] + satisfier = None + for adoptable in adoptables: + satisfier = adoptable + break + if not satisfier: + satisfier = cls(None, step_node.process, False, date) + satisfier.save(db_conn) + sub_step_nodes = list(step_node.steps.values()) + sub_step_nodes.sort(key=key_order_func) + for sub_node in sub_step_nodes: + if sub_node.is_suppressed: + continue + n_slots = len([n for n in sub_step_nodes + if n.process == sub_node.process]) + filled_slots = len([t for t in satisfier.children + if t.process == sub_node.process]) + # if we did not newly create satisfier, it may already fill + # some step dependencies, so only fill what remains open + if n_slots - filled_slots > 0: + satisfier.add_child(walk_steps(satisfier, sub_node)) + satisfier.save(db_conn) + return satisfier + + process = Process.by_id(db_conn, process_id) + todo = cls(None, process, False, date) + todo.save(db_conn) + steps_tree = process.get_steps(db_conn) + for step_node in steps_tree.values(): + if step_node.is_suppressed: + continue + todo.add_child(walk_steps(todo, step_node)) + todo.save(db_conn) + return todo + @classmethod def from_table_row(cls, db_conn: DatabaseConnection, row: Row | list[Any]) -> Todo: @@ -68,7 +134,7 @@ class Todo(BaseModel[int], ConditionsRelations): 'child', todo.id_): # pylint: disable=no-member todo.parents += [cls.by_id(db_conn, t_id)] - for name in ('conditions', 'enables', 'disables'): + for name in ('conditions', 'blockers', 'enables', 'disables'): table = f'todo_{name}' assert isinstance(todo.id_, int) for cond_id in db_conn.column_where(table, 'condition', @@ -78,37 +144,15 @@ class Todo(BaseModel[int], ConditionsRelations): return todo @classmethod - def by_date(cls, db_conn: DatabaseConnection, date: str) -> list[Todo]: - """Collect all Todos for Day of date.""" - todos = [] - for id_ in db_conn.column_where('todos', 'id', 'day', date): - todos += [cls.by_id(db_conn, id_)] - return todos - - @staticmethod - def _x_ablers_for_at(db_conn: DatabaseConnection, name: str, - cond: Condition, date: str) -> list[Todo]: - """Collect all Todos of day that [name] condition.""" - assert isinstance(cond.id_, int) - x_ablers = [] - table = f'todo_{name}' - for id_ in db_conn.column_where(table, 'todo', 'condition', cond.id_): - todo = Todo.by_id(db_conn, id_) - if todo.date == date: - x_ablers += [todo] - return x_ablers - - @classmethod - def enablers_for_at(cls, db_conn: DatabaseConnection, - condition: Condition, date: str) -> list[Todo]: - """Collect all Todos of day that enable condition.""" - return cls._x_ablers_for_at(db_conn, 'enables', condition, date) + def by_process_id(cls, db_conn: DatabaseConnection, + process_id: int | None) -> list[Todo]: + """Collect all Todos of Process of process_id.""" + return [t for t in cls.all(db_conn) if t.process.id_ == process_id] @classmethod - def disablers_for_at(cls, db_conn: DatabaseConnection, - condition: Condition, date: str) -> list[Todo]: - """Collect all Todos of day that disable condition.""" - return cls._x_ablers_for_at(db_conn, 'disables', condition, date) + def by_date(cls, db_conn: DatabaseConnection, date: str) -> list[Todo]: + """Collect all Todos for Day of date.""" + return cls.by_date_range(db_conn, (date, date)) @property def is_doable(self) -> bool: @@ -119,22 +163,33 @@ class Todo(BaseModel[int], ConditionsRelations): for condition in self.conditions: if not condition.is_active: return False + for condition in self.blockers: + if condition.is_active: + return False return True @property - def process_id(self) -> int | str | None: - """Return ID of tasked Process.""" - return self.process.id_ + def is_deletable(self) -> bool: + """Decide whether self be deletable (not if preserve-worthy values).""" + if self.comment: + return False + if self.effort and self.effort >= 0: + return False + return True + + @property + def performed_effort(self) -> float: + """Return performed effort, i.e. self.effort or default if done..""" + if self.effort is not None: + return self.effort + if self.is_done: + return self.effort_then + return 0 @property - def unsatisfied_dependencies(self) -> list[int]: - """Return Process IDs of .process.explicit_steps not in .children.""" - unsatisfied = [s.step_process_id for s in self.process.explicit_steps - if s.parent_step_id is None] - for child_process_id in [c.process.id_ for c in self.children]: - if child_process_id in unsatisfied: - unsatisfied.remove(child_process_id) - return unsatisfied + def process_id(self) -> int | str | None: + """Needed for super().save to save Processes as attributes.""" + return self.process.id_ @property def is_done(self) -> bool: @@ -153,85 +208,62 @@ class Todo(BaseModel[int], ConditionsRelations): for condition in self.disables: condition.is_active = False - def adopt_from(self, todos: list[Todo]) -> None: - """As far as possible, fill unsatisfied dependencies from todos.""" - for process_id in self.unsatisfied_dependencies: - for todo in [t for t in todos if t.process.id_ == process_id - and t not in self.children]: - self.add_child(todo) - break + @property + def title(self) -> VersionedAttribute: + """Shortcut to .process.title.""" + return self.process.title - def make_missing_children(self, db_conn: DatabaseConnection) -> None: - """Fill unsatisfied dependencies with new Todos.""" - for process_id in self.unsatisfied_dependencies: - process = Process.by_id(db_conn, process_id) - todo = self.__class__(None, process, False, self.date) - todo.save(db_conn) - self.add_child(todo) - - def get_step_tree(self, seen_todos: set[int], - seen_conditions: set[int]) -> TodoStepsNode: - """Return tree of depended-on Todos and Conditions.""" - - def make_node(step: Todo | Condition) -> TodoStepsNode: - assert isinstance(step.id_, int) - is_todo = isinstance(step, Todo) + @property + def title_then(self) -> str: + """Shortcut to .process.title.at(self.date)""" + title_then = self.process.title.at(self.date) + assert isinstance(title_then, str) + return title_then + + @property + def effort_then(self) -> float: + """Shortcut to .process.effort.at(self.date)""" + effort_then = self.process.effort.at(self.date) + assert isinstance(effort_then, float) + return effort_then + + @property + def has_doneness_in_path(self) -> bool: + """Check whether self is done or has any children that are.""" + if self.is_done: + return True + for child in self.children: + if child.is_done: + return True + if child.has_doneness_in_path: + return True + return False + + def get_step_tree(self, seen_todos: set[int]) -> TodoNode: + """Return tree of depended-on Todos.""" + + def make_node(todo: Todo) -> TodoNode: children = [] - if is_todo: - assert isinstance(step, Todo) - seen = step.id_ in seen_todos - seen_todos.add(step.id_) - potentially_enabled = set() - for child in step.children: - for condition in child.enables: - potentially_enabled.add(condition.id_) - children += [make_node(child)] - for condition in [c for c in step.conditions - if (not c.is_active) - and (c.id_ not in potentially_enabled)]: - children += [make_node(condition)] - else: - seen = step.id_ in seen_conditions - seen_conditions.add(step.id_) - return TodoStepsNode(step, is_todo, children, seen, False) - - node = make_node(self) - return node - - def get_undone_steps_tree(self) -> TodoStepsNode: - """Return tree of depended-on undone Todos and Conditions.""" - - def walk_tree(node: TodoStepsNode) -> None: - if isinstance(node.item, Todo) and node.item.is_done: - node.hide = True - for child in node.children: - walk_tree(child) + seen = todo.id_ in seen_todos + assert isinstance(todo.id_, int) + seen_todos.add(todo.id_) + for child in todo.children: + children += [make_node(child)] + return TodoNode(todo, seen, children) - seen_todos: set[int] = set() - seen_conditions: set[int] = set() - step_tree = self.get_step_tree(seen_todos, seen_conditions) - walk_tree(step_tree) - return step_tree + return make_node(self) - def get_done_steps_tree(self) -> list[TodoStepsNode]: - """Return tree of depended-on done Todos.""" + @property + def tree_effort(self) -> float: + """Return sum of performed efforts of self and all descendants.""" - def make_nodes(node: TodoStepsNode) -> list[TodoStepsNode]: - children: list[TodoStepsNode] = [] - if not isinstance(node.item, Todo): - return children + def walk_tree(node: Todo) -> float: + local_effort = 0.0 for child in node.children: - children += make_nodes(child) - if node.item.is_done: - node.children = children - return [node] - return children - - seen_todos: set[int] = set() - seen_conditions: set[int] = set() - step_tree = self.get_step_tree(seen_todos, seen_conditions) - nodes = make_nodes(step_tree) - return nodes + local_effort += walk_tree(child) + return node.performed_effort + local_effort + + return walk_tree(self) def add_child(self, child: Todo) -> None: """Add child to self.children, avoid recursion, update parenthoods.""" @@ -259,8 +291,17 @@ class Todo(BaseModel[int], ConditionsRelations): self.children.remove(child) child.parents.remove(self) + def save(self, db_conn: DatabaseConnection) -> None: + """On save calls, also check if auto-deletion by effort < 0.""" + if self.effort and self.effort < 0 and self.is_deletable: + self.remove(db_conn) + return + super().save(db_conn) + def remove(self, db_conn: DatabaseConnection) -> None: """Remove from DB, including relations.""" + if not self.is_deletable: + raise HandledException('Cannot remove non-deletable Todo.') children_to_remove = self.children[:] parents_to_remove = self.parents[:] for child in children_to_remove: diff --git a/plomtask/versioned_attributes.py b/plomtask/versioned_attributes.py index 1810a31..d3c3649 100644 --- a/plomtask/versioned_attributes.py +++ b/plomtask/versioned_attributes.py @@ -4,6 +4,7 @@ from typing import Any from sqlite3 import Row from time import sleep from plomtask.db import DatabaseConnection +from plomtask.exceptions import HandledException, BadFormatException TIMESTAMP_FMT = '%Y-%m-%d %H:%M:%S.%f' @@ -30,6 +31,32 @@ class VersionedAttribute: return self.default return self.history[self._newest_timestamp] + def reset_timestamp(self, old_str: str, new_str: str) -> None: + """Rename self.history key (timestamp) old to new. + + Chronological sequence of keys must be preserved, i.e. cannot move + key before earlier or after later timestamp. + """ + try: + new = datetime.strptime(new_str, TIMESTAMP_FMT) + old = datetime.strptime(old_str, TIMESTAMP_FMT) + except ValueError as exc: + raise BadFormatException('Timestamp of illegal format.') from exc + timestamps = list(self.history.keys()) + if old_str not in timestamps: + raise HandledException(f'Timestamp {old} not found in history.') + sorted_timestamps = sorted([datetime.strptime(t, TIMESTAMP_FMT) + for t in timestamps]) + expected_position = sorted_timestamps.index(old) + sorted_timestamps.remove(old) + sorted_timestamps += [new] + sorted_timestamps.sort() + if sorted_timestamps.index(new) != expected_position: + raise HandledException('Timestamp not respecting chronology.') + value = self.history[old_str] + del self.history[old_str] + self.history[new_str] = value + def set(self, value: str | float) -> None: """Add to self.history if and only if not same value as newest one. @@ -51,6 +78,8 @@ class VersionedAttribute: def at(self, queried_time: str) -> str | float: """Retrieve value of timestamp nearest queried_time from the past.""" + if len(queried_time) == 10: + queried_time += ' 23:59:59.999' sorted_timestamps = sorted(self.history.keys()) if 0 == len(sorted_timestamps): return self.default diff --git a/requirements.txt b/requirements.txt index cd1737d..d0164cb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ Jinja2==3.1.3 +unittest-parallel==1.6.1 diff --git a/run.py b/run.py index e1bbe5d..c69dc6a 100755 --- a/run.py +++ b/run.py @@ -2,28 +2,36 @@ """Call this to start the application.""" from sys import exit as sys_exit from os import environ -from plomtask.exceptions import HandledException +from plomtask.exceptions import HandledException, NotFoundException from plomtask.http import TaskHandler, TaskServer -from plomtask.db import DatabaseFile +from plomtask.db import DatabaseFile, UnmigratedDbException PLOMTASK_DB_PATH = environ.get('PLOMTASK_DB_PATH') HTTP_PORT = 8082 DB_CREATION_ASK = 'Database file not found. Create? Y/n\n' +DB_MIGRATE_ASK = 'Database file needs migration. Migrate? Y/n\n' + + +def yes_or_fail(question: str, fail_msg: str) -> None: + """Ask question, raise HandledException(fail_msg) if reply not yes.""" + reply = input(question) + if not reply.lower() in {'y', 'yes', 'yes.', 'yes!'}: + print('Not recognizing reply as "yes".') + raise HandledException(fail_msg) if __name__ == '__main__': try: if not PLOMTASK_DB_PATH: raise HandledException('PLOMTASK_DB_PATH not set.') - db_file = DatabaseFile(PLOMTASK_DB_PATH) - if not db_file.exists: - legal_yesses = {'y', 'yes', 'yes.', 'yes!'} - reply = input(DB_CREATION_ASK) - if reply.lower() in legal_yesses: - db_file.remake() - else: - print('Not recognizing reply as "yes".') - raise HandledException('Cannot run without database.') + try: + db_file = DatabaseFile(PLOMTASK_DB_PATH) + except NotFoundException: + yes_or_fail(DB_CREATION_ASK, 'Cannot run without DB.') + db_file = DatabaseFile.create_at(PLOMTASK_DB_PATH) + except UnmigratedDbException: + yes_or_fail(DB_MIGRATE_ASK, 'Cannot run with unmigrated DB.') + db_file = DatabaseFile.migrate(PLOMTASK_DB_PATH) server = TaskServer(db_file, ('localhost', HTTP_PORT), TaskHandler) print(f'running at port {HTTP_PORT}') try: diff --git a/scripts/init.sql b/scripts/init.sql deleted file mode 100644 index b2979a5..0000000 --- a/scripts/init.sql +++ /dev/null @@ -1,112 +0,0 @@ -CREATE TABLE condition_descriptions ( - parent INTEGER NOT NULL, - timestamp TEXT NOT NULL, - description TEXT NOT NULL, - PRIMARY KEY (parent, timestamp), - FOREIGN KEY (parent) REFERENCES conditions(id) -); -CREATE TABLE condition_titles ( - parent INTEGER NOT NULL, - timestamp TEXT NOT NULL, - title TEXT NOT NULL, - PRIMARY KEY (parent, timestamp), - FOREIGN KEY (parent) REFERENCES conditions(id) -); -CREATE TABLE conditions ( - id INTEGER PRIMARY KEY, - is_active BOOLEAN NOT NULL -); -CREATE TABLE days ( - id TEXT PRIMARY KEY, - comment TEXT NOT NULL -); -CREATE TABLE process_conditions ( - process INTEGER NOT NULL, - condition INTEGER NOT NULL, - PRIMARY KEY (process, condition), - FOREIGN KEY (process) REFERENCES processes(id), - FOREIGN KEY (condition) REFERENCES conditions(id) -); -CREATE TABLE process_descriptions ( - parent INTEGER NOT NULL, - timestamp TEXT NOT NULL, - description TEXT NOT NULL, - PRIMARY KEY (parent, timestamp), - FOREIGN KEY (parent) REFERENCES processes(id) -); -CREATE TABLE process_disables ( - process INTEGER NOT NULL, - condition INTEGER NOT NULL, - PRIMARY KEY(process, condition), - FOREIGN KEY (process) REFERENCES processes(id), - FOREIGN KEY (condition) REFERENCES conditions(id) -); -CREATE TABLE process_efforts ( - parent INTEGER NOT NULL, - timestamp TEXT NOT NULL, - effort REAL NOT NULL, - PRIMARY KEY (parent, timestamp), - FOREIGN KEY (parent) REFERENCES processes(id) -); -CREATE TABLE process_enables ( - process INTEGER NOT NULL, - condition INTEGER NOT NULL, - PRIMARY KEY(process, condition), - FOREIGN KEY (process) REFERENCES processes(id), - FOREIGN KEY (condition) REFERENCES conditions(id) -); -CREATE TABLE process_steps ( - id INTEGER PRIMARY KEY, - owner INTEGER NOT NULL, - step_process INTEGER NOT NULL, - parent_step INTEGER, - FOREIGN KEY (owner) REFERENCES processes(id), - FOREIGN KEY (step_process) REFERENCES processes(id), - FOREIGN KEY (parent_step) REFERENCES process_steps(step_id) -); -CREATE TABLE process_titles ( - parent INTEGER NOT NULL, - timestamp TEXT NOT NULL, - title TEXT NOT NULL, - PRIMARY KEY (parent, timestamp), - FOREIGN KEY (parent) REFERENCES processes(id) -); -CREATE TABLE processes ( - id INTEGER PRIMARY KEY -); -CREATE TABLE todo_children ( - parent INTEGER NOT NULL, - child INTEGER NOT NULL, - PRIMARY KEY (parent, child), - FOREIGN KEY (parent) REFERENCES todos(id), - FOREIGN KEY (child) REFERENCES todos(id) -); -CREATE TABLE todo_conditions ( - todo INTEGER NOT NULL, - condition INTEGER NOT NULL, - PRIMARY KEY(todo, condition), - FOREIGN KEY (todo) REFERENCES todos(id), - FOREIGN KEY (condition) REFERENCES conditions(id) -); -CREATE TABLE todo_disables ( - todo INTEGER NOT NULL, - condition INTEGER NOT NULL, - PRIMARY KEY(todo, condition), - FOREIGN KEY (todo) REFERENCES todos(id), - FOREIGN KEY (condition) REFERENCES conditions(id) -); -CREATE TABLE todo_enables ( - todo INTEGER NOT NULL, - condition INTEGER NOT NULL, - PRIMARY KEY(todo, condition), - FOREIGN KEY (todo) REFERENCES todos(id), - FOREIGN KEY (condition) REFERENCES conditions(id) -); -CREATE TABLE todos ( - id INTEGER PRIMARY KEY, - process INTEGER NOT NULL, - is_done BOOLEAN NOT NULL, - day TEXT NOT NULL, - FOREIGN KEY (process) REFERENCES processes(id), - FOREIGN KEY (day) REFERENCES days(id) -); diff --git a/scripts/pre-commit b/scripts/pre-commit index 2aaccb0..c92a5eb 100755 --- a/scripts/pre-commit +++ b/scripts/pre-commit @@ -1,7 +1,6 @@ #!/bin/sh set -e -# for dir in $(echo '.' 'plomtask' 'tests'); do -for dir in $(echo 'tests'); do +for dir in $(echo '.' 'plomtask' 'tests'); do echo "Running mypy on ${dir}/ …." python3 -m mypy --strict ${dir}/*.py echo "Running flake8 on ${dir}/ …" @@ -9,6 +8,9 @@ for dir in $(echo 'tests'); do echo "Running pylint on ${dir}/ …" python3 -m pylint ${dir}/*.py done -echo "Running unittest on tests/." -python3 -m unittest tests/*.py +echo "Running unittest-parallel on tests/." +unittest-parallel -t . -s tests/ -p '*.py' +set +e +rm test_db:*.* +set -e exit 0 diff --git a/templates/_base.html b/templates/_base.html new file mode 100644 index 0000000..182a1c6 --- /dev/null +++ b/templates/_base.html @@ -0,0 +1,75 @@ + + + + + +today +calendar +conditions +processes +todos +
+{% block content %} +{% endblock %} + + diff --git a/templates/_macros.html b/templates/_macros.html new file mode 100644 index 0000000..55ab626 --- /dev/null +++ b/templates/_macros.html @@ -0,0 +1,72 @@ +{% macro edit_buttons() %} +
+ +
+ +
+
+{% endmacro %} + + + +{% macro datalist_of_titles(title, candidates, historical=false, with_comments=false) %} + +{% for candidate in candidates %} + +{% endfor %} + +{% endmacro %} + + + +{% macro simple_checkbox_table(title, items, type_name, list_name, add_string="add", historical=false) %} +{% if items|length > 0 %} + +{% for item in items %} + + + + +{% endfor %} +
+ + +{% if historical is true %}{{item.title_then}}{% else %}{{item.title.newest|e}}{% endif %} +
+{% endif %} +{{add_string}}: +{% endmacro %} + + + +{% macro history_page(item_name, item, attribute_name, attribute, as_pre=false) %} +

{{item_name}} {{attribute_name}} history

+
+ + + + + + + + +{% for date in attribute.history.keys() | sort(reverse=True) %} + + + + +{% endfor %} + +
{{item_name}}{{item.title.newest|e}}
{% if as_pre %}
{% endif %}{{attribute.history[date]}}{% if as_pre %}
{% endif %}
+ +
+{% endmacro %} diff --git a/templates/base.html b/templates/base.html deleted file mode 100644 index 87b1864..0000000 --- a/templates/base.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - -processes -conditions -today -calendar -
-{% block content %} -{% endblock %} - - diff --git a/templates/calendar.html b/templates/calendar.html index 3acdbc6..4e83573 100644 --- a/templates/calendar.html +++ b/templates/calendar.html @@ -1,14 +1,72 @@ -{% extends 'base.html' %} +{% extends '_base.html' %} + + + +{% block css %} +tr.week_row td { + height: 0.3em; + background-color: black; + padding: 0; + margin: 0; + border-top: 0.2em solid white; +} +tr.month_row td { + border-top: 0.2em solid white; + color: white; + background-color: #555555; +} +table { + width: 100%; +} +tr.day_row td { + background-color: #cccccc; + border-top: 0.2em solid white; +} +td.day_name { + padding-right: 0.5em; +} +td.today { + font-weight: bold; +} +{% endblock %} + + {% block content %} +

calendar

+ +

basic view

+
-from -to +from +to
-