1313from django_email_learning .models import (
1414 ApiKey ,
1515 ContentDelivery ,
16+ CourseInstructor ,
1617 DeliveryStatus ,
1718 Organization ,
1819 ImapConnection ,
@@ -118,6 +119,11 @@ class CreateCourseRequest(BaseModel):
118119 ],
119120 )
120121 is_public : bool = Field (default = True , examples = [True ])
122+ instructors : Optional [list [int ]] = Field (
123+ None ,
124+ examples = [[1 , 2 , 3 ]],
125+ description = "List of organization user IDs to be assigned as instructors for this course." ,
126+ )
121127
122128 def to_django_model (self , organization_id : int ) -> Course :
123129 organization = Organization .objects .get (id = organization_id )
@@ -146,6 +152,22 @@ def to_django_model(self, organization_id: int) -> Course:
146152 )
147153 if imap_connection :
148154 course .imap_connection = imap_connection
155+ if self .instructors :
156+ course .save () # Save course before adding instructors
157+ for instructor_id in self .instructors :
158+ try :
159+ org_user = OrganizationUser .objects .get (
160+ id = instructor_id , organization = organization
161+ )
162+ except OrganizationUser .DoesNotExist :
163+ raise ValueError (
164+ f"OrganizationUser with id { instructor_id } does not exist in organization { organization .name } ."
165+ )
166+ if not org_user .can_act_as_instructor ():
167+ raise ValueError (
168+ f"OrganizationUser with id { instructor_id } does not have instructor role."
169+ )
170+ CourseInstructor .objects .create (course = course , org_user = org_user )
149171 if self .image :
150172 course .replace_image (self .image )
151173 if self .target_audience :
@@ -189,6 +211,7 @@ class UpdateCourseRequest(BaseModel):
189211 ],
190212 )
191213 is_public : Optional [bool ] = Field (None , examples = [True ])
214+ instructors : Optional [list [int ]] = Field (None , examples = [1 , 2 , 3 ])
192215
193216 def to_django_model (self , course_id : int ) -> Course :
194217 try :
@@ -227,9 +250,39 @@ def to_django_model(self, course_id: int) -> Course:
227250 course .external_references .create (name = ref ["name" ], url = ref ["url" ])
228251 if self .is_public is not None :
229252 course .is_public = self .is_public
253+ if self .instructors is not None :
254+ instructors_to_remove = course .instructors .exclude (
255+ org_user_id__in = self .instructors
256+ )
257+ for instructor in instructors_to_remove :
258+ instructor .delete ()
259+ instructors_to_add = set (self .instructors ) - set (
260+ course .instructors .values_list ("org_user_id" , flat = True )
261+ )
262+ for instructor_id in instructors_to_add :
263+ try :
264+ org_user = OrganizationUser .objects .get (
265+ id = instructor_id , organization = course .organization
266+ )
267+ except OrganizationUser .DoesNotExist :
268+ raise ValueError (
269+ f"OrganizationUser with id { instructor_id } does not exist in organization { course .organization .name } ."
270+ )
271+ if not org_user .can_act_as_instructor ():
272+ raise ValueError (
273+ f"OrganizationUser with id { instructor_id } does not have instructor role."
274+ )
275+ CourseInstructor .objects .create (course = course , org_user = org_user )
230276 return course
231277
232278
279+ class InstructorResponse (BaseModel ):
280+ id : int
281+ email : str
282+
283+ model_config = ConfigDict (from_attributes = True )
284+
285+
233286class CourseResponse (BaseModel ):
234287 id : int
235288 title : str
@@ -246,6 +299,7 @@ class CourseResponse(BaseModel):
246299 target_audience : Optional [str ] = None
247300 external_references : Optional [list [dict [str , str ]]] = None
248301 is_public : bool
302+ instructors : Optional [list [InstructorResponse ]] = None
249303
250304 model_config = ConfigDict (from_attributes = True )
251305
@@ -278,6 +332,12 @@ def from_django_model(
278332 if course .external_references .exists ()
279333 else None ,
280334 "is_public" : course .is_public ,
335+ "instructors" : [
336+ InstructorResponse (
337+ id = instructor .org_user .id , email = instructor .org_user .user .email
338+ )
339+ for instructor in course .instructors .all ()
340+ ],
281341 }
282342 )
283343
@@ -450,6 +510,7 @@ class OrganizationUserResponse(BaseModel):
450510 organization_id : int
451511 email : str
452512 role : UserRole
513+ can_act_as_instructor : bool
453514
454515 @staticmethod
455516 def from_django_model (org_user : OrganizationUser ) -> "OrganizationUserResponse" :
@@ -459,6 +520,7 @@ def from_django_model(org_user: OrganizationUser) -> "OrganizationUserResponse":
459520 organization_id = org_user .organization .id ,
460521 email = org_user .user .email ,
461522 role = UserRole (org_user .role ),
523+ can_act_as_instructor = org_user .can_act_as_instructor (),
462524 )
463525
464526
0 commit comments