def handle(self, *args, **options): """Command handler""" # updating the learning resources in the order they were # added to the database: this is to process parents before children with transaction.atomic(): all_learning_resources = LearningResource.objects.order_by('id') for learning_resource in all_learning_resources.iterator(): update_description_path(learning_resource)
def replace_missing_title(apps, schema_editor): """ Replaces `MISSING` with `Missing Title` in the title field and with `...` in the description_path field """ # replace title new_title = 'Missing Title' LearningResource = apps.get_model("learningresources", "LearningResource") # update description path all_learning_resources = LearningResource.objects.order_by('id') for learning_resource in all_learning_resources.iterator(): if learning_resource.title == 'MISSING': learning_resource.title = new_title learning_resource.save() update_description_path(learning_resource)
def create_resource(self, **kwargs): """Creates a learning resource with extra fields""" learn_res = create_resource( course=self.course, parent=kwargs.get('parent'), resource_type=kwargs.get('resource_type', "example"), title=kwargs.get('title', "other silly example"), content_xml=kwargs.get('content_xml', "<blah>other blah</blah>"), mpath=kwargs.get('mpath', "/otherblah"), url_name=kwargs.get('url_name'), dpath='') learn_res.xa_nr_views = kwargs.get('xa_nr_views', 0) learn_res.xa_nr_attempts = kwargs.get('xa_nr_attempts', 0) learn_res.xa_avg_grade = kwargs.get('xa_avg_grade', 0) learn_res.save() update_description_path(learn_res) return learn_res
def create_resource(self, **kwargs): """Creates a learning resource with extra fields""" learn_res = create_resource( course=self.course, parent=kwargs.get('parent'), resource_type=kwargs.get('resource_type', "example"), title=kwargs.get('title', "other silly example"), content_xml=kwargs.get('content_xml', "<blah>other blah</blah>"), mpath=kwargs.get('mpath', "/otherblah"), url_name=kwargs.get('url_name'), dpath='' ) learn_res.xa_nr_views = kwargs.get('xa_nr_views', 0) learn_res.xa_nr_attempts = kwargs.get('xa_nr_attempts', 0) learn_res.xa_avg_grade = kwargs.get('xa_avg_grade', 0) learn_res.save() update_description_path(learn_res) return learn_res
def test_update_description_path(self): """Tests for update_description_path""" # after created a resource without parent has the description path # equal to the title self.assertIsNone(self.resource.parent) self.assertEqual(self.resource.title, self.resource.description_path) # changing the title does not update the description path automatically self.resource.title = "123 xyz" self.resource.save() self.assertNotEqual(self.resource.title, self.resource.description_path) # update the description path api.update_description_path(self.resource) self.assertEqual(self.resource.title, self.resource.description_path) # create a child resource child_res = self.create_resource(parent=self.resource) # the description path is the combination of the child resource title # and the parent description path self.assertEqual( child_res.description_path, api.join_description_paths(self.resource.description_path, child_res.title) ) # change both resources title self.resource.title = "1234 xyza" self.resource.save() child_res.title = "foo 1234" child_res.save() # note: child_res.parent and self.resource are 2 different instances # of the same record, but they need to be refreshed separately # after a change made to one of them child_res.parent.refresh_from_db() # update the description path of the child # will not update the parent's one api.update_description_path(child_res) self.assertNotEqual(self.resource.title, self.resource.description_path) self.assertEqual( child_res.description_path, api.join_description_paths(self.resource.description_path, child_res.title) ) # but the parent's update can be forced api.update_description_path(child_res, force_parent_update=True) self.resource.refresh_from_db() self.assertEqual(self.resource.title, self.resource.description_path) self.assertEqual( child_res.description_path, api.join_description_paths(self.resource.description_path, child_res.title) ) # removing the description path of the parent self.resource.description_path = "" self.resource.title = "999 new title" self.resource.save() child_res.parent.refresh_from_db() # the update of the child will update the parent without forcing it api.update_description_path(child_res) self.resource.refresh_from_db() self.assertEqual(self.resource.title, self.resource.description_path)
def setUp(self): super(TestExport, self).setUp() # Add some LearningResources on top of the default to make things # interesting. tarball_file = self.get_course_single_tarball() import_file(tarball_file, self.repo.id, self.user.id) # Add a resource with a '/' in the title and too many characters. course = self.repo.course_set.first() resource = create_resource( course=course, resource_type=LearningResourceType.objects.first().name, title="//x"*300, content_xml="", mpath="", url_name=None, parent=None, dpath='' ) update_description_path(resource) # Add static assets. with TemporaryFile() as temp1: with TemporaryFile() as temp2: temp1.write(b"file1") temp2.write(b"file2") file1 = File(temp1, name="iamafile1.txt") file2 = File(temp2, name="iamafile2.txt") asset1 = create_static_asset(course.id, file1) resource.static_assets.add(asset1) # If url_name is missing we should just use id in filename. resource.url_name = None resource.save() asset2 = create_static_asset(course.id, file2) self.resource.static_assets.add(asset2)
def setUp(self): super(TestExport, self).setUp() # Add some LearningResources on top of the default to make things # interesting. tarball_file = self.get_course_single_tarball() import_file(tarball_file, self.repo.id, self.user.id) # Add a resource with a '/' in the title and too many characters. course = self.repo.course_set.first() resource = create_resource( course=course, resource_type=LearningResourceType.objects.first().name, title="//x" * 300, content_xml="", mpath="", url_name=None, parent=None, dpath='') update_description_path(resource) # Add static assets. with TemporaryFile() as temp1: with TemporaryFile() as temp2: temp1.write(b"file1") temp2.write(b"file2") file1 = File(temp1, name="iamafile1.txt") file2 = File(temp2, name="iamafile2.txt") asset1 = create_static_asset(course.id, file1) resource.static_assets.add(asset1) # If url_name is missing we should just use id in filename. resource.url_name = None resource.save() asset2 = create_static_asset(course.id, file2) self.resource.static_assets.add(asset2)
def populate_description_paths(apps, schema_editor): """Populate the description path field""" LearningResource = apps.get_model("learningresources", "LearningResource") for learning_resource in LearningResource.objects.all(): # pragma: no cover update_description_path(learning_resource) # pragma: no cover
def populate_description_paths(apps, schema_editor): """Populate the description path field""" LearningResource = apps.get_model("learningresources", "LearningResource") for learning_resource in LearningResource.objects.all( ): # pragma: no cover update_description_path(learning_resource) # pragma: no cover
def test_update_description_path(self): """Tests for update_description_path""" # after created a resource without parent has the description path # equal to the title self.assertIsNone(self.resource.parent) self.assertEqual( self.resource.title, self.resource.description_path ) # changing the title does not update the description path automatically self.resource.title = '123 xyz' self.resource.save() self.assertNotEqual( self.resource.title, self.resource.description_path ) # update the description path api.update_description_path(self.resource) self.assertEqual( self.resource.title, self.resource.description_path ) # create a child resource child_res = self.create_resource( parent=self.resource ) # the description path is the combination of the child resource title # and the parent description path self.assertEqual( child_res.description_path, api.join_description_paths( self.resource.description_path, child_res.title ) ) # change both resources title self.resource.title = '1234 xyza' self.resource.save() child_res.title = 'foo 1234' child_res.save() # note: child_res.parent and self.resource are 2 different instances # of the same record, but they need to be refreshed separately # after a change made to one of them child_res.parent.refresh_from_db() # update the description path of the child # will not update the parent's one api.update_description_path(child_res) self.assertNotEqual( self.resource.title, self.resource.description_path ) self.assertEqual( child_res.description_path, api.join_description_paths( self.resource.description_path, child_res.title ) ) # but the parent's update can be forced api.update_description_path(child_res, force_parent_update=True) self.resource.refresh_from_db() self.assertEqual( self.resource.title, self.resource.description_path ) self.assertEqual( child_res.description_path, api.join_description_paths( self.resource.description_path, child_res.title ) ) # removing the description path of the parent self.resource.description_path = '' self.resource.title = '999 new title' self.resource.save() child_res.parent.refresh_from_db() # the update of the child will update the parent without forcing it api.update_description_path(child_res) self.resource.refresh_from_db() self.assertEqual( self.resource.title, self.resource.description_path ) # update the title of the child to the "missing title" string self.resource.title = api.MissingTitle.for_title_field self.resource.save() api.update_description_path(self.resource) self.assertEqual( self.resource.title, api.MissingTitle.for_title_field ) self.assertEqual( self.resource.description_path, api.MissingTitle.for_desc_path_field ) # update also the child child_res.parent.refresh_from_db() api.update_description_path(child_res) self.assertEqual( child_res.description_path, api.join_description_paths( api.MissingTitle.for_desc_path_field, child_res.title ) )
def handle(self, *args, **options): """Command handler""" for learning_resource in LearningResource.objects.all(): update_description_path(learning_resource)