Skip to content

Commit 7d31ed7

Browse files
Merge pull request #7 from Cooperation-org/fix-class-choices-router
fix custom view implementation for class choices
2 parents cd8d60f + 0a21857 commit 7d31ed7

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

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: 29 additions & 27 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
@@ -49,10 +50,11 @@
4950
CanManageProjectParticipants, CanUploadProjectProgress
5051
)
5152
from .filters import ProjectFilter, SchoolFilter, EnvironmentalImpactFilter
52-
from rest_framework.permissions import AllowAny
5353
from rest_framework import serializers
5454
from 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-
12631265
from .additional_views import *

0 commit comments

Comments
 (0)