Skip to content

Commit c16fc0f

Browse files
committed
Collection download: Initiate on first API call
Previously the manifest files were read from disk so it didn't matter to read them all on module import. But now they are fetched from the Internet. So add a decorator to all API views for initiating the global variables and fetch collections only once on first API call.
1 parent 66e5845 commit c16fc0f

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

kolibri_explore_plugin/collectionviews.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -565,12 +565,15 @@ def _build_remotechannelimport_task(channel_id):
565565

566566
_content_manifests = []
567567
_content_manifests_by_grade_name = {}
568-
_collection_download_manager = CollectionDownloadManager()
568+
_collection_download_manager = None
569569

570570

571-
def _read_content_manifests():
571+
def _initiate():
572572
global _content_manifests
573573
global _content_manifests_by_grade_name
574+
global _collection_download_manager
575+
576+
_collection_download_manager = CollectionDownloadManager()
574577

575578
free_space_gb = get_free_space() / 1024**3
576579

@@ -594,7 +597,15 @@ def _create_manifest(grade, name):
594597
_create_manifest(grade, name)
595598

596599

597-
_read_content_manifests()
600+
def ensure_initiated(api_function):
601+
"""Decorator to initiate only once in the first API call."""
602+
603+
def wrapper(*args, **kwargs):
604+
if _collection_download_manager is None:
605+
_initiate()
606+
return api_function(*args, **kwargs)
607+
608+
return wrapper
598609

599610

600611
def _save_state_in_request_session(request):
@@ -629,6 +640,7 @@ def _get_channel_ids_for_all_content_manifests():
629640
return channel_ids
630641

631642

643+
@ensure_initiated
632644
@api_view(["GET"])
633645
def get_collection_info(request):
634646
"""Return the collection metadata and availability."""
@@ -638,6 +650,7 @@ def get_collection_info(request):
638650
return Response({"collectionInfo": collection_info})
639651

640652

653+
@ensure_initiated
641654
@api_view(["GET"])
642655
def get_all_collections_info(request):
643656
"""Return all the collections metadata and their availability."""
@@ -656,6 +669,7 @@ def get_all_collections_info(request):
656669
return Response({"allCollectionsInfo": info})
657670

658671

672+
@ensure_initiated
659673
@api_view(["GET"])
660674
def get_should_resume(request):
661675
"""Return if there is a saved state that should be resumed."""
@@ -674,6 +688,7 @@ def get_should_resume(request):
674688
)
675689

676690

691+
@ensure_initiated
677692
@api_view(["POST"])
678693
def start_download(request):
679694
"""Start downloading a collection.
@@ -715,6 +730,7 @@ def start_download(request):
715730
return Response({"status": status})
716731

717732

733+
@ensure_initiated
718734
@api_view(["POST"])
719735
def resume_download(request):
720736
"""Resume download from a previous session.
@@ -743,6 +759,7 @@ def resume_download(request):
743759
return Response({"status": status})
744760

745761

762+
@ensure_initiated
746763
@api_view(["POST"])
747764
def update_download(request):
748765
"""Continue downloading current collection.
@@ -765,6 +782,7 @@ def update_download(request):
765782
return Response({"status": status})
766783

767784

785+
@ensure_initiated
768786
@api_view(["DELETE"])
769787
def cancel_download(request):
770788
"""Cancel current download and clear the saved state.
@@ -784,6 +802,7 @@ def cancel_download(request):
784802
return Response({"status": status})
785803

786804

805+
@ensure_initiated
787806
@api_view(["GET"])
788807
def get_download_status(request):
789808
"""Return the download status."""

0 commit comments

Comments
 (0)