|
8 | 8 | from starlette.status import HTTP_404_NOT_FOUND |
9 | 9 |
|
10 | 10 | from authentication import KeycloakUser |
11 | | -from database.model.helper_functions import get_relationships |
| 11 | +from database.model.helper_functions import get_relationships, get_asset_by_identifier |
12 | 12 | from database.model.named_relation import NamedRelation, Taxonomy |
| 13 | +from database.model.ai_resource.resource_table import AIResourceORM |
| 14 | +from database.session import DbSession |
| 15 | + |
13 | 16 |
|
14 | 17 | MODEL = TypeVar("MODEL", bound=SQLModel) |
15 | 18 |
|
@@ -99,7 +102,7 @@ def deserialize( |
99 | 102 |
|
100 | 103 | @staticmethod |
101 | 104 | def deserialize_ids(clazz: type[SQLModel], session: Session, ids: list[int]): |
102 | | - query = select(clazz).where(clazz.identifier.in_(ids)) # noqa |
| 105 | + query = select(clazz).where(clazz.identifier.in_(ids)) |
103 | 106 | existing = session.scalars(query).all() |
104 | 107 | ids_not_found = set(ids) - {e.identifier for e in existing} |
105 | 108 | if any(ids_not_found): |
@@ -253,16 +256,30 @@ def create_getter_dict(attribute_serializers: Dict[str, Serializer]): |
253 | 256 | object.""" |
254 | 257 | attribute_names = set(attribute_serializers.keys()) |
255 | 258 |
|
| 259 | + def is_soft_deleted(item) -> bool: |
| 260 | + if hasattr(item, "date_deleted") and item.date_deleted: |
| 261 | + return True |
| 262 | + if isinstance(item, AIResourceORM): |
| 263 | + with DbSession() as session: |
| 264 | + clazz_, resource = get_asset_by_identifier(item.identifier, session) |
| 265 | + return resource.date_deleted is not None |
| 266 | + |
| 267 | + return False # Not sure what cases are not covered here. |
| 268 | + |
256 | 269 | class GetterDictSerializer(GetterDict): |
257 | 270 | def get(self, key: Any, default: Any = None) -> Any: |
258 | | - # if key == "has_part" and hasattr(self._obj, "ai_resource"): |
259 | | - # return [p.identifier for p in self._obj.ai_resource.has_part] |
260 | 271 | if key in attribute_names: |
261 | 272 | serializer = attribute_serializers[key] |
262 | 273 | attribute_value = serializer.value(model=self._obj, attribute_name=key) |
263 | 274 | if attribute_value is not None: |
264 | 275 | if isinstance(attribute_value, list): |
265 | | - return [serializer.serialize(v) for v in attribute_value] |
| 276 | + return [ |
| 277 | + serializer.serialize(v) |
| 278 | + for v in attribute_value |
| 279 | + if not is_soft_deleted(v) |
| 280 | + ] |
| 281 | + if is_soft_deleted(attribute_value): |
| 282 | + return None |
266 | 283 | return serializer.serialize(attribute_value) |
267 | 284 | return super().get(key, default) |
268 | 285 |
|
|
0 commit comments