Skip to content

Commit

Permalink
user serializers correctly for remaining models
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jan 29, 2024
1 parent ddf2ce5 commit d4c7a49
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
39 changes: 28 additions & 11 deletions codeforlife/user/serializers/klass.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,39 @@
Created on 20/01/2024 at 11:28:29(+00:00).
"""

from rest_framework import serializers

from ...serializers import ModelSerializer
from ..models import Class


# pylint: disable-next=missing-class-docstring
class ClassSerializer(ModelSerializer[Class]):
id = serializers.CharField(
source="access_code",
read_only=True,
)

read_classmates_data = serializers.BooleanField(
source="classmates_data_viewable",
read_only=True,
)

receive_requests_until = serializers.DateTimeField(
source="accept_requests_until",
read_only=True,
)

teacher = serializers.IntegerField(
source="teacher.id",
read_only=True,
)

school = serializers.IntegerField(
source="teacher.school.id",
read_only=True,
)

# pylint: disable-next=missing-class-docstring,too-few-public-methods
class Meta:
model = Class
Expand All @@ -21,15 +48,5 @@ class Meta:
"receive_requests_until",
]
extra_kwargs = {
"id": {"read_only": True},
}

def to_representation(self, instance):
return {
"id": instance.access_code,
"name": instance.name,
"read_classmates_data": instance.classmates_data_viewable,
"receive_requests_until": instance.accept_requests_until,
"teacher": instance.teacher.pk,
"school": instance.teacher.school.pk,
"name": {"read_only": True},
}
14 changes: 6 additions & 8 deletions codeforlife/user/serializers/school.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
Created on 20/01/2024 at 11:28:19(+00:00).
"""

from rest_framework import serializers

from ...serializers import ModelSerializer
from ..models import School


# pylint: disable-next=missing-class-docstring
class SchoolSerializer(ModelSerializer[School]):
uk_county = serializers.CharField(source="county", read_only=True)

# pylint: disable-next=missing-class-docstring,too-few-public-methods
class Meta:
model = School
Expand All @@ -20,12 +24,6 @@ class Meta:
]
extra_kwargs = {
"id": {"read_only": True},
}

def to_representation(self, instance):
return {
"id": instance.id,
"name": instance.name,
"country": str(instance.country),
"uk_county": instance.county,
"name": {"read_only": True},
"country": {"read_only": True},
}
2 changes: 2 additions & 0 deletions codeforlife/user/serializers/teacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ class Meta:
]
extra_kwargs = {
"id": {"read_only": True},
"school": {"read_only": True},
"is_admin": {"read_only": True},
}

0 comments on commit d4c7a49

Please sign in to comment.