33Handles all REST API endpoints for the application
44"""
55
6+ import logging
67from django .shortcuts import get_object_or_404
78from django .contrib .auth import authenticate
89from django .db .models import Count , Sum , Q
5051 CanManageProjectParticipants , CanUploadProjectProgress
5152)
5253from .filters import ProjectFilter , SchoolFilter , EnvironmentalImpactFilter
53- from rest_framework .permissions import AllowAny
5454from rest_framework import serializers
5555from rest_framework .exceptions import PermissionDenied
5656
57- logger = logging .getLogger (" __name__" )
57+ logger = logging .getLogger (__name__ )
5858
5959
6060# =============================================================================
@@ -400,6 +400,15 @@ class ClassViewSet(viewsets.ModelViewSet):
400400 filterset_fields = ['school' ]
401401 search_fields = ['name' ]
402402
403+ @action (detail = False , methods = ['get' ], url_path = 'class-choices' )
404+ def class_choices (self , request ):
405+ """Return available class name choices"""
406+ choices = [
407+ {"value" : choice [0 ], "label" : choice [1 ]}
408+ for choice in Class .ClassName .choices
409+ ]
410+ return Response (choices )
411+
403412
404413# =============================================================================
405414# PROFILE VIEWSETS
@@ -1046,7 +1055,7 @@ def add_user_to_school(request, school_id):
10461055
10471056 user_email = request .data .get ('user_email' )
10481057 user_role = request .data .get ('user_role' , 'student' )
1049- class_id = request .data .get ('class_id ' ) # For students
1058+ class_name = request .data .get ('class_name ' ) # For students
10501059
10511060 if not user_email :
10521061 return Response ({'error' : 'user_email is required' },
@@ -1084,20 +1093,22 @@ def add_user_to_school(request, school_id):
10841093 school = school ,
10851094 defaults = {'teacher_role' : 'subject_teacher' , 'status' : 'active' }
10861095 )
1087- elif user_role == 'student' and class_id :
1088- try :
1089- student_class = Class .objects .get (id = class_id , school = school )
1090- StudentProfile .objects .get_or_create (
1091- user = user ,
1092- school = school ,
1093- defaults = {
1094- 'student_id' : f"{ school .name [:3 ].upper ()} { user .id } " ,
1095- 'current_class' : student_class
1096- }
1097- )
1098- except Class .DoesNotExist :
1099- return Response ({'error' : 'Class not found in this school' },
1100- status = status .HTTP_400_BAD_REQUEST )
1096+ elif user_role == 'student' and class_name :
1097+ student_class , _ = Class .objects .get_or_create (
1098+ name = class_name ,
1099+ school = school ,
1100+ defaults = {
1101+ 'description' : f'{ class_name } class auto-created for { school .name } '
1102+ }
1103+ )
1104+ StudentProfile .objects .get_or_create (
1105+ user = user ,
1106+ school = school ,
1107+ defaults = {
1108+ 'student_id' : f"{ school .name [:3 ].upper ()} { user .id } " ,
1109+ 'current_class' : student_class
1110+ }
1111+ )
11011112
11021113 return Response ({
11031114 'message' : f'Successfully added { user .get_full_name ()} as { user_role } to { school .name } ' ,
@@ -1414,15 +1425,4 @@ def check_school_exists(request):
14141425 })
14151426
14161427
1417- @api_view (['GET' ])
1418- @permission_classes ([permissions .AllowAny ])
1419- def get_class_choices (request ):
1420- """Return available class name choices"""
1421- choices = [
1422- {"value" : choice [0 ], "label" : choice [1 ]}
1423- for choice in Class .ClassName .choices
1424- ]
1425- return Response (choices )
1426-
1427-
14281428from .additional_views import *
0 commit comments