def build_material_legacy_api_data(linked_object): # Skipping root folder and files in legacy API event = get_event(linked_object) try: cache = g.legacy_api_event_attachments[event] except (AttributeError, KeyError): if 'legacy_api_event_attachments' not in g: g.legacy_api_event_attachments = {} g.legacy_api_event_attachments[event] = cache = defaultdict(list) query = (event.all_attachment_folders .filter_by(is_deleted=False, is_default=False) .options(joinedload(AttachmentFolder.legacy_mapping), joinedload(AttachmentFolder.attachments))) for folder in query: cache[folder.object].append(folder) return filter(None, map(_build_folder_legacy_api_data, cache.get(linked_object, [])))