def adapt_references(subtree, destination_course_key, export_fs):
 subtree.runtime.export_fs = export_fs
 for (field_name, field) in subtree.fields.iteritems():
  if field.is_set_on(subtree):
   if isinstance(field, Reference):
    value = field.read_from(subtree)
    if (value is not None):
     field.write_to(subtree, field.read_from(subtree).map_into_course(destination_course_key))
   elif (field_name == 'children'):
    [adapt_references(child, destination_course_key, export_fs) for child in subtree.get_children()]
   elif isinstance(field, ReferenceList):
    field.write_to(subtree, [ele.map_into_course(destination_course_key) for ele in field.read_from(subtree)])
   elif isinstance(field, ReferenceValueDict):
    field.write_to(subtree, {key: ele.map_into_course(destination_course_key) for (key, ele) in field.read_from(subtree).iteritems()})

def adapt_references ( subtree , destination_course_key , export_fs ) : subtree . runtime . export_fs = export_fs for ( field_name , field ) in subtree . fields . iteritems ( ) : if field . is_set_on ( subtree ) : if isinstance ( field , Reference ) : value = field . read_from ( subtree ) if ( value is not None ) : field . write_to ( subtree , field . read_from ( subtree ) . map_into_course ( destination_course_key ) ) elif ( field_name == 'children' ) : [ adapt_references ( child , destination_course_key , export_fs ) for child in subtree . get_children ( ) ] elif isinstance ( field , ReferenceList ) : field . write_to ( subtree , [ ele . map_into_course ( destination_course_key ) for ele in field . read_from ( subtree ) ] ) elif isinstance ( field , ReferenceValueDict ) : field . write_to ( subtree , { key : ele . map_into_course ( destination_course_key ) for ( key , ele ) in field . read_from ( subtree ) . iteritems ( ) } )
map every reference in the subtree into destination_course_key and set it back into the xblock fields .