from sqlite3 import Row
from plomtask.db import DatabaseConnection, BaseModel
from plomtask.misc import VersionedAttribute
+from plomtask.exceptions import HandledException
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."""
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'))
<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 %}
{% 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) %}
"""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):
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)."""