From d4c7a495afe5232c47fc706af445bf68d4b01d86 Mon Sep 17 00:00:00 2001 From: SKairinos Date: Mon, 29 Jan 2024 09:45:03 +0000 Subject: [PATCH] user serializers correctly for remaining models --- codeforlife/user/serializers/klass.py | 39 ++++++++++++++++++------- codeforlife/user/serializers/school.py | 14 ++++----- codeforlife/user/serializers/teacher.py | 2 ++ 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/codeforlife/user/serializers/klass.py b/codeforlife/user/serializers/klass.py index 1398db3b..1f6cb7f8 100644 --- a/codeforlife/user/serializers/klass.py +++ b/codeforlife/user/serializers/klass.py @@ -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 @@ -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}, } diff --git a/codeforlife/user/serializers/school.py b/codeforlife/user/serializers/school.py index 52163149..80db312d 100644 --- a/codeforlife/user/serializers/school.py +++ b/codeforlife/user/serializers/school.py @@ -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 @@ -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}, } diff --git a/codeforlife/user/serializers/teacher.py b/codeforlife/user/serializers/teacher.py index cf15e29f..ea12c250 100644 --- a/codeforlife/user/serializers/teacher.py +++ b/codeforlife/user/serializers/teacher.py @@ -19,4 +19,6 @@ class Meta: ] extra_kwargs = { "id": {"read_only": True}, + "school": {"read_only": True}, + "is_admin": {"read_only": True}, }