home · contact · privacy
Enable deletion of Conditions.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 28 Apr 2024 22:32:24 +0000 (00:32 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 28 Apr 2024 22:32:24 +0000 (00:32 +0200)
plomtask/conditions.py
plomtask/http.py
templates/condition.html
templates/process.html
tests/conditions.py

index a45260092fdc6c75a94846eb0893b775eb9316dd..337970924ab25305d64abef13afe24148b0209b1 100644 (file)
@@ -4,6 +4,7 @@ from typing import Any
 from sqlite3 import Row
 from plomtask.db import DatabaseConnection, BaseModel
 from plomtask.misc import VersionedAttribute
+from plomtask.exceptions import HandledException
 
 
 class Condition(BaseModel[int]):
@@ -35,6 +36,16 @@ class Condition(BaseModel[int]):
         self.title.save(db_conn)
         self.description.save(db_conn)
 
+    def remove(self, db_conn: DatabaseConnection) -> None:
+        """Remove from DB, with dependencies."""
+        assert isinstance(self.id_, int)
+        for item in ('process', 'todo'):
+            for attr in ('conditions', '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')
+        super().remove(db_conn)
+
 
 class ConditionsRelations:
     """Methods for handling relations to Conditions, for Todo and Process."""
index 800193ce49ab7c90a1ff00add0a9d3fd32cf8b4e..7e2b241aec468a8b6c29221b6f146a702b990883 100644 (file)
@@ -273,6 +273,10 @@ class TaskHandler(BaseHTTPRequestHandler):
     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'):
+            condition = Condition.by_id(self.conn, id_)
+            condition.remove(self.conn)
+            return '/conditions'
         condition = Condition.by_id(self.conn, id_, create=True)
         condition.title.set(self.form_data.get_str('title'))
         condition.description.set(self.form_data.get_str('description'))
index dfdf6ef9ce376f8c231e4b33388102d6eb51d16c..a0a9f45c54c3372bad772c1a67f33ba699e1630d 100644 (file)
@@ -5,6 +5,11 @@
 <form action="condition?id={{condition.id_ or ''}}" method="POST">
 title: <input name="title" value="{{condition.title.newest|e}}" />
 description: <input name="description" value="{{condition.description.newest|e}}" />
-<input type="submit" value="OK" />
+
+<input class="btn-harmless" type="submit" name="update" value="update" />
+<div class="btn-to-right">
+<input class="btn-dangerous" type="submit" name="delete" value="delete" />
+</div>
+
 {% endblock %}
 
index 8a50e15d16f541974c7c494248885898cffe0095..2a577152720c2101353767182f443d9d7fd11874 100644 (file)
@@ -1,18 +1,5 @@
 {% extends 'base.html' %}
 
-{% block css %}
-input.btn-harmless {
-  color: green;
-}
-input.btn-dangerous {
-  color: red;
-}
-div.btn-to-right {
-  float: right;
-  text-align: right;
-}
-{% endblock %}
-
 
 
 {% macro step_with_steps(step_id, step_node, indent) %}
index 3b95de1f3fe7e89cc30a14f149983d14ad690962..6538e87019bfa7bffa0d8fccb22f8981500943d1 100644 (file)
@@ -1,7 +1,8 @@
 """Test Conditions module."""
 from tests.utils import TestCaseWithDB, TestCaseWithServer
 from plomtask.conditions import Condition
-from plomtask.exceptions import NotFoundException
+from plomtask.processes import Process
+from plomtask.exceptions import NotFoundException, HandledException
 
 
 class TestsWithDB(TestCaseWithDB):
@@ -36,6 +37,22 @@ class TestsWithDB(TestCaseWithDB):
         condition_retrieved = Condition.by_id(self.db_conn, 1)
         self.assertEqual(True, condition_retrieved.is_active)
 
+    def test_Condition_removal(self) -> None:
+        """Test removal of Condition."""
+        cond = Condition(None, False)
+        cond.save(self.db_conn)
+        assert isinstance(cond.id_, int)
+        proc = Process(None)
+        proc.save(self.db_conn)
+        proc.set_conditions(self.db_conn, [cond.id_], 'conditions')
+        proc.save(self.db_conn)
+        with self.assertRaises(HandledException):
+            cond.remove(self.db_conn)
+        proc.set_conditions(self.db_conn, [], 'conditions')
+        proc.save(self.db_conn)
+        cond.remove(self.db_conn)
+        self.assertEqual(Condition.all(self.db_conn), [])
+
 
 class TestsWithServer(TestCaseWithServer):
     """Module tests against our HTTP server/handler (and database)."""