Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

track progress for content download request #10830

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like the correct place for this logic to live.

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 @@ -518,3 +518,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,
)