3333
3434def create_collection (
3535 learning_package_id : int ,
36- key : str ,
36+ collection_code : str ,
3737 * ,
3838 title : str ,
3939 created_by : int | None ,
@@ -43,35 +43,37 @@ def create_collection(
4343 """
4444 Create a new Collection
4545 """
46- collection = Collection . objects . create (
46+ collection = Collection (
4747 learning_package_id = learning_package_id ,
48- key = key ,
48+ collection_code = collection_code ,
4949 title = title ,
5050 created_by_id = created_by ,
5151 description = description ,
5252 enabled = enabled ,
5353 )
54+ collection .full_clean ()
55+ collection .save ()
5456 return collection
5557
5658
57- def get_collection (learning_package_id : int , collection_key : str ) -> Collection :
59+ def get_collection (learning_package_id : int , collection_code : str ) -> Collection :
5860 """
5961 Get a Collection by ID
6062 """
61- return Collection .objects .get_by_key (learning_package_id , collection_key )
63+ return Collection .objects .get_by_code (learning_package_id , collection_code )
6264
6365
6466def update_collection (
6567 learning_package_id : int ,
66- key : str ,
68+ collection_code : str ,
6769 * ,
6870 title : str | None = None ,
6971 description : str | None = None ,
7072) -> Collection :
7173 """
72- Update a Collection identified by the learning_package_id + key .
74+ Update a Collection identified by the learning_package_id + collection_code .
7375 """
74- collection = get_collection (learning_package_id , key )
76+ collection = get_collection (learning_package_id , collection_code )
7577
7678 # If no changes were requested, there's nothing to update, so just return
7779 # the Collection as-is
@@ -89,17 +91,17 @@ def update_collection(
8991
9092def delete_collection (
9193 learning_package_id : int ,
92- key : str ,
94+ collection_code : str ,
9395 * ,
9496 hard_delete = False ,
9597) -> Collection :
9698 """
97- Disables or deletes a collection identified by the given learning_package + key .
99+ Disables or deletes a collection identified by the given learning_package + collection_code .
98100
99101 By default (hard_delete=False), the collection is "soft deleted", i.e disabled.
100102 Soft-deleted collections can be re-enabled using restore_collection.
101103 """
102- collection = get_collection (learning_package_id , key )
104+ collection = get_collection (learning_package_id , collection_code )
103105
104106 if hard_delete :
105107 collection .delete ()
@@ -111,12 +113,12 @@ def delete_collection(
111113
112114def restore_collection (
113115 learning_package_id : int ,
114- key : str ,
116+ collection_code : str ,
115117) -> Collection :
116118 """
117119 Undo a "soft delete" by re-enabling a Collection.
118120 """
119- collection = get_collection (learning_package_id , key )
121+ collection = get_collection (learning_package_id , collection_code )
120122
121123 collection .enabled = True
122124 collection .save ()
@@ -125,7 +127,7 @@ def restore_collection(
125127
126128def add_to_collection (
127129 learning_package_id : int ,
128- key : str ,
130+ collection_code : str ,
129131 entities_qset : QuerySet [PublishableEntity ],
130132 created_by : int | None = None ,
131133) -> Collection :
@@ -145,10 +147,10 @@ def add_to_collection(
145147 if invalid_entity :
146148 raise ValidationError (
147149 f"Cannot add entity { invalid_entity .pk } in learning package { invalid_entity .learning_package_id } "
148- f"to collection { key } in learning package { learning_package_id } ."
150+ f"to collection { collection_code } in learning package { learning_package_id } ."
149151 )
150152
151- collection = get_collection (learning_package_id , key )
153+ collection = get_collection (learning_package_id , collection_code )
152154 collection .entities .add (
153155 * entities_qset .all (),
154156 through_defaults = {"created_by_id" : created_by },
@@ -161,7 +163,7 @@ def add_to_collection(
161163
162164def remove_from_collection (
163165 learning_package_id : int ,
164- key : str ,
166+ collection_code : str ,
165167 entities_qset : QuerySet [PublishableEntity ],
166168) -> Collection :
167169 """
@@ -173,7 +175,7 @@ def remove_from_collection(
173175
174176 Returns the updated Collection.
175177 """
176- collection = get_collection (learning_package_id , key )
178+ collection = get_collection (learning_package_id , collection_code )
177179
178180 collection .entities .remove (* entities_qset .all ())
179181 collection .modified = datetime .now (tz = timezone .utc )
0 commit comments