-
Notifications
You must be signed in to change notification settings - Fork 0
implement add teacher and student to school #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| from django.views.decorators.http import require_GET | ||
| from django.core.mail import send_mail | ||
| import random | ||
| import logging | ||
|
|
||
| from rest_framework import viewsets, status, permissions, filters | ||
| from rest_framework.decorators import action, api_view, permission_classes | ||
|
|
@@ -32,7 +33,7 @@ | |
| ProjectParticipant | ||
| ) | ||
| from .serializers import ( | ||
| UserRegistrationSerializer, UserSerializer, UserUpdateSerializer, | ||
| SchoolAddStudentSerializer, SchoolAddTeacherSerializer, UserRegistrationSerializer, UserSerializer, UserUpdateSerializer, | ||
| PasswordChangeSerializer, SchoolSerializer, SchoolCreateSerializer, | ||
| SchoolMembershipSerializer, SubjectSerializer, ClassSerializer, | ||
| TeacherProfileSerializer, StudentProfileSerializer, ProjectSerializer, | ||
|
|
@@ -1123,6 +1124,168 @@ def add_user_to_school(request, school_id): | |
| status=status.HTTP_500_INTERNAL_SERVER_ERROR) | ||
|
|
||
|
|
||
| @api_view(['POST']) | ||
| @permission_classes([CanManageSchoolMembers]) | ||
| def add_student_to_school(request, school_id): | ||
| """ | ||
| Add a student to a school. | ||
| Only school admins and teachers can add students to their schools. | ||
| """ | ||
| try: | ||
| school = get_object_or_404(School, id=school_id) | ||
|
|
||
| # Check permission | ||
| if (school.admin != request.user or request.user.role != "teacher") and not request.user.is_staff: | ||
| return Response({'error': 'Only school admins and teachers can add students to schools'}, | ||
| status=status.HTTP_403_FORBIDDEN) | ||
|
|
||
| serializer_class = SchoolAddStudentSerializer(data=request.data) | ||
| if not serializer_class.is_valid(): | ||
| return Response(serializer_class.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
|
||
| data = serializer_class.validated_data | ||
| student_email = data['student_email'] | ||
| assigned_class_name = data['assigned_class'] | ||
|
|
||
| try: | ||
| user = User.objects.get(email=student_email) | ||
| if user.role and user.role != "student": | ||
| return Response({'error': 'User already has a role other than student.'}, | ||
| status=status.HTTP_400_BAD_REQUEST) | ||
| elif not user.role: | ||
| user.role = "student" | ||
| user.save() | ||
| except User.DoesNotExist: | ||
| return Response({'error': 'User not found'}, | ||
| status=status.HTTP_404_NOT_FOUND) | ||
|
|
||
| # Create school membership | ||
| membership, created = SchoolMembership.objects.get_or_create( | ||
| user=user, | ||
| school=school, | ||
| defaults={'is_active': True} | ||
| ) | ||
|
|
||
| if not created and membership.is_active: | ||
| return Response({'error': 'User is already a member of this school'}, | ||
| status=status.HTTP_400_BAD_REQUEST) | ||
| elif not created: | ||
| membership.is_active = True | ||
| membership.save() | ||
|
|
||
| try: | ||
| assigned_class = Class.objects.get(name=assigned_class_name, school=school) | ||
| except Class.DoesNotExist: | ||
| return Response({'error': 'Class not found in this school'}, status=status.HTTP_400_BAD_REQUEST) | ||
|
|
||
| # Create appropriate profile | ||
| StudentProfile.objects.get_or_create( | ||
| user=user, | ||
| school=school, | ||
| defaults={ | ||
| 'student_id': f"{school.name[:3].upper()}{user.id}", | ||
| 'current_class': assigned_class | ||
| } | ||
| ) | ||
|
|
||
| return Response({ | ||
| 'message': f'Successfully added {user.get_full_name()} as {user.role} to {school.name}', | ||
| 'user_id': str(user.id), | ||
| 'user_name': user.get_full_name(), | ||
| 'user_role': user.role, | ||
| 'school_name': school.name | ||
| }, status=status.HTTP_200_OK) | ||
|
|
||
| except Exception as e: | ||
| logger.error(f"Error adding student to school: {str(e)}") | ||
| return Response({'error': f'Failed to add student to school: {str(e)}'}, | ||
| status=status.HTTP_500_INTERNAL_SERVER_ERROR) | ||
|
|
||
|
|
||
| @api_view(['POST']) | ||
| @permission_classes([CanManageSchoolMembers]) | ||
| def add_teacher_to_school(request, school_id): | ||
| """ | ||
| Add a teacher to a school. | ||
| Only school admins can add teachers to their schools. | ||
| """ | ||
| try: | ||
| school = get_object_or_404(School, id=school_id) | ||
|
|
||
| # Check permission | ||
| if school.admin != request.user and not request.user.is_staff: | ||
| return Response({'error': 'Only school admins can add teachers to schools'}, | ||
| status=status.HTTP_403_FORBIDDEN) | ||
|
|
||
| serializer_class = SchoolAddTeacherSerializer(data=request.data) | ||
| if not serializer_class.is_valid(): | ||
| return Response(serializer_class.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
|
||
| data = serializer_class.validated_data | ||
| teacher_email = data['teacher_email'] | ||
| teacher_role = data['teacher_role'] | ||
| assigned_class_names = data.get('assigned_classes') | ||
|
|
||
| try: | ||
| user = User.objects.get(email=teacher_email) | ||
| if user.role and user.role != "teacher": | ||
| return Response({'error': 'User already has a role other than teacher.'}, | ||
| status=status.HTTP_400_BAD_REQUEST) | ||
| elif not user.role: | ||
| user.role = "teacher" | ||
| user.save() | ||
| except User.DoesNotExist: | ||
| return Response({'error': 'User not found'}, | ||
| status=status.HTTP_404_NOT_FOUND) | ||
|
|
||
| # Create school membership | ||
| membership, created = SchoolMembership.objects.get_or_create( | ||
| user=user, | ||
| school=school, | ||
| defaults={'is_active': True} | ||
| ) | ||
|
|
||
| if not created and membership.is_active: | ||
| return Response({'error': 'User is already a member of this school'}, | ||
| status=status.HTTP_400_BAD_REQUEST) | ||
| elif not created: | ||
| membership.is_active = True | ||
| membership.save() | ||
|
|
||
| # Create appropriate profile | ||
| teacher_profile, _ = TeacherProfile.objects.get_or_create( | ||
| user=user, | ||
| school=school, | ||
| defaults={'teacher_role': teacher_role, 'status': 'active'} | ||
| ) | ||
| # Query classes matching names and this school | ||
| valid_class_names = Class.objects.filter(name__in=assigned_class_names, school=school).values_list('name', flat=True) | ||
|
|
||
| # Find invalid class names | ||
| invalid_classes = set(assigned_class_names) - set(valid_class_names) | ||
| if invalid_classes: | ||
| return Response( | ||
| {'error': f'The following classes are invalid or do not belong to the school: {", ".join(invalid_classes)}'}, | ||
| status=status.HTTP_400_BAD_REQUEST | ||
| ) | ||
|
|
||
| valid_classes_qs = Class.objects.filter(name__in=assigned_class_names, school=school) | ||
| teacher_profile.assigned_classes.set(valid_classes_qs) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Missing Default Breaks Class FilteringWhen |
||
|
|
||
| return Response({ | ||
| 'message': f'Successfully added {user.get_full_name()} as {user.role} to {school.name}', | ||
| 'user_id': str(user.id), | ||
| 'user_name': user.get_full_name(), | ||
| 'user_role': teacher_role, | ||
| 'school_name': school.name | ||
| }, status=status.HTTP_200_OK) | ||
|
|
||
| except Exception as e: | ||
| logger.error(f"Error adding teacher to school: {str(e)}") | ||
| return Response({'error': f'Failed to add teacher to school: {str(e)}'}, | ||
| status=status.HTTP_500_INTERNAL_SERVER_ERROR) | ||
|
|
||
|
|
||
| @api_view(['POST']) | ||
| @permission_classes([CanManageSchoolContent]) | ||
| def add_student_to_class(request, class_id): | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Faulty Logic Denies Teacher Student Access
The permission check uses
orinstead ofand, preventing teachers from adding students. The condition(school.admin != request.user or request.user.role != "teacher")evaluates to True when the user is a teacher but not the admin, incorrectly denying access. This contradicts the docstring stating "school admins and teachers can add students".