def delete_metadata(channel, node_ids, exclude_node_ids, force_delete): # Only delete all metadata if we are not doing selective deletion delete_all_metadata = not (node_ids or exclude_node_ids) if node_ids or exclude_node_ids: # If we have been passed node ids do not do a full deletion pass with db_task_write_lock: set_content_invisible(channel.id, node_ids, exclude_node_ids) # If everything has been made invisible, delete all the metadata delete_all_metadata = not channel.root.available if force_delete: # Do this before we delete all the metadata, as otherwise we lose # track of which local files were associated with the channel we # just deleted. unused_files, _ = get_files_to_transfer(channel.id, node_ids, exclude_node_ids, True, renderable_only=False) with db_task_write_lock: propagate_forced_localfile_removal(unused_files) if delete_all_metadata: logger.info("Deleting all channel metadata") with db_task_write_lock: channel.delete_content_tree_and_files() return delete_all_metadata
def test_no_nodes_present_peer(self, channel_stats_mock): ContentNode.objects.update(available=False) LocalFile.objects.update(available=False) stats = {} channel_stats_mock.return_value = stats files_to_transfer, _ = get_files_to_transfer(self.the_channel_id, [], [], False, False, peer_id="1") self.assertEqual(files_to_transfer.count(), 0)
def test_one_node_present_peer(self, channel_stats_mock): ContentNode.objects.update(available=False) LocalFile.objects.update(available=False) obj = ContentNode.objects.get(title="c2c1") stats = {obj.id: {}} channel_stats_mock.return_value = stats files_to_transfer, _ = get_files_to_transfer(self.the_channel_id, [], [], False, False, peer_id="1") self.assertEqual(files_to_transfer.count(), obj.files.count())
def test_include_one_available_nodes_disk(self, channel_stats_mock): ContentNode.objects.update(available=False) LocalFile.objects.update(available=False) parent = ContentNode.objects.get(title="c2") obj = ContentNode.objects.get(title="c2c1") stats = {obj.id: {}, parent.id: {}} channel_stats_mock.return_value = stats files_to_transfer, _ = get_files_to_transfer(self.the_channel_id, [parent.id], [], False, False, drive_id="1") self.assertEqual(files_to_transfer.count(), obj.files.count())
def test_all_nodes_present_peer(self, channel_stats_mock): ContentNode.objects.update(available=False) LocalFile.objects.update(available=False) stats = { key: {} for key in ContentNode.objects.all().values_list("id", flat=True) } channel_stats_mock.return_value = stats files_to_transfer, _ = get_files_to_transfer(self.the_channel_id, [], [], False, False, peer_id="1") self.assertEqual(files_to_transfer.count(), LocalFile.objects.filter(available=False).count())
def test_no_exclude_duplicate_files(self): """ Test that including a node id in exclude_node_ids does not exclude a shared file that is also used an in included node """ root_node = ContentNode.objects.get(parent__isnull=True) node = ContentNode.objects.filter(parent=root_node, kind=content_kinds.TOPIC).first() node1 = ContentNode.objects.create( title="test1", id=uuid.uuid4().hex, content_id=uuid.uuid4().hex, channel_id=root_node.channel_id, parent=node, kind=content_kinds.VIDEO, available=False, ) node2 = ContentNode.objects.create( title="test2", id=uuid.uuid4().hex, content_id=uuid.uuid4().hex, channel_id=root_node.channel_id, parent=node, kind=content_kinds.VIDEO, available=False, ) local_file = LocalFile.objects.create(id=uuid.uuid4().hex, extension="mp4", available=False, file_size=10) File.objects.create( id=uuid.uuid4().hex, local_file=local_file, available=False, contentnode=node1, ) File.objects.create( id=uuid.uuid4().hex, local_file=local_file, available=False, contentnode=node2, ) files_to_transfer, _ = get_files_to_transfer(root_node.channel_id, [node1.id], [node2.id], False, False) self.assertEqual(files_to_transfer.filter(id=local_file.id).count(), 1)
def test_all_nodes_present_disk_renderable_only(self, channel_stats_mock): ContentNode.objects.update(available=False) LocalFile.objects.update(available=False) stats = { key: {} for key in ContentNode.objects.all().values_list("id", flat=True) } channel_stats_mock.return_value = stats files_to_transfer, _ = get_files_to_transfer(self.the_channel_id, [], [], False, True, drive_id="1") self.assertEqual( files_to_transfer.count(), LocalFile.objects.filter( available=False, files__contentnode__in=ContentNode.objects.filter( renderable_contentnodes_q_filter), ).count(), )