From 12c1774e34281f6ef9a1671bdd6165b2b58a6fdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Qui=C3=B1ones?= Date: Thu, 5 Oct 2023 09:09:15 -0300 Subject: [PATCH 1/2] Download manager: Rename start method It is now consistent: start/update/cancel --- kolibri_explore_plugin/collectionviews.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kolibri_explore_plugin/collectionviews.py b/kolibri_explore_plugin/collectionviews.py index 35303be2..59a0d820 100644 --- a/kolibri_explore_plugin/collectionviews.py +++ b/kolibri_explore_plugin/collectionviews.py @@ -240,7 +240,7 @@ def _set_empty_state(self): self._enqueuing_timestamp = None self._enqueued_timestamp = None - def from_manifest(self, manifest, user): + def start(self, manifest, user): """Start downloading a collection manifest.""" if self._stage != DownloadStage.NOT_STARTED: raise DownloadError("A download has already started. Can't start") @@ -646,7 +646,7 @@ def start_download(request): # Init the download manager and start downloading try: - _collection_download_manager.from_manifest(manifest, request.user) + _collection_download_manager.start(manifest, request.user) logger.info(f"Download started for grade={grade} name={name}") except DownloadError as err: raise APIException(err) From 5fc604cd52fd0ee11d76fc8592e4497dfd4975fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Qui=C3=B1ones?= Date: Thu, 5 Oct 2023 09:58:27 -0300 Subject: [PATCH 2/2] Download manager: Set initial state on get status Otherwise the UI will attempt a new download in the next session after the download is complete. Helps #863 --- kolibri_explore_plugin/collectionviews.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/kolibri_explore_plugin/collectionviews.py b/kolibri_explore_plugin/collectionviews.py index 59a0d820..ba7af268 100644 --- a/kolibri_explore_plugin/collectionviews.py +++ b/kolibri_explore_plugin/collectionviews.py @@ -240,6 +240,12 @@ def _set_empty_state(self): self._enqueuing_timestamp = None self._enqueued_timestamp = None + def is_state_unset(self): + return ( + self._stage == DownloadStage.NOT_STARTED + and self._content_manifest is None + ) + def start(self, manifest, user): """Start downloading a collection manifest.""" if self._stage != DownloadStage.NOT_STARTED: @@ -265,6 +271,12 @@ def from_state(self, state): self._tasks_completed = state["tasks_completed"] self._tasks_previously_completed = state["tasks_previously_completed"] + logger.info( + f"Download state loaded for grade={grade} name={name}, " + + f"stage={stage_name}" + ) + logger.debug(f"Loaded download state: {state}") + def cancel(self): if self._current_job_id is not None: job_storage.cancel(self._current_job_id) @@ -733,5 +745,13 @@ def cancel_download(request): @api_view(["GET"]) def get_download_status(request): """Return the download status.""" + + saved_state = request.session.get("COLLECTIONS_STATE") + if ( + _collection_download_manager.is_state_unset() + and saved_state is not None + ): + _collection_download_manager.from_state(saved_state) + status = _collection_download_manager.get_status() return Response({"status": status})