From: Christian Heller Date: Tue, 21 May 2024 02:29:54 +0000 (+0200) Subject: Add text-based search/filter for Conditions and Processes. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/decks/static/pick_tasks?a=commitdiff_plain;h=85c260d1416340274b954175a69563868f481bc5;p=plomtask Add text-based search/filter for Conditions and Processes. --- diff --git a/plomtask/conditions.py b/plomtask/conditions.py index 8ab4282..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_) diff --git a/plomtask/db.py b/plomtask/db.py index d2791b1..548381e 100644 --- a/plomtask/db.py +++ b/plomtask/db.py @@ -185,6 +185,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.""" @@ -220,6 +231,7 @@ class BaseModel(Generic[BaseModelId]): to_save_relations: list[tuple[str, str, str]] = [] 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: @@ -342,6 +354,26 @@ class BaseModel(Generic[BaseModelId]): items[item.id_] = item return list(items.values()) + @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_. diff --git a/plomtask/http.py b/plomtask/http.py index 41ce5d6..4841472 100644 --- a/plomtask/http.py +++ b/plomtask/http.py @@ -154,7 +154,8 @@ class TaskHandler(BaseHTTPRequestHandler): def do_GET_conditions(self) -> dict[str, object]: """Show all Conditions.""" - 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) @@ -164,7 +165,9 @@ class TaskHandler(BaseHTTPRequestHandler): 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} + return {'conditions': conditions, + 'sort_by': sort_by, + 'pattern': pattern} def do_GET_condition(self) -> dict[str, object]: """Show Condition of ?id=.""" @@ -213,7 +216,8 @@ class TaskHandler(BaseHTTPRequestHandler): def do_GET_processes(self) -> dict[str, object]: """Show all Processes.""" - 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 c: len(c.explicit_steps)) @@ -223,7 +227,7 @@ class TaskHandler(BaseHTTPRequestHandler): processes.sort(key=lambda c: c.title.newest, reverse=True) else: processes.sort(key=lambda c: c.title.newest) - return {'processes': processes, 'sort_by': sort_by} + return {'processes': processes, 'sort_by': sort_by, 'pattern': pattern} def do_POST(self) -> None: """Handle any POST request.""" diff --git a/plomtask/processes.py b/plomtask/processes.py index c23c6de..684dec8 100644 --- a/plomtask/processes.py +++ b/plomtask/processes.py @@ -30,6 +30,7 @@ class Process(BaseModel[int], ConditionsRelations): ('process_blockers', 'process', 'blockers'), ('process_enables', 'process', 'enables'), ('process_disables', 'process', 'disables')] + to_search = ['title.newest', 'description.newest'] def __init__(self, id_: int | None, calendarize: bool = False) -> None: BaseModel.__init__(self, id_) diff --git a/templates/conditions.html b/templates/conditions.html index e8e9fed..5990711 100644 --- a/templates/conditions.html +++ b/templates/conditions.html @@ -3,6 +3,11 @@ {% block content %}

conditions

+
+ + +
+ diff --git a/templates/processes.html b/templates/processes.html index 977ac40..9b282bf 100644 --- a/templates/processes.html +++ b/templates/processes.html @@ -3,6 +3,11 @@ {% block content %}

processes

+ + + + +
active
steps