3434
3535def create_collection (
3636 learning_package_id : LearningPackage .ID ,
37- key : str ,
37+ collection_code : str ,
3838 * ,
3939 title : str ,
4040 created_by : int | None ,
@@ -44,35 +44,37 @@ def create_collection(
4444 """
4545 Create a new Collection
4646 """
47- collection = Collection . objects . create (
47+ collection = Collection (
4848 learning_package_id = learning_package_id ,
49- key = key ,
49+ collection_code = collection_code ,
5050 title = title ,
5151 created_by_id = created_by ,
5252 description = description ,
5353 enabled = enabled ,
5454 )
55+ collection .full_clean ()
56+ collection .save ()
5557 return collection
5658
5759
58- def get_collection (learning_package_id : LearningPackage .ID , collection_key : str ) -> Collection :
60+ def get_collection (learning_package_id : LearningPackage .ID , collection_code : str ) -> Collection :
5961 """
6062 Get a Collection by ID
6163 """
62- return Collection .objects .get_by_key (learning_package_id , collection_key )
64+ return Collection .objects .get_by_code (learning_package_id , collection_code )
6365
6466
6567def update_collection (
6668 learning_package_id : LearningPackage .ID ,
67- key : str ,
69+ collection_code : str ,
6870 * ,
6971 title : str | None = None ,
7072 description : str | None = None ,
7173) -> Collection :
7274 """
73- Update a Collection identified by the learning_package_id + key .
75+ Update a Collection identified by the learning_package_id + collection_code .
7476 """
75- collection = get_collection (learning_package_id , key )
77+ collection = get_collection (learning_package_id , collection_code )
7678
7779 # If no changes were requested, there's nothing to update, so just return
7880 # the Collection as-is
@@ -90,17 +92,17 @@ def update_collection(
9092
9193def delete_collection (
9294 learning_package_id : LearningPackage .ID ,
93- key : str ,
95+ collection_code : str ,
9496 * ,
9597 hard_delete = False ,
9698) -> Collection :
9799 """
98- Disables or deletes a collection identified by the given learning_package + key .
100+ Disables or deletes a collection identified by the given learning_package + collection_code .
99101
100102 By default (hard_delete=False), the collection is "soft deleted", i.e disabled.
101103 Soft-deleted collections can be re-enabled using restore_collection.
102104 """
103- collection = get_collection (learning_package_id , key )
105+ collection = get_collection (learning_package_id , collection_code )
104106
105107 if hard_delete :
106108 collection .delete ()
@@ -112,12 +114,12 @@ def delete_collection(
112114
113115def restore_collection (
114116 learning_package_id : LearningPackage .ID ,
115- key : str ,
117+ collection_code : str ,
116118) -> Collection :
117119 """
118120 Undo a "soft delete" by re-enabling a Collection.
119121 """
120- collection = get_collection (learning_package_id , key )
122+ collection = get_collection (learning_package_id , collection_code )
121123
122124 collection .enabled = True
123125 collection .save ()
@@ -126,7 +128,7 @@ def restore_collection(
126128
127129def add_to_collection (
128130 learning_package_id : LearningPackage .ID ,
129- key : str ,
131+ collection_code : str ,
130132 entities_qset : QuerySet [PublishableEntity ],
131133 created_by : int | None = None ,
132134) -> Collection :
@@ -146,10 +148,10 @@ def add_to_collection(
146148 if invalid_entity :
147149 raise ValidationError (
148150 f"Cannot add entity { invalid_entity .id } in learning package { invalid_entity .learning_package_id } "
149- f"to collection { key } in learning package { learning_package_id } ."
151+ f"to collection { collection_code } in learning package { learning_package_id } ."
150152 )
151153
152- collection = get_collection (learning_package_id , key )
154+ collection = get_collection (learning_package_id , collection_code )
153155 collection .entities .add (
154156 * entities_qset .all (),
155157 through_defaults = {"created_by_id" : created_by },
@@ -162,7 +164,7 @@ def add_to_collection(
162164
163165def remove_from_collection (
164166 learning_package_id : LearningPackage .ID ,
165- key : str ,
167+ collection_code : str ,
166168 entities_qset : QuerySet [PublishableEntity ],
167169) -> Collection :
168170 """
@@ -174,7 +176,7 @@ def remove_from_collection(
174176
175177 Returns the updated Collection.
176178 """
177- collection = get_collection (learning_package_id , key )
179+ collection = get_collection (learning_package_id , collection_code )
178180
179181 collection .entities .remove (* entities_qset .all ())
180182 collection .modified = datetime .now (tz = timezone .utc )
@@ -198,7 +200,7 @@ def get_entity_collections(learning_package_id: LearningPackage.ID, entity_key:
198200
199201def get_collection_entities (
200202 learning_package_id : LearningPackage .ID ,
201- collection_key : str ,
203+ collection_code : str ,
202204) -> QuerySet [PublishableEntity ]:
203205 """
204206 Returns a QuerySet of PublishableEntities in a Collection.
@@ -207,7 +209,7 @@ def get_collection_entities(
207209 """
208210 return PublishableEntity .objects .filter (
209211 learning_package_id = learning_package_id ,
210- collections__key = collection_key ,
212+ collections__collection_code = collection_code ,
211213 ).order_by ("pk" )
212214
213215
0 commit comments