home · contact · privacy
Remove more redundant code.
authorChristian Heller <c.heller@plomlompom.de>
Thu, 25 Apr 2024 04:30:35 +0000 (06:30 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Thu, 25 Apr 2024 04:30:35 +0000 (06:30 +0200)
plomtask/conditions.py
plomtask/days.py
plomtask/db.py
plomtask/processes.py

index b2ecda14cb7cef0b5bf350d1a253389ea32aabb7..8d67e5a78c85b1672aaaaac133ab0422e80a6d73 100644 (file)
@@ -30,20 +30,6 @@ class Condition(BaseModel[int]):
                 getattr(condition, name).history_from_row(row_)
         return condition
 
-    @classmethod
-    def all(cls, db_conn: DatabaseConnection) -> list[Condition]:
-        """Collect all Conditions and their VersionedAttributes."""
-        conditions = {}
-        for id_, condition in cls.cache_.items():
-            conditions[id_] = condition
-        already_recorded = conditions.keys()
-        for id_ in db_conn.column_all('conditions', 'id'):
-            if id_ not in already_recorded:
-                condition = cls.by_id(db_conn, id_)
-                assert isinstance(condition.id_, int)
-                conditions[condition.id_] = condition
-        return list(conditions.values())
-
     @classmethod
     def by_id(cls, db_conn: DatabaseConnection, id_: int | None,
               create: bool = False) -> Condition:
index ce112006042681ba059bc5129d7c45bb3d8c6d21..258d38dbbf1d7f920f36ca06c9f8292506d16807 100644 (file)
@@ -28,7 +28,6 @@ class Day(BaseModel[str]):
     """Individual days defined by their dates."""
     table_name = 'days'
     to_save = ['comment']
-    id_type = str
 
     def __init__(self, date: str, comment: str = '') -> None:
         self.id_: str = valid_date(date)
index 634862ce6c57b94282e95ce02c7ef1d21d42c99a..2d9ae2714ca9a2e625036ea44f295880db986f16 100644 (file)
@@ -196,3 +196,19 @@ class BaseModel(Generic[BaseModelId]):
     def empty_cache(cls) -> None:
         """Empty class's cache."""
         cls.cache_ = {}
+
+    @classmethod
+    def all(cls: type[BaseModelInstance],
+            db_conn: DatabaseConnection) -> list[BaseModelInstance]:
+        """Collect all objects of class."""
+        items: dict[BaseModelId, BaseModelInstance] = {}
+        for k, v in cls.cache_.items():
+            assert isinstance(v, cls)
+            items[k] = v
+        already_recorded = items.keys()
+        for id_ in db_conn.column_all(cls.table_name, 'id'):
+            if id_ not in already_recorded:
+                # pylint: disable=no-member
+                item = cls.by_id(db_conn, id_)  # type: ignore[attr-defined]
+                items[item.id_] = item
+        return list(items.values())
index 654e5fcb7d8c8659b7c034e427767893b90a84b4..9705f17a5672336371ba186ff5d52cdd5fe002ef 100644 (file)
@@ -34,20 +34,6 @@ class Process(BaseModel[int], ConditionsRelations):
         self.enables: list[Condition] = []
         self.disables: list[Condition] = []
 
-    @classmethod
-    def all(cls, db_conn: DatabaseConnection) -> list[Process]:
-        """Collect all Processes and their connected VersionedAttributes."""
-        processes = {}
-        for id_, process in cls.cache_.items():
-            processes[id_] = process
-        already_recorded = processes.keys()
-        for id_ in db_conn.column_all('processes', 'id'):
-            if id_ not in already_recorded:
-                process = cls.by_id(db_conn, id_)
-                assert isinstance(process.id_, int)
-                processes[process.id_] = process
-        return list(processes.values())
-
     @classmethod
     def by_id(cls, db_conn: DatabaseConnection, id_: int | None,
               create: bool = False) -> Process: