Skip to content

Commit

Permalink
read user serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Sep 2, 2024
1 parent 101d889 commit b561476
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Pipfile.lock

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

1 change: 1 addition & 0 deletions src/api/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .user import (
CreateUserSerializer,
HandleIndependentUserJoinClassRequestSerializer,
ReadUserSerializer,
RegisterEmailToNewsletter,
RequestUserPasswordResetSerializer,
ResetUserPasswordSerializer,
Expand Down
40 changes: 40 additions & 0 deletions src/api/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)

Check warning on line 462 in src/api/serializers/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/serializers/user.py#L462

Added line #L462 was not covered by tests

try:
if (

Check warning on line 465 in src/api/serializers/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/serializers/user.py#L464-L465

Added lines #L464 - L465 were not covered by tests
instance.new_student
and instance.new_student.pending_class_request
):
representation[

Check warning on line 469 in src/api/serializers/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/serializers/user.py#L469

Added line #L469 was not covered by tests
"requesting_to_join_class"
] = self.ClassSerializer(
instance.new_student.pending_class_request
).data
except Student.DoesNotExist:
pass

Check warning on line 475 in src/api/serializers/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/serializers/user.py#L474-L475

Added lines #L474 - L475 were not covered by tests

return representation

Check warning on line 477 in src/api/serializers/user.py

View check run for this annotation

Codecov / codecov/patch

src/api/serializers/user.py#L477

Added line #L477 was not covered by tests
3 changes: 2 additions & 1 deletion src/api/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from ..serializers import (
CreateUserSerializer,
HandleIndependentUserJoinClassRequestSerializer,
ReadUserSerializer,
RegisterEmailToNewsletter,
RequestUserPasswordResetSerializer,
ResetUserPasswordSerializer,
Expand Down Expand Up @@ -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"]:
Expand Down
6 changes: 3 additions & 3 deletions src/api/views/user_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -45,6 +44,7 @@
from ..serializers import (
CreateUserSerializer,
HandleIndependentUserJoinClassRequestSerializer,
ReadUserSerializer,
RegisterEmailToNewsletter,
RequestUserPasswordResetSerializer,
ResetUserPasswordSerializer,
Expand Down Expand Up @@ -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",
)

Expand Down
20 changes: 0 additions & 20 deletions src/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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/",
Expand Down

0 comments on commit b561476

Please sign in to comment.