def remap_namespace(module, target_location_namespace): if target_location_namespace is None: return module # This looks a bit wonky as we need to also change the 'name' of the # imported course to be what the caller passed in if module.location.category != 'course': module.location = module.location._replace( tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course ) else: original_location = module.location # # module is a course module # module.location = module.location._replace( tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course, name=target_location_namespace.name ) # There is more re-namespacing work we have to do when # importing course modules # remap pdf_textbook urls for entry in module.pdf_textbooks: for chapter in entry.get('chapters', []): if StaticContent.is_c4x_path(chapter.get('url', '')): chapter['url'] = StaticContent.renamespace_c4x_path( chapter['url'], target_location_namespace ) # if there is a wiki_slug which is the same as the original location # (aka default value), then remap that so the wiki doesn't point to # the old Wiki. if module.wiki_slug == original_location.course: module.wiki_slug = target_location_namespace.course module.save() # then remap children pointers since they too will be re-namespaced if hasattr(module, 'children'): children_locs = module.children if children_locs is not None and children_locs != []: new_locs = [] for child in children_locs: child_loc = Location(child) new_child_loc = child_loc._replace( tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course ) new_locs.append(new_child_loc.url()) module.children = new_locs return module
def remap_namespace(module, target_location_namespace): if target_location_namespace is None: return module # This looks a bit wonky as we need to also change the 'name' of the imported course to be what # the caller passed in if module.location.category != 'course': module.location = module.location._replace( tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course ) else: original_location = module.location # # module is a course module # module.location = module.location._replace( tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course, name=target_location_namespace.name ) # # There is more re-namespacing work we have to do when importing course modules # # remap pdf_textbook urls for entry in module.pdf_textbooks: for chapter in entry.get('chapters', []): if StaticContent.is_c4x_path(chapter.get('url', '')): chapter['url'] = StaticContent.renamespace_c4x_path(chapter['url'], target_location_namespace) # if there is a wiki_slug which is the same as the original location (aka default value), # then remap that so the wiki doesn't point to the old Wiki. if module.wiki_slug == original_location.course: module.wiki_slug = target_location_namespace.course module.save() # then remap children pointers since they too will be re-namespaced if hasattr(module, 'children'): children_locs = module.children if children_locs is not None and children_locs != []: new_locs = [] for child in children_locs: child_loc = Location(child) new_child_loc = child_loc._replace( tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course ) new_locs.append(new_child_loc.url()) module.children = new_locs return module
def remap_namespace(module, target_location_namespace): if target_location_namespace is None: return module original_location = module.location # This looks a bit wonky as we need to also change the 'name' of the # imported course to be what the caller passed in if module.location.category != 'course': _update_module_location( module, module.location.replace( tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course ) ) else: # # module is a course module # module.location = module.location.replace( tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course, name=target_location_namespace.name ) # There is more re-namespacing work we have to do when # importing course modules # remap pdf_textbook urls for entry in module.pdf_textbooks: for chapter in entry.get('chapters', []): if StaticContent.is_c4x_path(chapter.get('url', '')): chapter['url'] = StaticContent.renamespace_c4x_path( chapter['url'], target_location_namespace ) # Original wiki_slugs had value location.course. To make them unique this was changed to 'org.course.name'. # If we are importing into a course with a different course_id and wiki_slug is equal to either of these default # values then remap it so that the wiki does not point to the old wiki. if original_location.course_id != target_location_namespace.course_id: original_unique_wiki_slug = u'{0}.{1}.{2}'.format( original_location.org, original_location.course, original_location.name ) if module.wiki_slug == original_unique_wiki_slug or module.wiki_slug == original_location.course: module.wiki_slug = u'{0}.{1}.{2}'.format( target_location_namespace.org, target_location_namespace.course, target_location_namespace.name, ) module.save() all_fields = module.get_explicitly_set_fields_by_scope(Scope.content) all_fields.update(module.get_explicitly_set_fields_by_scope(Scope.settings)) if hasattr(module, 'children'): all_fields['children'] = module.children def convert_ref(reference): """ Convert a reference to the new namespace, but only if the original namespace matched the original course. Otherwise, returns the input value. """ new_ref = reference ref = Location(reference) in_original_namespace = (original_location.tag == ref.tag and original_location.org == ref.org and original_location.course == ref.course) if in_original_namespace: new_ref = ref.replace( tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course ).url() return new_ref for field_name in all_fields: field_object = module.fields.get(field_name) if isinstance(field_object, Reference): new_ref = convert_ref(getattr(module, field_name)) setattr(module, field_name, new_ref) module.save() elif isinstance(field_object, ReferenceList): references = getattr(module, field_name) new_references = [convert_ref(reference) for reference in references] setattr(module, field_name, new_references) module.save() elif isinstance(field_object, ReferenceValueDict): reference_dict = getattr(module, field_name) new_reference_dict = { key: convert_ref(reference) for key, reference in reference_dict.items() } setattr(module, field_name, new_reference_dict) module.save() return module
def remap_namespace(module, target_location_namespace): if target_location_namespace is None: return module original_location = module.location # This looks a bit wonky as we need to also change the 'name' of the # imported course to be what the caller passed in if module.location.category != 'course': module.location = module.location.replace( tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course) else: # # module is a course module # module.location = module.location.replace( tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course, name=target_location_namespace.name) # There is more re-namespacing work we have to do when # importing course modules # remap pdf_textbook urls for entry in module.pdf_textbooks: for chapter in entry.get('chapters', []): if StaticContent.is_c4x_path(chapter.get('url', '')): chapter['url'] = StaticContent.renamespace_c4x_path( chapter['url'], target_location_namespace) # if there is a wiki_slug which is the same as the original location # (aka default value), then remap that so the wiki doesn't point to # the old Wiki. if module.wiki_slug == original_location.course: module.wiki_slug = target_location_namespace.course module.save() all_fields = module.get_explicitly_set_fields_by_scope(Scope.content) all_fields.update(module.get_explicitly_set_fields_by_scope( Scope.settings)) if hasattr(module, 'children'): all_fields['children'] = module.children def convert_ref(reference): """ Convert a reference to the new namespace, but only if the original namespace matched the original course. Otherwise, returns the input value. """ new_ref = reference ref = Location(reference) in_original_namespace = (original_location.tag == ref.tag and original_location.org == ref.org and original_location.course == ref.course) if in_original_namespace: new_ref = ref.replace( tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course).url() return new_ref for field in all_fields: if isinstance(module.fields.get(field), Reference): new_ref = convert_ref(getattr(module, field)) setattr(module, field, new_ref) module.save() elif isinstance(module.fields.get(field), ReferenceList): references = getattr(module, field) new_references = [ convert_ref(reference) for reference in references ] setattr(module, field, new_references) module.save() return module
def remap_namespace(module, target_location_namespace): if target_location_namespace is None: return module original_location = module.location # This looks a bit wonky as we need to also change the 'name' of the # imported course to be what the caller passed in if module.location.category != 'course': module.location = module.location.replace( tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course ) else: # # module is a course module # module.location = module.location.replace( tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course, name=target_location_namespace.name ) # There is more re-namespacing work we have to do when # importing course modules # remap pdf_textbook urls for entry in module.pdf_textbooks: for chapter in entry.get('chapters', []): if StaticContent.is_c4x_path(chapter.get('url', '')): chapter['url'] = StaticContent.renamespace_c4x_path( chapter['url'], target_location_namespace ) # if there is a wiki_slug which is the same as the original location # (aka default value), then remap that so the wiki doesn't point to # the old Wiki. if module.wiki_slug == original_location.course: module.wiki_slug = target_location_namespace.course module.save() all_fields = module.get_explicitly_set_fields_by_scope(Scope.content) all_fields.update(module.get_explicitly_set_fields_by_scope(Scope.settings)) if hasattr(module, 'children'): all_fields['children'] = module.children def convert_ref(reference): """ Convert a reference to the new namespace, but only if the original namespace matched the original course. Otherwise, returns the input value. """ new_ref = reference ref = Location(reference) in_original_namespace = (original_location.tag == ref.tag and original_location.org == ref.org and original_location.course == ref.course) if in_original_namespace: new_ref = ref.replace( tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course ).url() return new_ref for field in all_fields: if isinstance(module.fields.get(field), Reference): new_ref = convert_ref(getattr(module, field)) setattr(module, field, new_ref) module.save() elif isinstance(module.fields.get(field), ReferenceList): references = getattr(module, field) new_references = [convert_ref(reference) for reference in references] setattr(module, field, new_references) module.save() return module
def remap_namespace(module, target_location_namespace): if target_location_namespace is None: return module original_location = module.location # This looks a bit wonky as we need to also change the 'name' of the # imported course to be what the caller passed in if module.location.category != 'course': module.location = module.location.replace( tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course) else: # # module is a course module # module.location = module.location.replace( tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course, name=target_location_namespace.name) # There is more re-namespacing work we have to do when # importing course modules # remap pdf_textbook urls for entry in module.pdf_textbooks: for chapter in entry.get('chapters', []): if StaticContent.is_c4x_path(chapter.get('url', '')): chapter['url'] = StaticContent.renamespace_c4x_path( chapter['url'], target_location_namespace) # Original wiki_slugs had value location.course. To make them unique this was changed to 'org.course.name'. # If we are importing into a course with a different course_id and wiki_slug is equal to either of these default # values then remap it so that the wiki does not point to the old wiki. if original_location.course_id != target_location_namespace.course_id: original_unique_wiki_slug = '{0}.{1}.{2}'.format( original_location.org, original_location.course, original_location.name) if module.wiki_slug == original_unique_wiki_slug or module.wiki_slug == original_location.course: module.wiki_slug = '{0}.{1}.{2}'.format( target_location_namespace.org, target_location_namespace.course, target_location_namespace.name, ) module.save() all_fields = module.get_explicitly_set_fields_by_scope(Scope.content) all_fields.update(module.get_explicitly_set_fields_by_scope( Scope.settings)) if hasattr(module, 'children'): all_fields['children'] = module.children def convert_ref(reference): """ Convert a reference to the new namespace, but only if the original namespace matched the original course. Otherwise, returns the input value. """ new_ref = reference ref = Location(reference) in_original_namespace = (original_location.tag == ref.tag and original_location.org == ref.org and original_location.course == ref.course) if in_original_namespace: new_ref = ref.replace( tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course).url() return new_ref for field_name in all_fields: field_object = module.fields.get(field_name) if isinstance(field_object, Reference): new_ref = convert_ref(getattr(module, field_name)) setattr(module, field_name, new_ref) module.save() elif isinstance(field_object, ReferenceList): references = getattr(module, field_name) new_references = [ convert_ref(reference) for reference in references ] setattr(module, field_name, new_references) module.save() elif isinstance(field_object, ReferenceValueDict): reference_dict = getattr(module, field_name) new_reference_dict = { key: convert_ref(reference) for key, reference in reference_dict.items() } setattr(module, field_name, new_reference_dict) module.save() return module