def structure_from_mongo(structure, course_context=None): """ Converts the 'blocks' key from a list [block_data] to a map {BlockKey: block_data}. Converts 'root' from [block_type, block_id] to BlockKey. Converts 'blocks.*.fields.children' from [[block_type, block_id]] to [BlockKey]. N.B. Does not convert any other ReferenceFields (because we don't know which fields they are at this level). Arguments: structure: The document structure to convert course_context (CourseKey): For metrics gathering, the CourseKey for the course that this data is being processed for. """ with TIMER.timer('structure_from_mongo', course_context) as tagger: tagger.measure('blocks', len(structure['blocks'])) check('seq[2]', structure['root']) check('list(dict)', structure['blocks']) for block in structure['blocks']: if 'children' in block['fields']: check('list(list[2])', block['fields']['children']) structure['root'] = BlockKey(*structure['root']) new_blocks = {} for block in structure['blocks']: if 'children' in block['fields']: block['fields']['children'] = [ BlockKey(*child) for child in block['fields']['children'] ] new_blocks[BlockKey(block['block_type'], block.pop('block_id'))] = BlockData(**block) structure['blocks'] = new_blocks return structure
def structure_from_mongo(structure): """ Converts the 'blocks' key from a list [block_data] to a map {BlockKey: block_data}. Converts 'root' from [block_type, block_id] to BlockKey. Converts 'blocks.*.fields.children' from [[block_type, block_id]] to [BlockKey]. N.B. Does not convert any other ReferenceFields (because we don't know which fields they are at this level). """ check('seq[2]', structure['root']) check('list(dict)', structure['blocks']) for block in structure['blocks']: if 'children' in block['fields']: check('list(list[2])', block['fields']['children']) structure['root'] = BlockKey(*structure['root']) new_blocks = {} for block in structure['blocks']: if 'children' in block['fields']: block['fields']['children'] = [ BlockKey(*child) for child in block['fields']['children'] ] new_blocks[BlockKey(block['block_type'], block.pop('block_id'))] = BlockData(**block) structure['blocks'] = new_blocks return structure