Skip to content
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

Bulk transfer students #311

Merged
merged 19 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name = "pypi"
# Before adding a new package, check it's not listed under [packages] at
# https://github.com/ocadotechnology/codeforlife-package-python/blob/{ref}/Pipfile
# Replace "{ref}" in the above URL with the ref set below.
codeforlife = {ref = "v0.14.0", git = "https://github.com/ocadotechnology/codeforlife-package-python.git"}
codeforlife = {ref = "bulk_transfer_students", git = "https://github.com/ocadotechnology/codeforlife-package-python.git"}
# TODO: check if we need the below packages
whitenoise = "==6.5.0"
django-pipeline = "==2.0.8"
Expand All @@ -34,7 +34,7 @@ google-cloud-container = "==2.3.0"
# Before adding a new package, check it's not listed under [dev-packages] at
# https://github.com/ocadotechnology/codeforlife-package-python/blob/{ref}/Pipfile
# Replace "{ref}" in the above URL with the ref set below.
codeforlife = {ref = "v0.14.0", git = "https://github.com/ocadotechnology/codeforlife-package-python.git", extras = ["dev"]}
codeforlife = {ref = "bulk_transfer_students", git = "https://github.com/ocadotechnology/codeforlife-package-python.git", extras = ["dev"]}
# TODO: check if we need the below packages
django-selenium-clean = "==0.3.3"
django-test-migrations = "==1.2.0"
Expand Down
120 changes: 60 additions & 60 deletions backend/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion backend/api/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
from .school_teacher_invitation import SchoolTeacherInvitationSerializer
from .student import StudentSerializer
from .teacher import TeacherSerializer
from .user import ReleaseStudentUserSerializer, UserSerializer
from .user import (
ReleaseStudentUserSerializer,
TransferStudentUserSerializer,
UserSerializer,
)
27 changes: 8 additions & 19 deletions backend/api/serializers/klass.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@

# pylint: disable-next=missing-class-docstring,too-many-ancestors
class ClassSerializer(_ClassSerializer):
teacher = serializers.IntegerField(
source="teacher.id",
required=False,
)

read_classmates_data = serializers.BooleanField(
source="classmates_data_viewable",
)
Expand All @@ -31,26 +26,20 @@ class Meta(_ClassSerializer.Meta):
extra_kwargs = {
**_ClassSerializer.Meta.extra_kwargs,
"name": {"read_only": False},
"teacher": {"required": False},
}

# pylint: disable-next=missing-function-docstring
def validate_teacher(self, value: int):
queryset = Teacher.objects.filter(id=value)
if not queryset.exists():
raise serializers.ValidationError(
"This teacher does not exist.",
code="does_not_exist",
)

def validate_teacher(self, value: Teacher):
user = self.request.school_teacher_user
if not queryset.filter(school=user.teacher.school_id).exists():
if value.school_id != user.teacher.school_id:
raise serializers.ValidationError(
"This teacher is not in your school.",
code="not_in_school",
)
if value != user.teacher.id and not user.teacher.is_admin:
if value != user.teacher and not user.teacher.is_admin:
raise serializers.ValidationError(
"Cannot assign another teacher if you're not admin.",
"Cannot assign another teacher if you're not an admin.",
code="not_admin",
)

Expand Down Expand Up @@ -87,10 +76,10 @@ def create(self, validated_data):
{
"access_code": access_code,
"name": validated_data["name"],
"teacher_id": (
validated_data["teacher"]["id"]
"teacher": (
validated_data["teacher"]
if "teacher" in validated_data
else self.request.school_teacher_user.teacher.id
else self.request.school_teacher_user.teacher
),
"classmates_data_viewable": validated_data[
"classmates_data_viewable"
Expand Down
10 changes: 0 additions & 10 deletions backend/api/serializers/student.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,3 @@ def validate_klass(self, value: str):
)

return value

def validate(self, attrs):
instance = t.cast(t.Optional[User], self.parent.instance)
if instance and not instance.student:
raise serializers.ValidationError(
"Target user is not a student.",
code="not_student",
)

return attrs
10 changes: 0 additions & 10 deletions backend/api/serializers/teacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,3 @@ def validate_is_admin(self, value: bool):
)

return value

def validate(self, attrs):
instance = t.cast(t.Optional[User], self.parent.instance)
if instance and not instance.teacher:
raise serializers.ValidationError(
"Target user is not a teacher.",
code="not_teacher",
)

return attrs
20 changes: 20 additions & 0 deletions backend/api/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,23 @@ def validate_email(self, value: str):
)

return value


class TransferStudentUserListSerializer(ModelListSerializer[StudentUser]):
def update(self, instance, validated_data):
for student_user, data in zip(instance, validated_data):
student_user.student.class_field = Class.objects.get(
access_code=data["new_student"]["class_field"]["access_code"]
)
student_user.student.save(update_fields=["class_field"])

return instance


class TransferStudentUserSerializer(_UserSerializer[StudentUser]):
"""Transfer a student to another class."""

student = StudentSerializer(source="new_student")

class Meta(_UserSerializer.Meta):
list_serializer_class = TransferStudentUserListSerializer
Loading
Loading