home · contact · privacy
Enable server to alternatively output response ctx as JSON, for debugging and testing...
[plomtask] / plomtask / db.py
index 99998a6ab29f760ba0d62f90395739dad4b521ff..1cecc16f6985b555f25757ad6e9f65724311a287 100644 (file)
@@ -271,6 +271,23 @@ class BaseModel(Generic[BaseModelId]):
         assert isinstance(other.id_, int)
         return self.id_ < other.id_
 
+    @property
+    def as_dict(self) -> dict[str, object]:
+        """Return self as (json.dumps-coompatible) dict."""
+        d: dict[str, object] = {'id': self.id_}
+        for k in self.to_save:
+            attr = getattr(self, k)
+            if hasattr(attr, 'as_dict'):
+                d[k] = attr.as_dict
+            d[k] = attr
+        for k in self.to_save_versioned:
+            attr = getattr(self, k)
+            d[k] = attr.as_dict
+        for r in self.to_save_relations:
+            attr_name = r[2]
+            d[attr_name] = [x.as_dict for x in getattr(self, attr_name)]
+        return d
+
     # cache management
     # (we primarily use the cache to ensure we work on the same object in
     # memory no matter where and how we retrieve it, e.g. we don't want