Skip to content

Commit fd2d668

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 ed7bcd6 commit fd2d668

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
@@ -602,12 +602,15 @@ def _build_remotechannelimport_task(channel_id):
602602

603603
_content_manifests = []
604604
_content_manifests_by_grade_name = {}
605-
_collection_download_manager = CollectionDownloadManager()
605+
_collection_download_manager = None
606606

607607

608-
def _read_content_manifests():
608+
def _initiate():
609609
global _content_manifests
610610
global _content_manifests_by_grade_name
611+
global _collection_download_manager
612+
613+
_collection_download_manager = CollectionDownloadManager()
611614

612615
free_space_gb = get_free_space() / 1024**3
613616

@@ -631,7 +634,15 @@ def _create_manifest(grade, name):
631634
_create_manifest(grade, name)
632635

633636

634-
_read_content_manifests()
637+
def ensure_initiated(api_function):
638+
"""Decorator to initiate only once in the first API call."""
639+
640+
def wrapper(*args, **kwargs):
641+
if _collection_download_manager is None:
642+
_initiate()
643+
return api_function(*args, **kwargs)
644+
645+
return wrapper
635646

636647

637648
def _save_state_in_request_session(request):
@@ -670,6 +681,7 @@ def _get_channel_metadata(channel_id):
670681
return ChannelMetadata.objects.get(id=channel_id)
671682

672683

684+
@ensure_initiated
673685
@api_view(["GET"])
674686
def get_collection_info(request):
675687
"""Return the collection metadata and availability."""
@@ -679,6 +691,7 @@ def get_collection_info(request):
679691
return Response({"collectionInfo": collection_info})
680692

681693

694+
@ensure_initiated
682695
@api_view(["GET"])
683696
def get_all_collections_info(request):
684697
"""Return all the collections metadata and their availability."""
@@ -697,6 +710,7 @@ def get_all_collections_info(request):
697710
return Response({"allCollectionsInfo": info})
698711

699712

713+
@ensure_initiated
700714
@api_view(["GET"])
701715
def get_should_resume(request):
702716
"""Return if there is a saved state that should be resumed."""
@@ -715,6 +729,7 @@ def get_should_resume(request):
715729
)
716730

717731

732+
@ensure_initiated
718733
@api_view(["POST"])
719734
def start_download(request):
720735
"""Start downloading a collection.
@@ -756,6 +771,7 @@ def start_download(request):
756771
return Response({"status": status})
757772

758773

774+
@ensure_initiated
759775
@api_view(["POST"])
760776
def resume_download(request):
761777
"""Resume download from a previous session.
@@ -784,6 +800,7 @@ def resume_download(request):
784800
return Response({"status": status})
785801

786802

803+
@ensure_initiated
787804
@api_view(["POST"])
788805
def update_download(request):
789806
"""Continue downloading current collection.
@@ -806,6 +823,7 @@ def update_download(request):
806823
return Response({"status": status})
807824

808825

826+
@ensure_initiated
809827
@api_view(["DELETE"])
810828
def cancel_download(request):
811829
"""Cancel current download and clear the saved state.
@@ -825,6 +843,7 @@ def cancel_download(request):
825843
return Response({"status": status})
826844

827845

846+
@ensure_initiated
828847
@api_view(["GET"])
829848
def get_download_status(request):
830849
"""Return the download status."""

0 commit comments

Comments
 (0)