Skip to content

Commit 385a398

Browse files
Merge branch 'main' into fix-split-add-user
2 parents bd230c1 + 7d31ed7 commit 385a398

File tree

3 files changed

+42
-33
lines changed

3 files changed

+42
-33
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Generated by Django 4.2.7 on 2025-10-24 02:43
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('core', '0011_alter_class_options_alter_class_description_and_more'),
10+
('core', '0011_alter_user_role'),
11+
]
12+
13+
operations = [
14+
]

core/urls.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@
7676
path('projects/<uuid:pk>/join/', views.ProjectViewSet.as_view({'post': 'join'}), name='project-join'),
7777
path('projects/<uuid:pk>/impacts/', views.ProjectViewSet.as_view({'get': 'impacts'}), name='project-impacts'),
7878
path('projects/<uuid:project_id>/add-class/<uuid:class_id>/', views.add_class_to_project, name='add-class-to-project'),
79-
80-
# =================================================================
81-
# CUSTOM CLASS ENDPOINTS
82-
# =================================================================
83-
path('classes/class-choices/', views.get_class_choices, name='class-choices'),
8479

8580
# =================================================================
8681
# CUSTOM SCHOOL ENDPOINTS

core/views.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Handles all REST API endpoints for the application
44
"""
55

6+
import logging
67
from django.shortcuts import get_object_or_404
78
from django.contrib.auth import authenticate
89
from django.db.models import Count, Sum, Q
@@ -50,11 +51,10 @@
5051
CanManageProjectParticipants, CanUploadProjectProgress
5152
)
5253
from .filters import ProjectFilter, SchoolFilter, EnvironmentalImpactFilter
53-
from rest_framework.permissions import AllowAny
5454
from rest_framework import serializers
5555
from 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-
14281428
from .additional_views import *

0 commit comments

Comments
 (0)