2020from django .http import HttpResponse , HttpResponseBadRequest
2121from django .utils .translation import gettext as _
2222from edx_django_utils .plugins import pluggable_override
23- from openedx .core .djangoapps .content_libraries .api import LibraryXBlockMetadata
23+ from openedx .core .djangoapps .content_libraries .api import ContainerMetadata , ContainerType , LibraryXBlockMetadata
2424from openedx .core .djangoapps .content_tagging .api import get_object_tag_counts
2525from edx_proctoring .api import (
2626 does_backend_support_onboarding ,
@@ -533,6 +533,7 @@ def sync_library_content(downstream: XBlock, request, store) -> StaticFileNotice
533533 Handle syncing library content for given xblock depending on its upstream type.
534534 It can sync unit containers and lower level xblocks.
535535 """
536+ # CHECK: Sync library content for given xblock depending on its upstream type.
536537 link = UpstreamLink .get_for_block (downstream )
537538 upstream_key = link .upstream_key
538539 if isinstance (upstream_key , LibraryUsageLocatorV2 ):
@@ -548,28 +549,54 @@ def sync_library_content(downstream: XBlock, request, store) -> StaticFileNotice
548549 notices = []
549550 # Store final children keys to update order of components in unit
550551 children = []
552+
551553 for i , upstream_child in enumerate (upstream_children ):
552- assert isinstance (upstream_child , LibraryXBlockMetadata ) # for now we only support units
553- if upstream_child .usage_key not in downstream_children_keys :
554+ if isinstance (upstream_child , LibraryXBlockMetadata ):
555+ upstream_key = upstream_child .usage_key
556+ block_type = upstream_child .usage_key .block_type
557+ elif isinstance (upstream_child , ContainerMetadata ):
558+ upstream_key = upstream_child .container_key
559+ match upstream_child .container_type :
560+ case ContainerType .Unit :
561+ block_type = "vertical"
562+ case ContainerType .Subsection :
563+ block_type = "sequential"
564+ case _:
565+ # We don't support other container types for now.
566+ log .error (
567+ "Unexpected upstream child container type: %s" ,
568+ upstream_child .container_type ,
569+ )
570+ continue
571+ else :
572+ log .error (
573+ "Unexpected type of upstream child: %s" ,
574+ type (upstream_child ),
575+ )
576+ continue
577+
578+ if upstream_key not in downstream_children_keys :
554579 # This upstream_child is new, create it.
555580 downstream_child = store .create_child (
556581 parent_usage_key = downstream .usage_key ,
557582 position = i ,
558583 user_id = request .user .id ,
559- block_type = upstream_child . usage_key . block_type ,
584+ block_type = block_type ,
560585 # TODO: Can we generate a unique but friendly block_id, perhaps using upstream block_id
561- block_id = f"{ upstream_child . usage_key . block_type } { uuid4 ().hex [:8 ]} " ,
586+ block_id = f"{ block_type } { uuid4 ().hex [:8 ]} " ,
562587 fields = {
563- "upstream" : str (upstream_child . usage_key ),
588+ "upstream" : str (upstream_key ),
564589 },
565590 )
566591 else :
567- downstream_child_old_index = downstream_children_keys .index (upstream_child . usage_key )
592+ downstream_child_old_index = downstream_children_keys .index (upstream_key )
568593 downstream_child = downstream_children [downstream_child_old_index ]
569594
570- result = sync_library_content (downstream = downstream_child , request = request , store = store )
571595 children .append (downstream_child .usage_key )
596+
597+ result = sync_library_content (downstream = downstream_child , request = request , store = store )
572598 notices .append (result )
599+
573600 for child in downstream_children :
574601 if child .usage_key not in children :
575602 # This downstream block was added, or deleted from upstream block.
@@ -634,6 +661,7 @@ def _create_block(request):
634661 status = 400 ,
635662 )
636663
664+ # CHECK: Add container to course
637665 created_block = create_xblock (
638666 parent_locator = parent_locator ,
639667 user = request .user ,
0 commit comments