@@ -1054,7 +1054,7 @@ def add_user_to_school(request, school_id):
10541054
10551055 user_email = request .data .get ('user_email' )
10561056 user_role = request .data .get ('user_role' , 'student' )
1057- class_id = request .data .get ('class_id ' ) # For students
1057+ class_name = request .data .get ('class_name ' ) # For students
10581058
10591059 if not user_email :
10601060 return Response ({'error' : 'user_email is required' },
@@ -1092,20 +1092,22 @@ def add_user_to_school(request, school_id):
10921092 school = school ,
10931093 defaults = {'teacher_role' : 'subject_teacher' , 'status' : 'active' }
10941094 )
1095- elif user_role == 'student' and class_id :
1096- try :
1097- student_class = Class .objects .get (id = class_id , school = school )
1098- StudentProfile .objects .get_or_create (
1099- user = user ,
1100- school = school ,
1101- defaults = {
1102- 'student_id' : f"{ school .name [:3 ].upper ()} { user .id } " ,
1103- 'current_class' : student_class
1104- }
1105- )
1106- except Class .DoesNotExist :
1107- return Response ({'error' : 'Class not found in this school' },
1108- status = status .HTTP_400_BAD_REQUEST )
1095+ elif user_role == 'student' and class_name :
1096+ student_class , _ = Class .objects .get_or_create (
1097+ name = class_name ,
1098+ school = school ,
1099+ defaults = {
1100+ 'description' : f'{ class_name } class auto-created for { school .name } '
1101+ }
1102+ )
1103+ StudentProfile .objects .get_or_create (
1104+ user = user ,
1105+ school = school ,
1106+ defaults = {
1107+ 'student_id' : f"{ school .name [:3 ].upper ()} { user .id } " ,
1108+ 'current_class' : student_class
1109+ }
1110+ )
11091111
11101112 return Response ({
11111113 'message' : f'Successfully added { user .get_full_name ()} as { user_role } to { school .name } ' ,
0 commit comments