From a58f0f088ef7bed064a7752aeebb03498b692e8b Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Sat, 18 May 2024 06:00:36 +0200
Subject: [PATCH] Add basic sorting features to Condition, Process table views.

---
 plomtask/http.py          | 24 ++++++++++++++++++++++--
 templates/conditions.html |  4 ++--
 templates/processes.html  |  4 ++--
 3 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/plomtask/http.py b/plomtask/http.py
index d5da1d7..c16fddd 100644
--- a/plomtask/http.py
+++ b/plomtask/http.py
@@ -147,7 +147,17 @@ class TaskHandler(BaseHTTPRequestHandler):
 
     def do_GET_conditions(self) -> dict[str, object]:
         """Show all Conditions."""
-        return {'conditions': Condition.all(self.conn)}
+        conditions = Condition.all(self.conn)
+        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}
 
     def do_GET_condition(self) -> dict[str, object]:
         """Show Condition of ?id=."""
@@ -196,7 +206,17 @@ class TaskHandler(BaseHTTPRequestHandler):
 
     def do_GET_processes(self) -> dict[str, object]:
         """Show all Processes."""
-        return {'processes': Process.all(self.conn)}
+        processes = Process.all(self.conn)
+        sort_by = self.params.get_str('sort_by')
+        if sort_by == 'steps':
+            processes.sort(key=lambda c: len(c.explicit_steps))
+        elif sort_by == '-steps':
+            processes.sort(key=lambda c: len(c.explicit_steps), reverse=True)
+        elif sort_by == '-title':
+            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}
 
     def do_POST(self) -> None:
         """Handle any POST request."""
diff --git a/templates/conditions.html b/templates/conditions.html
index 88d1cc7..e8e9fed 100644
--- a/templates/conditions.html
+++ b/templates/conditions.html
@@ -5,8 +5,8 @@
 
 <table>
 <tr>
-<th>active</th>
-<th>title</th>
+<th><a href="?sort_by={% if sort_by == "is_active" %}-{% endif %}is_active">active</a></th>
+<th><a href="?sort_by={% if sort_by == "title" %}-{% endif %}title">title</a></th>
 </tr>
 {% for condition in conditions %}
 <tr>
diff --git a/templates/processes.html b/templates/processes.html
index 5cd00b5..977ac40 100644
--- a/templates/processes.html
+++ b/templates/processes.html
@@ -5,8 +5,8 @@
 
 <table>
 <tr>
-<th>steps</th>
-<th>title</th>
+<th><a href="?sort_by={% if sort_by == "steps" %}-{% endif %}steps">steps</a></th>
+<th><a href="?sort_by={% if sort_by == "title" %}-{% endif %}title">title</a></th>
 </tr>
 {% for process in processes %}
 <tr>
-- 
2.30.2