Skip to content

Commit

Permalink
Clean up admin imported flags, except in content request cleanups.
Browse files Browse the repository at this point in the history
  • Loading branch information
rtibbles committed Sep 16, 2023
1 parent f93ca6b commit a806b8a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 11 deletions.
24 changes: 18 additions & 6 deletions kolibri/core/content/management/commands/deletecontent.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
logger = logging.getLogger(__name__)


def delete_metadata(channel, node_ids, exclude_node_ids, force_delete):
def delete_metadata(
channel, node_ids, exclude_node_ids, force_delete, ignore_admin_flags
):
# Only delete all metadata if we are not doing selective deletion
delete_all_metadata = not (node_ids or exclude_node_ids)

Expand All @@ -31,7 +33,9 @@ def delete_metadata(channel, node_ids, exclude_node_ids, force_delete):
)

# If we have been passed node ids do not do a full deletion pass
set_content_invisible(channel.id, node_ids, exclude_node_ids)
set_content_invisible(
channel.id, node_ids, exclude_node_ids, not ignore_admin_flags
)
# If everything has been made invisible, delete all the metadata
delete_all_metadata = delete_all_metadata or not channel.root.available

Expand Down Expand Up @@ -123,11 +127,20 @@ def add_arguments(self, parser):
help="Ensure removal of files",
)

parser.add_argument(
"--ignore_admin_flags",
action="store_false",
dest="ignore_admin_flags",
default=True,
help="Don't modify admin_imported values when deleting content",
)

def handle_async(self, *args, **options):
channel_id = options["channel_id"]
node_ids = options["node_ids"]
exclude_node_ids = options["exclude_node_ids"]
force_delete = options["force_delete"]
ignore_admin_flags = options["ignore_admin_flags"]

try:
channel = ChannelMetadata.objects.get(pk=channel_id)
Expand All @@ -136,10 +149,9 @@ def handle_async(self, *args, **options):
"Channel matching id {id} does not exist".format(id=channel_id)
)

(
total_resource_number,
delete_all_metadata,
) = delete_metadata(channel, node_ids, exclude_node_ids, force_delete)
(total_resource_number, delete_all_metadata,) = delete_metadata(
channel, node_ids, exclude_node_ids, force_delete, ignore_admin_flags
)
unused_files = LocalFile.objects.get_unused_files()
# Get the number of files that are being deleted
unused_files_count = unused_files.count()
Expand Down
22 changes: 22 additions & 0 deletions kolibri/core/content/test/test_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,28 @@ def test_all_nodes_available_non_include_exclude_unaffected(self):
node.refresh_from_db()
self.assertTrue(node.available)

def test_all_leaves_clear_admin_imported(self):
ContentNode.objects.all().update(available=True)
set_leaf_nodes_invisible(test_channel_id, clear_admin_imported=True)
self.assertFalse(
any(
ContentNode.objects.exclude(kind=content_kinds.TOPIC).values_list(
"admin_imported", flat=True
)
)
)

def test_all_leaves_no_clear_admin_imported(self):
ContentNode.objects.all().update(available=True, admin_imported=True)
set_leaf_nodes_invisible(test_channel_id)
self.assertTrue(
all(
ContentNode.objects.exclude(kind=content_kinds.TOPIC).values_list(
"admin_imported", flat=True
)
)
)

def tearDown(self):
call_command("flush", interactive=False)
super(SetContentNodesInvisibleTestCase, self).tearDown()
Expand Down
2 changes: 1 addition & 1 deletion kolibri/core/content/test/utils/test_content_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ def test_basic(self):
"deletecontent",
self.node.channel_id,
node_ids=[self.request.contentnode_id],
force_delete=True,
ignore_admin_flags=True,
)
self.assertEqual(self.qs.count(), 0)
self.assertEqual(ContentDownloadRequest.objects.count(), 0)
Expand Down
22 changes: 18 additions & 4 deletions kolibri/core/content/utils/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ def _calculate_batch_params(bridge, channel_id, node_ids, exclude_node_ids):
return max_rght, dynamic_chunksize


def set_leaf_nodes_invisible(channel_id, node_ids=None, exclude_node_ids=None):
def set_leaf_nodes_invisible(
channel_id, node_ids=None, exclude_node_ids=None, clear_admin_imported=False
):
"""
Set nodes in a channel as unavailable.
With no additional arguments, this will hide an entire channel.
Expand All @@ -280,6 +282,13 @@ def set_leaf_nodes_invisible(channel_id, node_ids=None, exclude_node_ids=None):
)
)

values_dict = {
"available": False,
}

if clear_admin_imported:
values_dict["admin_imported"] = False

while min_boundary < max_rght:
batch_statement = _create_batch_update_statement(
bridge,
Expand All @@ -292,7 +301,7 @@ def set_leaf_nodes_invisible(channel_id, node_ids=None, exclude_node_ids=None):

# Execute the update for this batch
connection.execute(
batch_statement.values(available=False).execution_options(autocommit=True)
batch_statement.values(**values_dict).execution_options(autocommit=True)
)

min_boundary += dynamic_chunksize
Expand Down Expand Up @@ -748,8 +757,13 @@ def set_content_visibility_from_disk(channel_id):
update_content_metadata(channel_id)


def set_content_invisible(channel_id, node_ids, exclude_node_ids):
set_leaf_nodes_invisible(channel_id, node_ids, exclude_node_ids)
def set_content_invisible(channel_id, node_ids, exclude_node_ids, clear_admin_imported):
set_leaf_nodes_invisible(
channel_id,
node_ids,
exclude_node_ids,
clear_admin_imported=clear_admin_imported,
)
recurse_annotation_up_tree(channel_id)
set_channel_metadata_fields(channel_id)
ContentCacheKey.update_cache_key()
Expand Down
1 change: 1 addition & 0 deletions kolibri/core/content/utils/content_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,7 @@ def process_content_removal_requests(queryset):
"deletecontent",
channel_id,
node_ids=contentnode_ids,
ignore_admin_flags=True,
)
# mark all as completed
channel_requests.update(status=ContentRequestStatus.Completed)
Expand Down

0 comments on commit a806b8a

Please sign in to comment.