From 998990aabcd1307c0d503ff122ef421b2b151b33 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Mon, 1 May 2023 22:09:16 +0200 Subject: [PATCH] Made `Entity.children_ids` resilient against deleted objects. --- platypush/entities/_base.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/platypush/entities/_base.py b/platypush/entities/_base.py index bad21861a..b68d8d814 100644 --- a/platypush/entities/_base.py +++ b/platypush/entities/_base.py @@ -7,7 +7,7 @@ import subprocess import sys import types from datetime import datetime -from typing import Callable, Dict, Final, Optional, Set, Type, Tuple, Any +from typing import Callable, Dict, Final, List, Optional, Set, Type, Tuple, Any import pkgutil @@ -192,6 +192,20 @@ if 'entity' not in Base.metadata: return () return col_name, self._serialize_value(col_name) + @property + def children_ids(self) -> List[int]: + """ + Returns the children IDs of the entity. + """ + children_ids = [] + for child in self.children: + try: + children_ids.append(child.id) + except ObjectDeletedError: + pass + + return children_ids + def to_dict(self) -> dict: """ Returns the current entity as a flatten dictionary. @@ -202,7 +216,7 @@ if 'entity' not in Base.metadata: for col in self.columns if self._column_to_pair(col) ), - 'children_ids': [c.id for c in self.children], + 'children_ids': self.children_ids, } def to_json(self) -> dict: