3131
3232from ..collections import api as collections_api
3333from ..components import api as components_api
34+ from ..containers import api as containers_api
3435from ..media import api as media_api
3536from ..publishing import api as publishing_api
36- from ..sections import api as sections_api
37- from ..subsections import api as subsections_api
38- from ..units import api as units_api
37+ from ..sections . models import Section
38+ from ..subsections . models import Subsection
39+ from ..units . models import Unit
3940from .serializers import (
4041 CollectionSerializer ,
4142 ComponentSerializer ,
@@ -804,70 +805,70 @@ def _save_components(self, learning_package, components, component_static_files)
804805 ** valid_published
805806 )
806807
807- def _save_units (self , learning_package , containers ):
808- """Save units and published unit versions."""
809- for valid_unit in containers .get ("unit" , []):
810- entity_key = valid_unit .get ("key" )
811- unit = units_api .create_unit (learning_package .id , created_by = self .user_id , ** valid_unit )
812- self .units_map_by_key [entity_key ] = unit
808+ def _save_container (
809+ self ,
810+ learning_package ,
811+ containers ,
812+ * ,
813+ container_cls : containers_api .ContainerSubclass ,
814+ container_map : dict ,
815+ children_map : dict ,
816+ ):
817+ """Internal logic for _save_units, _save_subsections, and _save_sections"""
818+ type_code = container_cls .type_code # e.g. "unit"
819+ for data in containers .get (type_code , []):
820+ entity_key = data .get ("key" )
821+ container = containers_api .create_container (
822+ learning_package .id ,
823+ ** data , # should this be allowed to override any of the following fields?
824+ created_by = self .user_id ,
825+ container_cls = container_cls ,
826+ )
827+ container_map [entity_key ] = container # e.g. `self.units_map_by_key[entity_key] = unit`
813828
814- for valid_published in containers .get ("unit_published " , []):
829+ for valid_published in containers .get (f" { type_code } _published " , []):
815830 entity_key = valid_published .pop ("entity_key" )
816- children = self ._resolve_children (valid_published , self . components_map_by_key )
831+ children = self ._resolve_children (valid_published , children_map )
817832 self .all_published_entities_versions .add (
818833 (entity_key , valid_published .get ('version_num' ))
819834 ) # Track published version
820- units_api .create_next_unit_version (
821- self .units_map_by_key [entity_key ],
835+ containers_api .create_next_container_version (
836+ container_map [entity_key ],
837+ ** valid_published , # should this be allowed to override any of the following fields?
822838 force_version_num = valid_published .pop ("version_num" , None ),
823- components = children ,
839+ entities = children ,
824840 created_by = self .user_id ,
825- ** valid_published
826841 )
827842
843+ def _save_units (self , learning_package , containers ):
844+ """Save units and published unit versions."""
845+ self ._save_container (
846+ learning_package ,
847+ containers ,
848+ container_cls = Unit ,
849+ container_map = self .units_map_by_key ,
850+ children_map = self .components_map_by_key ,
851+ )
852+
828853 def _save_subsections (self , learning_package , containers ):
829854 """Save subsections and published subsection versions."""
830- for valid_subsection in containers .get ("subsection" , []):
831- entity_key = valid_subsection .get ("key" )
832- subsection = subsections_api .create_subsection (
833- learning_package .id , created_by = self .user_id , ** valid_subsection
834- )
835- self .subsections_map_by_key [entity_key ] = subsection
836-
837- for valid_published in containers .get ("subsection_published" , []):
838- entity_key = valid_published .pop ("entity_key" )
839- children = self ._resolve_children (valid_published , self .units_map_by_key )
840- self .all_published_entities_versions .add (
841- (entity_key , valid_published .get ('version_num' ))
842- ) # Track published version
843- subsections_api .create_next_subsection_version (
844- self .subsections_map_by_key [entity_key ],
845- units = children ,
846- force_version_num = valid_published .pop ("version_num" , None ),
847- created_by = self .user_id ,
848- ** valid_published
849- )
855+ self ._save_container (
856+ learning_package ,
857+ containers ,
858+ container_cls = Subsection ,
859+ container_map = self .subsections_map_by_key ,
860+ children_map = self .units_map_by_key ,
861+ )
850862
851863 def _save_sections (self , learning_package , containers ):
852864 """Save sections and published section versions."""
853- for valid_section in containers .get ("section" , []):
854- entity_key = valid_section .get ("key" )
855- section = sections_api .create_section (learning_package .id , created_by = self .user_id , ** valid_section )
856- self .sections_map_by_key [entity_key ] = section
857-
858- for valid_published in containers .get ("section_published" , []):
859- entity_key = valid_published .pop ("entity_key" )
860- children = self ._resolve_children (valid_published , self .subsections_map_by_key )
861- self .all_published_entities_versions .add (
862- (entity_key , valid_published .get ('version_num' ))
863- ) # Track published version
864- sections_api .create_next_section_version (
865- self .sections_map_by_key [entity_key ],
866- subsections = children ,
867- force_version_num = valid_published .pop ("version_num" , None ),
868- created_by = self .user_id ,
869- ** valid_published
870- )
865+ self ._save_container (
866+ learning_package ,
867+ containers ,
868+ container_cls = Section ,
869+ container_map = self .sections_map_by_key ,
870+ children_map = self .subsections_map_by_key ,
871+ )
871872
872873 def _save_draft_versions (self , components , containers , component_static_files ):
873874 """Save draft versions for all entity types."""
@@ -888,47 +889,29 @@ def _save_draft_versions(self, components, containers, component_static_files):
888889 ** valid_draft
889890 )
890891
891- for valid_draft in containers .get ("unit_drafts" , []):
892- entity_key = valid_draft .pop ("entity_key" )
893- version_num = valid_draft ["version_num" ] # Should exist, validated earlier
894- if self ._is_version_already_exists (entity_key , version_num ):
895- continue
896- children = self ._resolve_children (valid_draft , self .components_map_by_key )
897- units_api .create_next_unit_version (
898- self .units_map_by_key [entity_key ],
899- components = children ,
900- force_version_num = valid_draft .pop ("version_num" , None ),
901- created_by = self .user_id ,
902- ** valid_draft
903- )
904-
905- for valid_draft in containers .get ("subsection_drafts" , []):
906- entity_key = valid_draft .pop ("entity_key" )
907- version_num = valid_draft ["version_num" ] # Should exist, validated earlier
908- if self ._is_version_already_exists (entity_key , version_num ):
909- continue
910- children = self ._resolve_children (valid_draft , self .units_map_by_key )
911- subsections_api .create_next_subsection_version (
912- self .subsections_map_by_key [entity_key ],
913- units = children ,
914- force_version_num = valid_draft .pop ("version_num" , None ),
915- created_by = self .user_id ,
916- ** valid_draft
917- )
892+ def _process_draft_containers (
893+ container_cls : containers_api .ContainerSubclass ,
894+ container_map : dict ,
895+ children_map : dict ,
896+ ):
897+ for valid_draft in containers .get (f"{ container_cls .type_code } _drafts" , []):
898+ entity_key = valid_draft .pop ("entity_key" )
899+ version_num = valid_draft ["version_num" ] # Should exist, validated earlier
900+ if self ._is_version_already_exists (entity_key , version_num ):
901+ continue
902+ children = self ._resolve_children (valid_draft , children_map )
903+ del valid_draft ["version_num" ]
904+ containers_api .create_next_container_version (
905+ container_map [entity_key ],
906+ ** valid_draft , # should this be allowed to override any of the following fields?
907+ entities = children ,
908+ force_version_num = version_num ,
909+ created_by = self .user_id ,
910+ )
918911
919- for valid_draft in containers .get ("section_drafts" , []):
920- entity_key = valid_draft .pop ("entity_key" )
921- version_num = valid_draft ["version_num" ] # Should exist, validated earlier
922- if self ._is_version_already_exists (entity_key , version_num ):
923- continue
924- children = self ._resolve_children (valid_draft , self .subsections_map_by_key )
925- sections_api .create_next_section_version (
926- self .sections_map_by_key [entity_key ],
927- subsections = children ,
928- force_version_num = valid_draft .pop ("version_num" , None ),
929- created_by = self .user_id ,
930- ** valid_draft
931- )
912+ _process_draft_containers (Unit , self .units_map_by_key , children_map = self .components_map_by_key )
913+ _process_draft_containers (Subsection , self .subsections_map_by_key , children_map = self .units_map_by_key )
914+ _process_draft_containers (Section , self .sections_map_by_key , children_map = self .subsections_map_by_key )
932915
933916 # --------------------------
934917 # Utilities
0 commit comments