From 6034476a4d81e28c45e906cdcc36138bc28ccd55 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Wed, 10 Apr 2024 06:36:15 +0200
Subject: [PATCH] Hide already-seen descendants of implicit ProcessSteps.

---
 plomtask/processes.py  |  5 ++++-
 templates/process.html |  6 ++++++
 tests/processes.py     | 14 +++++++-------
 3 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/plomtask/processes.py b/plomtask/processes.py
index 23cded7..0a7d5b5 100644
--- a/plomtask/processes.py
+++ b/plomtask/processes.py
@@ -2,7 +2,7 @@
 from __future__ import annotations
 from sqlite3 import Row
 from datetime import datetime
-from typing import Any
+from typing import Any, Set
 from plomtask.db import DatabaseConnection
 from plomtask.exceptions import NotFoundException, BadFormatException
 
@@ -88,10 +88,13 @@ class Process:
                                  if s.parent_step_id == node_id]
             for child in explicit_children:
                 node['steps'][child.id_] = make_node(child)
+            node['seen'] = node_id in seen_step_ids
+            seen_step_ids.add(node_id)
             for id_, step in node['steps'].items():
                 walk_steps(id_, step)
 
         steps: dict[int, dict[str, object]] = {}
+        seen_step_ids: Set[int] = set()
         if external_owner is None:
             external_owner = self
         for step in [s for s in self.explicit_steps
diff --git a/templates/process.html b/templates/process.html
index 0a15cc4..1dcfff6 100644
--- a/templates/process.html
+++ b/templates/process.html
@@ -11,7 +11,11 @@
 {% endif %}
 </td>
 <td>{% for i in range(indent) %}+{%endfor %}
+{% if (not step_node.is_explicit) and step_node.seen %}
+<a href="process?id={{step_node.process.id_}}">({{step_node.process.title.newest|e}})</a>
+{% else %}
 <a href="process?id={{step_node.process.id_}}">{{step_node.process.title.newest|e}}</a>
+{% endif %}
 </td>
 <td>
 {% if step_node.is_explicit %}
@@ -19,9 +23,11 @@ add step: <input name="new_step_to_{{step_id}}" list="candidates" autocomplete="
 {% endif %}
 </td>
 </tr>
+{% if step_node.is_explicit or not step_node.seen %}
 {% for substep_id, substep in step_node.steps.items() %}
 {{ process_with_steps(substep_id, substep, indent+1) }}
 {% endfor %}
+{% endif %}
 {% endmacro %}
 
 {% block content %}
diff --git a/tests/processes.py b/tests/processes.py
index ac519c8..d6a9899 100644
--- a/tests/processes.py
+++ b/tests/processes.py
@@ -64,40 +64,40 @@ class TestsWithDB(TestCaseWithDB):
         p_1.add_step(self.db_conn, None, p_2.id_, None)
         p_1_dict: dict[int, dict[str, Any]] = {1: {
             'process': p_2, 'parent_id': None,
-            'is_explicit': True, 'steps': {}
+            'is_explicit': True, 'steps': {}, 'seen': False
         }}
         self.assertEqual(p_1.get_steps(self.db_conn, None), p_1_dict)
         s_b = p_1.add_step(self.db_conn, None, p_3.id_, None)
         p_1_dict[2] = {
             'process': p_3, 'parent_id': None,
-            'is_explicit': True, 'steps': {}
+            'is_explicit': True, 'steps': {}, 'seen': False
         }
         self.assertEqual(p_1.get_steps(self.db_conn, None), p_1_dict)
         s_c = p_2.add_step(self.db_conn, None, p_3.id_, None)
         assert s_c.id_ is not None
         p_1_dict[1]['steps'] = {3: {
             'process': p_3, 'parent_id': None,
-            'is_explicit': False, 'steps': {}
+            'is_explicit': False, 'steps': {}, 'seen': False
         }}
         self.assertEqual(p_1.get_steps(self.db_conn, None), p_1_dict)
         p_1.add_step(self.db_conn, None, p_2.id_, s_b.id_)
         p_1_dict[2]['steps'][4] = {
-            'process': p_2, 'parent_id': s_b.id_,
+            'process': p_2, 'parent_id': s_b.id_, 'seen': False,
             'is_explicit': True, 'steps': {3: {
                 'process': p_3, 'parent_id': None,
-                'is_explicit': False, 'steps': {}
+                'is_explicit': False, 'steps': {}, 'seen': True
                 }}}
         self.assertEqual(p_1.get_steps(self.db_conn, None), p_1_dict)
         p_1.add_step(self.db_conn, None, p_3.id_, 999)
         p_1_dict[5] = {
             'process': p_3, 'parent_id': None,
-            'is_explicit': True, 'steps': {}
+            'is_explicit': True, 'steps': {}, 'seen': False
         }
         self.assertEqual(p_1.get_steps(self.db_conn, None), p_1_dict)
         p_1.add_step(self.db_conn, None, p_3.id_, 3)
         p_1_dict[6] = {
             'process': p_3, 'parent_id': None,
-            'is_explicit': True, 'steps': {}
+            'is_explicit': True, 'steps': {}, 'seen': False
         }
         self.assertEqual(p_1.get_steps(self.db_conn, None), p_1_dict)
 
-- 
2.30.2