Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jan 22, 2024
1 parent 6e2716a commit 7238bbc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
28 changes: 21 additions & 7 deletions codeforlife/user/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

from ...serializers import ModelSerializer
from ..models import User
from ..models import User, Student, Teacher
from .student import StudentSerializer
from .teacher import TeacherSerializer

Expand Down Expand Up @@ -43,17 +43,31 @@ class Meta:
}

def to_representation(self, instance):
try:
student = (
StudentSerializer(instance.new_student).data
if instance.new_student and instance.new_student.class_field
else None
)
except Student.DoesNotExist:
student = None

try:
teacher = (
TeacherSerializer(instance.new_teacher).data
if instance.new_teacher
else None
)
except Teacher.DoesNotExist:
teacher = None

return {
"id": instance.id,
"first_name": instance.first_name,
"last_name": instance.last_name,
"email": instance.email,
"is_active": instance.is_active,
"date_joined": instance.date_joined,
"student": StudentSerializer(instance.student).data
if instance.student and instance.student.class_field
else None,
"teacher": TeacherSerializer(instance.teacher).data
if instance.teacher
else None,
"student": student,
"teacher": teacher,
}
13 changes: 8 additions & 5 deletions codeforlife/user/tests/views/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def test_retrieve__admin_teacher__student__same_school__not_same_class(

def test_retrieve__student__teacher__same_school__same_class(self):
"""
Student cannot retrieve a teacher from the same school and class.
Student can retrieve a teacher from the same school and class.
"""

user = self._login_student()
Expand All @@ -263,7 +263,7 @@ def test_retrieve__student__teacher__same_school__same_class(self):
same_class=True,
)

self.client.retrieve(other_user, status.HTTP_404_NOT_FOUND)
self.client.retrieve(other_user)

def test_retrieve__student__teacher__same_school__not_same_class(self):
"""
Expand Down Expand Up @@ -482,9 +482,12 @@ def test_list__student(self):
user = self._login_student()

self.client.list(
User.objects.filter(
new_student__class_field=user.student.class_field
)
[
user.student.class_field.teacher.new_user,
*User.objects.filter(
new_student__class_field=user.student.class_field
),
]
)

def test_list__indy_student(self):
Expand Down
7 changes: 6 additions & 1 deletion codeforlife/user/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ def get_queryset(self):
if user.student.class_field is None:
return User.objects.filter(id=user.id)

return User.objects.filter(
teachers = User.objects.filter(
new_teacher=user.student.class_field.teacher
)
students = User.objects.filter(
new_student__class_field=user.student.class_field
)

return teachers | students

teachers = User.objects.filter(
new_teacher__school=user.teacher.school_id
)
Expand Down

0 comments on commit 7238bbc

Please sign in to comment.