def update_content_copy(self, request, channelmetadata_channel_id, pk, content_copy, *args, **kwargs): """ endpoint for content api method update_content_copy(file_object=None, content_copy=None) """ target_file = models.File.objects.using(channelmetadata_channel_id).get(pk=pk) return Response(api.update_content_copy(file_object=target_file, content_copy=str(content_copy)))
def test_update_content_copy(self): """ test adding same content copies, and deleting content copy """ # add same content copy twise, there should be no duplication fpath_1 = self.temp_f_1.name fm_1 = content.Format.objects.using(self.the_channel_id).get(format_size=102) fm_3 = content.Format.objects.using(self.the_channel_id).get(format_size=46) file_1 = content.File.objects.using(self.the_channel_id).get(format=fm_1) api.update_content_copy(file_1, fpath_1) file_3 = content.File.objects.using(self.the_channel_id).filter(format=fm_3)[1] api.update_content_copy(file_3, fpath_1) self.assertEqual(1, len(os.listdir(os.path.join(settings.CONTENT_COPY_DIR, '0', '9')))) # swap the content copy in file_3 fpath_2 = self.temp_f_2.name self.assertEqual(file_3.extension, '.pdf') api.update_content_copy(file_3, fpath_2) self.assertEqual(file_3.extension, '.mp4') # because file_3 and file_2 all have reference pointing to this content copy, # erase the reference from file_2 won't delete the content copy fm_2 = content.Format.objects.using(self.the_channel_id).get(format_size=51) file_2 = content.File.objects.using(self.the_channel_id).get(format=fm_2) api.update_content_copy(file_2, fpath_2) self.assertTrue(file_2.content_copy) api.update_content_copy(file_2, None) self.assertFalse(file_2.content_copy) content_copy_path = os.path.join(settings.CONTENT_COPY_DIR, '3', '3', '335782204c8215e0061516c6b3b80271.mp4') self.assertTrue(os.path.isfile(content_copy_path)) # all reference pointing to this content copy is gone, # the content copy should be deleted api.update_content_copy(file_3, None) self.assertFalse(os.path.isfile(content_copy_path)) self.assertFalse(file_2.content_copy) self.assertFalse(file_2.checksum) # update None content copy on empty File object should be silent and have no effect api.update_content_copy(file_2, None) # test File __str__ method self.assertEqual(file_1.__str__(), '09293abba61d4fcfa4e3bd804bcaba43.pdf') # test MimeType __str__ method self.assertEqual(fm_1.mimetype.__str__(), 'video_high') # test for non File object exception with self.assertRaises(TypeError): api.update_content_copy(None, None)
def test_update_content_copy(self): """ test adding same content copies, and deleting content copy """ # add same content copy twise, there should be no duplication fpath_1 = self.temp_f_1.name fm_1 = content.Format.objects.using( self.the_channel_id).get(format_size=102) fm_3 = content.Format.objects.using( self.the_channel_id).get(format_size=46) file_1 = content.File.objects.using( self.the_channel_id).get(format=fm_1) api.update_content_copy(file_1, fpath_1) file_3 = content.File.objects.using( self.the_channel_id).filter(format=fm_3)[1] api.update_content_copy(file_3, fpath_1) self.assertEqual( 1, len(os.listdir(os.path.join(settings.CONTENT_COPY_DIR, '0', '9')))) # swap the content copy in file_3 fpath_2 = self.temp_f_2.name self.assertEqual(file_3.extension, '.pdf') api.update_content_copy(file_3, fpath_2) self.assertEqual(file_3.extension, '.mp4') # because file_3 and file_2 all have reference pointing to this content copy, # erase the reference from file_2 won't delete the content copy fm_2 = content.Format.objects.using( self.the_channel_id).get(format_size=51) file_2 = content.File.objects.using( self.the_channel_id).get(format=fm_2) api.update_content_copy(file_2, fpath_2) self.assertTrue(file_2.content_copy) api.update_content_copy(file_2, None) self.assertFalse(file_2.content_copy) content_copy_path = os.path.join( settings.CONTENT_COPY_DIR, '3', '3', '335782204c8215e0061516c6b3b80271.mp4') self.assertTrue(os.path.isfile(content_copy_path)) # all reference pointing to this content copy is gone, # the content copy should be deleted api.update_content_copy(file_3, None) self.assertFalse(os.path.isfile(content_copy_path)) self.assertFalse(file_2.content_copy) self.assertFalse(file_2.checksum) # update None content copy on empty File object should be silent and have no effect api.update_content_copy(file_2, None) # test File __str__ method self.assertEqual(file_1.__str__(), '09293abba61d4fcfa4e3bd804bcaba43.pdf') # test MimeType __str__ method self.assertEqual(fm_1.mimetype.__str__(), 'video_high') # test for non File object exception with self.assertRaises(TypeError): api.update_content_copy(None, None)