def test_duplicated_version(self):
        """
        Test that if a library is updated, and the content block is duplicated,
        the new block will use the old library version and not the new one.
        """
        store = modulestore()
        self.assertEqual(len(self.library.children), 1)
        self.assertEqual(len(self.lc_block.children), 1)

        # Edit the only problem in the library:
        self.problem.display_name = "--changed in library--"
        store.update_item(self.problem, self.user.id)
        # Create an additional problem block in the library:
        ItemFactory.create(
            category="problem",
            parent_location=self.library.location,
            user_id=self.user.id,
            publish_item=False,
        )

        # Refresh our reference to the library
        self.library = store.get_library(self.lib_key)

        # Refresh our reference to the block
        self.lc_block = store.get_item(self.lc_block.location)
        self.problem_in_course = store.get_item(
            self.problem_in_course.location)

        # The library has changed...
        self.assertEqual(len(self.library.children), 2)

        # But the block hasn't.
        self.assertEqual(len(self.lc_block.children), 1)
        self.assertEqual(self.problem_in_course.location,
                         self.lc_block.children[0])
        self.assertEqual(self.problem_in_course.display_name,
                         self.original_display_name)

        # Duplicate self.lc_block:
        duplicate = store.get_item(
            _duplicate_item(self.course.location, self.lc_block.location,
                            self.user))
        # The duplicate should have identical children to the original:
        self.assertEqual(len(duplicate.children), 1)
        self.assertTrue(self.lc_block.source_library_version)
        self.assertEqual(self.lc_block.source_library_version,
                         duplicate.source_library_version)
        problem2_in_course = store.get_item(duplicate.children[0])
        self.assertEqual(problem2_in_course.display_name,
                         self.original_display_name)
    def test_duplicated_version(self):
        """
        Test that if a library is updated, and the content block is duplicated,
        the new block will use the old library version and not the new one.
        """
        store = modulestore()
        self.assertEqual(len(self.library.children), 1)
        self.assertEqual(len(self.lc_block.children), 1)

        # Edit the only problem in the library:
        self.problem.display_name = "--changed in library--"
        store.update_item(self.problem, self.user.id)
        # Create an additional problem block in the library:
        ItemFactory.create(
            category="problem",
            parent_location=self.library.location,
            user_id=self.user.id,
            publish_item=False,
        )

        # Refresh our reference to the library
        self.library = store.get_library(self.lib_key)

        # Refresh our reference to the block
        self.lc_block = store.get_item(self.lc_block.location)
        self.problem_in_course = store.get_item(
            self.problem_in_course.location)

        # The library has changed...
        self.assertEqual(len(self.library.children), 2)

        # But the block hasn't.
        self.assertEqual(len(self.lc_block.children), 1)
        self.assertEqual(self.problem_in_course.location,
                         self.lc_block.children[0])
        self.assertEqual(self.problem_in_course.display_name,
                         self.original_display_name)

        # Duplicate self.lc_block:
        duplicate = store.get_item(
            _duplicate_item(self.course.location, self.lc_block.location,
                            self.user))
        # The duplicate should have identical children to the original:
        self.assertEqual(len(duplicate.children), 1)
        self.assertTrue(self.lc_block.source_library_version)
        self.assertEqual(self.lc_block.source_library_version,
                         duplicate.source_library_version)
        problem2_in_course = store.get_item(duplicate.children[0])
        self.assertEqual(problem2_in_course.display_name,
                         self.original_display_name)
Esempio n. 3
0
def duplicate_lab_content(user, source_location, parent_location):
    store = modulestore()
    parent_locator = UsageKey.from_string(parent_location)
    source_locator = UsageKey.from_string(source_location)
    source_item = store.get_item(source_locator)
    parent_item = store.get_item(parent_locator)

    # delete parent's children first
    for child in parent_item.get_children():
        store.delete_item(child.location, user.id)

    # duplicate quiz_blocks
    for quiz_block in source_item.get_children():
        new_location = _duplicate_item(parent_locator, quiz_block.location, user=user, display_name=quiz_block.display_name)
        modulestore().publish(new_location, user.id)
Esempio n. 4
0
def duplicate_course(source, target, user, extra_fields=None, http_protocol='https'):
    source_course = get_course_by_id(SlashSeparatedCourseKey.from_deprecated_string(source))
    target_course = force_create_course(source, target, user, extra_fields)

    new_course_key = str(target_course.location.course_key)
    image_url = "{}://{}{}".format(http_protocol, settings.LMS_BASE, course_image_url(source_course))
    target_course.course_image = upload_image_from_url(new_course_key, image_url)
    modulestore().update_item(target_course, user.id)

    for child in target_course.get_children():
        modulestore().delete_item(child.location, user.id)

    target_locator = target_course.location

    for child in source_course.get_children():
        new_location = _duplicate_item(target_locator, child.location, user=user, display_name=child.display_name)
        modulestore().publish(new_location, user.id)

    return target_course
Esempio n. 5
0
def sync_surveys(course, user, master_survey):
    """
    Copy surveys from master course to all other courses
    """
    from contentstore.views.item import _duplicate_item
    section = None

    for each in course.get_children():
        if each.display_name == 'Survey':
            section = each
            break

    course_location = course.location.to_deprecated_string()
    if not section:
        section = create_xblock(user, 'chapter', course_location, name='Survey')

    for sub_section in master_survey.get_children():
        new_location = _duplicate_item(
            section.location, sub_section.location, user=user,
            display_name=sub_section.display_name)
        get_modulestore().publish(new_location, user.id)
    def test_persistent_overrides(self, duplicate):
        """
        Test that when we override Scope.settings values in a course,
        the override values persist even when the block is refreshed
        with updated blocks from the library.
        """
        new_display_name = "Modified Problem Title"
        new_weight = 15
        self.problem_in_course.display_name = new_display_name
        self.problem_in_course.weight = new_weight

        modulestore().update_item(self.problem_in_course, self.user.id)
        if duplicate:
            # Check that this also works when the RCB is duplicated.
            self.lc_block = modulestore().get_item(
                _duplicate_item(self.course.location, self.lc_block.location,
                                self.user))
            self.problem_in_course = modulestore().get_item(
                self.lc_block.children[0])
        else:
            self.problem_in_course = modulestore().get_item(
                self.problem_in_course.location)
        self.assertEqual(self.problem_in_course.display_name, new_display_name)
        self.assertEqual(self.problem_in_course.weight, new_weight)

        # Change the settings in the library version:
        self.problem.display_name = "X"
        self.problem.weight = 99
        new_data_value = "<problem><p>Changed data to check that non-overriden fields *do* get updated.</p></problem>"
        self.problem.data = new_data_value
        modulestore().update_item(self.problem, self.user.id)

        self.lc_block = self._refresh_children(self.lc_block)
        self.problem_in_course = modulestore().get_item(
            self.problem_in_course.location)

        self.assertEqual(self.problem_in_course.display_name, new_display_name)
        self.assertEqual(self.problem_in_course.weight, new_weight)
        self.assertEqual(self.problem_in_course.data, new_data_value)
    def test_persistent_overrides(self, duplicate):
        """
        Test that when we override Scope.settings values in a course,
        the override values persist even when the block is refreshed
        with updated blocks from the library.
        """
        new_display_name = "Modified Problem Title"
        new_weight = 15
        self.problem_in_course.display_name = new_display_name
        self.problem_in_course.weight = new_weight

        modulestore().update_item(self.problem_in_course, self.user.id)
        if duplicate:
            # Check that this also works when the RCB is duplicated.
            self.lc_block = modulestore().get_item(
                _duplicate_item(self.course.location, self.lc_block.location,
                                self.user))
            self.problem_in_course = modulestore().get_item(
                self.lc_block.children[0])
        else:
            self.problem_in_course = modulestore().get_item(
                self.problem_in_course.location)
        self.assertEqual(self.problem_in_course.display_name, new_display_name)
        self.assertEqual(self.problem_in_course.weight, new_weight)

        # Change the settings in the library version:
        self.problem.display_name = "X"
        self.problem.weight = 99
        new_data_value = "<problem><p>Changed data to check that non-overriden fields *do* get updated.</p></problem>"
        self.problem.data = new_data_value
        modulestore().update_item(self.problem, self.user.id)

        self.lc_block = self._refresh_children(self.lc_block)
        self.problem_in_course = modulestore().get_item(
            self.problem_in_course.location)

        self.assertEqual(self.problem_in_course.display_name, new_display_name)
        self.assertEqual(self.problem_in_course.weight, new_weight)
        self.assertEqual(self.problem_in_course.data, new_data_value)