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
4950 CanManageProjectParticipants , CanUploadProjectProgress
5051)
5152from .filters import ProjectFilter , SchoolFilter , EnvironmentalImpactFilter
52- from rest_framework .permissions import AllowAny
5353from rest_framework import serializers
5454from rest_framework .exceptions import PermissionDenied
5555
56+ logger = logging .getLogger (__name__ )
57+
5658
5759# =============================================================================
5860# AUTHENTICATION & LOGIN VIEWS (Grouped at the top for clarity)
@@ -397,6 +399,15 @@ class ClassViewSet(viewsets.ModelViewSet):
397399 filterset_fields = ['school' ]
398400 search_fields = ['name' ]
399401
402+ @action (detail = False , methods = ['get' ], url_path = 'class-choices' )
403+ def class_choices (self , request ):
404+ """Return available class name choices"""
405+ choices = [
406+ {"value" : choice [0 ], "label" : choice [1 ]}
407+ for choice in Class .ClassName .choices
408+ ]
409+ return Response (choices )
410+
400411
401412# =============================================================================
402413# PROFILE VIEWSETS
@@ -1043,7 +1054,7 @@ def add_user_to_school(request, school_id):
10431054
10441055 user_email = request .data .get ('user_email' )
10451056 user_role = request .data .get ('user_role' , 'student' )
1046- class_id = request .data .get ('class_id ' ) # For students
1057+ class_name = request .data .get ('class_name ' ) # For students
10471058
10481059 if not user_email :
10491060 return Response ({'error' : 'user_email is required' },
@@ -1081,20 +1092,22 @@ def add_user_to_school(request, school_id):
10811092 school = school ,
10821093 defaults = {'teacher_role' : 'subject_teacher' , 'status' : 'active' }
10831094 )
1084- elif user_role == 'student' and class_id :
1085- try :
1086- student_class = Class .objects .get (id = class_id , school = school )
1087- StudentProfile .objects .get_or_create (
1088- user = user ,
1089- school = school ,
1090- defaults = {
1091- 'student_id' : f"{ school .name [:3 ].upper ()} { user .id } " ,
1092- 'current_class' : student_class
1093- }
1094- )
1095- except Class .DoesNotExist :
1096- return Response ({'error' : 'Class not found in this school' },
1097- 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+ )
10981111
10991112 return Response ({
11001113 'message' : f'Successfully added { user .get_full_name ()} as { user_role } to { school .name } ' ,
@@ -1249,15 +1262,4 @@ def check_school_exists(request):
12491262 })
12501263
12511264
1252- @api_view (['GET' ])
1253- @permission_classes ([permissions .AllowAny ])
1254- def get_class_choices (request ):
1255- """Return available class name choices"""
1256- choices = [
1257- {"value" : choice [0 ], "label" : choice [1 ]}
1258- for choice in Class .ClassName .choices
1259- ]
1260- return Response (choices )
1261-
1262-
12631265from .additional_views import *
0 commit comments