Skip to content

Commit

Permalink
Merge pull request #10830 from Jaspreet-singh-1032/10391-content-down…
Browse files Browse the repository at this point in the history
…load-request-progress-tracking

track progress for content download request
  • Loading branch information
rtibbles authored Jun 14, 2023
2 parents 08f6f32 + eac019d commit ea1e075
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
14 changes: 14 additions & 0 deletions kolibri/core/content/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,20 @@ def build_for_user(cls, user):
status=ContentRequestStatus.Pending,
)

def update_progress(self, progress, total_progress):
self.metadata = self.metadata or {}
self.metadata["progress"] = progress
self.metadata["total_progress"] = total_progress
self.save()

@property
def progress(self):
return self.metadata.get("progress", 0) if self.metadata else 0

@property
def total_progress(self):
return self.metadata.get("total_progress", 0) if self.metadata else 0


class ContentRequestManager(models.Manager):
request_type = None
Expand Down
5 changes: 3 additions & 2 deletions kolibri/core/content/utils/content_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from kolibri.core.content.utils.assignment import DeletedAssignment
from kolibri.core.content.utils.channel_import import import_channel_from_data
from kolibri.core.content.utils.resource_import import (
RemoteChannelResourceImportManager,
ContentDownloadRequestResourceImportManager,
)
from kolibri.core.device.models import DeviceStatus
from kolibri.core.device.models import LearnerDeviceStatus
Expand Down Expand Up @@ -474,11 +474,12 @@ def process_download_request(download_request):
channel_id = ContentNode.objects.get(
pk=download_request.contentnode_id
).channel_id
import_manager = RemoteChannelResourceImportManager(
import_manager = ContentDownloadRequestResourceImportManager(
channel_id,
peer_id=peer.id,
baseurl=peer.base_url,
node_ids=[download_request.contentnode_id],
download_request=download_request,
)
import_manager.run()

Expand Down
43 changes: 43 additions & 0 deletions kolibri/core/content/utils/resource_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,3 +538,46 @@ def get_import_data(self):
renderable_only=self.renderable_only,
drive_id=self.drive_id,
)


class ContentDownloadRequestResourceImportManager(RemoteChannelResourceImportManager):
def __init__(
self,
channel_id,
peer_id=None,
baseurl=None,
node_ids=None,
exclude_node_ids=None,
renderable_only=True,
fail_on_error=False,
content_dir=None,
download_request=None,
timeout=transfer.Transfer.DEFAULT_TIMEOUT,
):
super(ContentDownloadRequestResourceImportManager, self).__init__(
channel_id,
peer_id=peer_id,
baseurl=baseurl,
node_ids=node_ids,
exclude_node_ids=exclude_node_ids,
renderable_only=renderable_only,
fail_on_error=fail_on_error,
content_dir=content_dir,
timeout=timeout,
)
self.download_request = download_request

def start_progress(self, total=100):
super(ContentDownloadRequestResourceImportManager, self).start_progress(total)
if self.download_request:
self.download_request.update_progress(0, total)

def update_progress(self, increment=1, message="", extra_data=None):
super(ContentDownloadRequestResourceImportManager, self).update_progress(
increment, message, extra_data
)
if self.download_request:
self.download_request.update_progress(
self.download_request.progress + increment,
self.download_request.total_progress,
)

0 comments on commit ea1e075

Please sign in to comment.