From b5614767e729b8e7172d4085dc68e603598915d3 Mon Sep 17 00:00:00 2001 From: SKairinos Date: Mon, 2 Sep 2024 08:55:36 +0000 Subject: [PATCH] read user serializer --- Pipfile.lock | 4 ++-- src/api/serializers/__init__.py | 1 + src/api/serializers/user.py | 40 +++++++++++++++++++++++++++++++++ src/api/views/user.py | 3 ++- src/api/views/user_test.py | 6 ++--- src/urls.py | 20 ----------------- 6 files changed, 48 insertions(+), 26 deletions(-) diff --git a/Pipfile.lock b/Pipfile.lock index d1ebbb57..14fa957e 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -137,7 +137,7 @@ }, "codeforlife": { "git": "https://github.com/ocadotechnology/codeforlife-package-python.git", - "ref": "04013c096caf3cc80f1449ac1f7ab82cf37ef275" + "ref": "7b7e42182821f6fbf884f49feacb32d088b87a5a" }, "codeforlife-portal": { "hashes": [ @@ -947,7 +947,7 @@ }, "codeforlife": { "git": "https://github.com/ocadotechnology/codeforlife-package-python.git", - "ref": "04013c096caf3cc80f1449ac1f7ab82cf37ef275" + "ref": "7b7e42182821f6fbf884f49feacb32d088b87a5a" }, "codeforlife-portal": { "hashes": [ diff --git a/src/api/serializers/__init__.py b/src/api/serializers/__init__.py index 8229ed88..f368472e 100644 --- a/src/api/serializers/__init__.py +++ b/src/api/serializers/__init__.py @@ -25,6 +25,7 @@ from .user import ( CreateUserSerializer, HandleIndependentUserJoinClassRequestSerializer, + ReadUserSerializer, RegisterEmailToNewsletter, RequestUserPasswordResetSerializer, ResetUserPasswordSerializer, diff --git a/src/api/serializers/user.py b/src/api/serializers/user.py index 935fa8d2..90612e97 100644 --- a/src/api/serializers/user.py +++ b/src/api/serializers/user.py @@ -12,14 +12,18 @@ Class, ContactableUser, IndependentUser, + SchoolTeacher, Student, StudentUser, Teacher, + TeacherUser, User, ) from codeforlife.user.serializers import ( BaseUserSerializer as _BaseUserSerializer, ) +from codeforlife.user.serializers import ClassSerializer as _ClassSerializer +from codeforlife.user.serializers import TeacherSerializer as _TeacherSerializer from codeforlife.user.serializers import UserSerializer as _UserSerializer from django.conf import settings from django.contrib.auth.password_validation import ( @@ -435,3 +439,39 @@ def create(self, validated_data): user.add_contact_to_dot_digital() return user + + +class ReadUserSerializer(_UserSerializer[User]): + class ClassSerializer(_ClassSerializer): + class TeacherSerializer(_TeacherSerializer[SchoolTeacher]): + user = _BaseUserSerializer[TeacherUser]( + source="new_user", read_only=True + ) + + class Meta(_TeacherSerializer.Meta): + fields = [*_TeacherSerializer.Meta.fields, "user"] + + teacher = TeacherSerializer(read_only=True) + + requesting_to_join_class = ClassSerializer( # type: ignore[assignment] + source="new_student.pending_class_request", + read_only=True, + ) + + def to_representation(self, instance): + representation = super().to_representation(instance) + + try: + if ( + instance.new_student + and instance.new_student.pending_class_request + ): + representation[ + "requesting_to_join_class" + ] = self.ClassSerializer( + instance.new_student.pending_class_request + ).data + except Student.DoesNotExist: + pass + + return representation diff --git a/src/api/views/user.py b/src/api/views/user.py index 751f6fbe..8e98df71 100644 --- a/src/api/views/user.py +++ b/src/api/views/user.py @@ -29,6 +29,7 @@ from ..serializers import ( CreateUserSerializer, HandleIndependentUserJoinClassRequestSerializer, + ReadUserSerializer, RegisterEmailToNewsletter, RequestUserPasswordResetSerializer, ResetUserPasswordSerializer, @@ -96,7 +97,7 @@ def get_serializer_class(self): if self.action == "register_to_newsletter": return RegisterEmailToNewsletter - return super().get_serializer_class() + return ReadUserSerializer def get_queryset(self, user_class=User): if self.action in ["reset_password", "verify_email_address"]: diff --git a/src/api/views/user_test.py b/src/api/views/user_test.py index 6fb788cd..573f6394 100644 --- a/src/api/views/user_test.py +++ b/src/api/views/user_test.py @@ -31,7 +31,6 @@ UserProfile, ) from codeforlife.user.permissions import IsIndependent, IsTeacher -from codeforlife.user.serializers import UserSerializer from django.conf import settings from django.contrib.auth.hashers import make_password from django.contrib.auth.tokens import ( @@ -45,6 +44,7 @@ from ..serializers import ( CreateUserSerializer, HandleIndependentUserJoinClassRequestSerializer, + ReadUserSerializer, RegisterEmailToNewsletter, RequestUserPasswordResetSerializer, ResetUserPasswordSerializer, @@ -303,14 +303,14 @@ def test_get_serializer_class__partial_update(self): def test_get_serializer_class__retrieve(self): """Retrieving a user uses the general serializer.""" self.assert_get_serializer_class( - serializer_class=UserSerializer, + serializer_class=ReadUserSerializer, action="retrieve", ) def test_get_serializer_class__list(self): """Listing users uses the general serializer.""" self.assert_get_serializer_class( - serializer_class=UserSerializer, + serializer_class=ReadUserSerializer, action="list", ) diff --git a/src/urls.py b/src/urls.py index 80655674..9c61ded3 100644 --- a/src/urls.py +++ b/src/urls.py @@ -14,13 +14,8 @@ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ -from aimmo import urls as aimmo_urls # type: ignore[import-untyped] from codeforlife.urls import get_urlpatterns from django.urls import include, path -from portal.views.aimmo.dashboard import ( # type: ignore[import-untyped] - StudentAimmoDashboard, - TeacherAimmoDashboard, -) from .api.urls import urlpatterns @@ -30,21 +25,6 @@ include("game.urls"), name="rapidrouter", ), - path( - "teach/kurono/dashboard/", - TeacherAimmoDashboard.as_view(), - name="teacher_aimmo_dashboard", - ), - path( - "play/kurono/dashboard/", - StudentAimmoDashboard.as_view(), - name="student_aimmo_dashboard", - ), - path( - "kurono/", - include(aimmo_urls), - name="kurono", - ), *get_urlpatterns(urlpatterns, include_user_urls=False), path( "api/sso/",