X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=plomtask%2Fdb.py;fp=plomtask%2Fdb.py;h=1cecc16f6985b555f25757ad6e9f65724311a287;hb=db62e6559fdd577dae38d4b6f5cbd5ef6a14cc57;hp=99998a6ab29f760ba0d62f90395739dad4b521ff;hpb=a658d31f985ec9a08181005614c3d9876e046274;p=plomtask diff --git a/plomtask/db.py b/plomtask/db.py index 99998a6..1cecc16 100644 --- a/plomtask/db.py +++ b/plomtask/db.py @@ -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