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

Portal frontend 51 #363

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions src/api/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@


class BaseUserSerializer(_BaseUserSerializer[AnyUser], t.Generic[AnyUser]):
def validate_first_name(self, value: str):
user = self.instance
if user and user.student and user.student.class_field:
if User.objects.filter(
first_name=value,
new_student__class_field=user.student.class_field,
).exists():
raise serializers.ValidationError(
"A student in this class already has this name.",
code="student_name_in_class",
)

return value

# TODO: make email unique in new models and remove this validation.
def validate_email(self, value: str):
user = User.objects.filter(email__iexact=value).first()
Expand Down
22 changes: 22 additions & 0 deletions src/api/serializers/user_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
User,
)
from django.contrib.auth.hashers import make_password
from django.db.models import Count

from ..auth import password_reset_token_generator
from .user import (
Expand All @@ -37,6 +38,27 @@ class TestBaseUserSerializer(ModelSerializerTestCase[User, User]):
model_serializer_class = BaseUserSerializer[User]
# fixtures = ["school_1"]

def test_validate_first_name__student_name_in_class(self):
"""
Cannot assign a student-user a name that already exists in their class.
"""
klass = (
Class.objects.annotate(student_count=Count("students"))
.filter(student_count__gte=2)
.first()
)
assert klass
students = klass.students.all()
student_user_1 = StudentUser.objects.get(new_student=students[0])
student_user_2 = StudentUser.objects.get(new_student=students[1])

self.assert_validate_field(
name="first_name",
value=student_user_2.first_name,
error_code="student_name_in_class",
instance=student_user_1,
)

def test_validate_email__already_exists(self):
"""Cannot assign a user an email that already exists."""
user_fields = User.objects.values("email").first()
Expand Down
7 changes: 7 additions & 0 deletions src/api/views/student.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ def get_permissions(self):
def get_queryset(self):
return self.request.school_teacher_user.teacher.students

def get_serializer_context(self):
serializer_context = super().get_serializer_context()
if self.action == "reset_password":
serializer_context["user_type"] = "student"

return serializer_context

def get_serializer_class(self):
if self.action == "release":
return ReleaseStudentSerializer
Expand Down
9 changes: 9 additions & 0 deletions src/api/views/student_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,15 @@ def test_get_queryset__reset_password__non_admin(self):
request_method="put",
)

# test: get serializer context

def test_get_serializer_context__reset_password(self):
"""Serializer context sets user type as student."""
self.assert_get_serializer_context(
serializer_context={"user_type": "student"},
action="reset_password",
)

# test: get serializer class

def test_get_serializer_class__release(self):
Expand Down