Skip to content

Commit

Permalink
Move contentnode removal propagation function to standalone utility.
Browse files Browse the repository at this point in the history
  • Loading branch information
rtibbles committed Oct 16, 2023
1 parent 263d027 commit fa54ebd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
4 changes: 2 additions & 2 deletions kolibri/core/content/management/commands/deletecontent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

from kolibri.core.content.models import ChannelMetadata
from kolibri.core.content.models import ContentNode
from kolibri.core.content.models import ContentRequest
from kolibri.core.content.models import LocalFile
from kolibri.core.content.utils.annotation import propagate_forced_localfile_removal
from kolibri.core.content.utils.annotation import reannotate_all_channels
from kolibri.core.content.utils.annotation import set_content_invisible
from kolibri.core.content.utils.content_request import propagate_contentnode_removal
from kolibri.core.content.utils.importability_annotation import clear_channel_stats
from kolibri.core.content.utils.paths import get_content_database_file_path
from kolibri.core.tasks.management.commands.base import AsyncCommand
Expand Down Expand Up @@ -84,7 +84,7 @@ def delete_metadata(
channel.delete_content_tree_and_files()

if update_content_requests and removed_resources:
ContentRequest.objects.propagate_removal(list(removed_resources))
propagate_contentnode_removal(list(removed_resources))

# Clear any previously set channel availability stats for this channel
clear_channel_stats(channel.id)
Expand Down
21 changes: 0 additions & 21 deletions kolibri/core/content/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,27 +459,6 @@ def get_queryset(self):
queryset = queryset.filter(type=self.request_type)
return queryset

def propagate_removal(self, contentnode_ids):
"""
Deletes all learner initiated ContentRequests for the passed in contentnode_ids
Matching learner initiated ContentRequests will be deleted - this means that if
resources are deleted by an admin, we remove any associated learner initiated requests.
Also updates the status of any COMPLETED non-learner initiated ContentDownloadRequests to PENDING
"""
BATCH_SIZE = 250
for i in range(0, len(contentnode_ids), BATCH_SIZE):
batch = contentnode_ids[i : i + BATCH_SIZE]
self.filter(
contentnode_id__in=batch, reason=ContentRequestReason.UserInitiated
).delete()
self.filter(
contentnode_id__in=batch,
type=ContentRequestType.Download,
status=ContentRequestStatus.Completed,
).exclude(reason=ContentRequestReason.UserInitiated).update(
status=ContentRequestStatus.Pending
)


class ContentRequest(models.Model):
"""
Expand Down
22 changes: 22 additions & 0 deletions kolibri/core/content/utils/content_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from kolibri.core.content.models import ContentDownloadRequest
from kolibri.core.content.models import ContentNode
from kolibri.core.content.models import ContentRemovalRequest
from kolibri.core.content.models import ContentRequest
from kolibri.core.content.models import ContentRequestReason
from kolibri.core.content.models import ContentRequestStatus
from kolibri.core.content.models import File
Expand Down Expand Up @@ -947,6 +948,27 @@ def process_content_removal_requests(queryset):
remaining_pending.update(status=ContentRequestStatus.Completed)


def propagate_contentnode_removal(contentnode_ids):
"""
Deletes all learner initiated ContentRequests for the passed in contentnode_ids
Matching learner initiated ContentRequests will be deleted - this means that if
resources are deleted by an admin, we remove any associated learner initiated requests.
Also updates the status of any COMPLETED non-learner initiated ContentDownloadRequests to PENDING
"""
BATCH_SIZE = 250
for i in range(0, len(contentnode_ids), BATCH_SIZE):
batch = contentnode_ids[i : i + BATCH_SIZE]
ContentRequest.objects.filter(
contentnode_id__in=batch, reason=ContentRequestReason.UserInitiated
).delete()
ContentDownloadRequest.objects.filter(
contentnode_id__in=batch,
status=ContentRequestStatus.Completed,
).exclude(reason=ContentRequestReason.UserInitiated).update(
status=ContentRequestStatus.Pending
)


class StorageCalculator:
def __init__(self, incomplete_downloads_queryset):
incomplete_removals = incomplete_removals_queryset()
Expand Down

0 comments on commit fa54ebd

Please sign in to comment.