home · contact · privacy
Slightly improve and re-organize Condition tests.
[plomtask] / plomtask / conditions.py
index 70365ce6a1d3d5eb4aa49077eb95e62cf1444bb0..15dcb9df623c60378485632ce3bebc4c30f03d47 100644 (file)
@@ -11,6 +11,9 @@ class Condition(BaseModel[int]):
     to_save = ['is_active']
     to_save_versioned = ['title', 'description']
     to_search = ['title.newest', 'description.newest']
+    can_create_by_id = True
+    sorters = {'is_active': lambda c: c.is_active,
+               'title': lambda c: c.title.newest}
 
     def __init__(self, id_: int | None, is_active: bool = False) -> None:
         super().__init__(id_)
@@ -25,13 +28,14 @@ class Condition(BaseModel[int]):
         Checks for Todos and Processes that depend on Condition, prohibits
         deletion if found.
         """
-        if self.id_ is None:
-            raise HandledException('cannot remove unsaved item')
-        for item in ('process', 'todo'):
-            for attr in ('conditions', 'blockers', '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')
+        if self.id_ is not None:
+            for item in ('process', 'todo'):
+                for attr in ('conditions', 'blockers', 'enables', 'disables'):
+                    table_name = f'{item}_{attr}'
+                    for _ in db_conn.row_where(table_name, 'condition',
+                                               self.id_):
+                        msg = 'cannot remove Condition in use'
+                        raise HandledException(msg)
         super().remove(db_conn)