From 809269a8eb8e755b7a865dc3d984ebc1bcab2d9b Mon Sep 17 00:00:00 2001 From: SKairinos Date: Fri, 19 Jan 2024 12:17:07 +0000 Subject: [PATCH] data access layer --- codeforlife/tests/api.py | 14 ++++++-- codeforlife/user/serializers/klass.py | 24 ++++++++++--- codeforlife/user/serializers/school.py | 18 ++++++++-- codeforlife/user/serializers/student.py | 14 +++++++- codeforlife/user/serializers/teacher.py | 8 ++++- codeforlife/user/serializers/user.py | 48 +++++++++++++++++++++++-- 6 files changed, 113 insertions(+), 13 deletions(-) diff --git a/codeforlife/tests/api.py b/codeforlife/tests/api.py index b68eb10..6a0e9ac 100644 --- a/codeforlife/tests/api.py +++ b/codeforlife/tests/api.py @@ -1,4 +1,5 @@ import typing as t +from datetime import datetime from unittest.mock import patch from django.db.models import Model @@ -103,8 +104,17 @@ def assert_data_equals_model( model: AnyModel, model_serializer_class: t.Type[AnyModelSerializer], ): - assert ( - data == model_serializer_class(model).data + def parse_data(data): + if isinstance(data, list): + return [parse_data(value) for value in data] + if isinstance(data, dict): + return {key: parse_data(value) for key, value in data.items()} + if isinstance(data, datetime): + return data.strftime("%Y-%m-%dT%H:%M:%S.%fZ") + return data + + assert data == parse_data( + model_serializer_class(model).data ), "Data does not equal serialized model." def retrieve( diff --git a/codeforlife/user/serializers/klass.py b/codeforlife/user/serializers/klass.py index 79c6b0c..0c06bbb 100644 --- a/codeforlife/user/serializers/klass.py +++ b/codeforlife/user/serializers/klass.py @@ -3,13 +3,29 @@ from ..models import Class +# pylint: disable-next=missing-class-docstring class ClassSerializer(serializers.ModelSerializer[Class]): + # pylint: disable-next=missing-class-docstring,too-few-public-methods class Meta: model = Class - fields = "__all__" + fields = [ + "id", + "teacher", + "school", + "name", + "read_classmates_data", + "receive_requests_until", + ] extra_kwargs = { "id": {"read_only": True}, - "access_code": {"read_only": True}, - "creation_time": {"read_only": True}, - "created_by": {"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, } diff --git a/codeforlife/user/serializers/school.py b/codeforlife/user/serializers/school.py index 16fbafa..3d96a93 100644 --- a/codeforlife/user/serializers/school.py +++ b/codeforlife/user/serializers/school.py @@ -3,11 +3,25 @@ from ..models import School +# pylint: disable-next=missing-class-docstring class SchoolSerializer(serializers.ModelSerializer[School]): + # pylint: disable-next=missing-class-docstring,too-few-public-methods class Meta: model = School - fields = "__all__" + fields = [ + "id", + "name", + "country", + "uk_county", + ] extra_kwargs = { "id": {"read_only": True}, - "creation_time": {"read_only": True}, + } + + def to_representation(self, instance): + return { + "id": instance.id, + "name": instance.name, + "country": str(instance.country), + "uk_county": instance.county, } diff --git a/codeforlife/user/serializers/student.py b/codeforlife/user/serializers/student.py index 1a109ec..45389e8 100644 --- a/codeforlife/user/serializers/student.py +++ b/codeforlife/user/serializers/student.py @@ -3,10 +3,22 @@ from ..models import Student +# pylint: disable-next=missing-class-docstring class StudentSerializer(serializers.ModelSerializer[Student]): + # pylint: disable-next=missing-class-docstring,too-few-public-methods class Meta: model = Student - fields = "__all__" + fields = [ + "id", + "klass", + ] extra_kwargs = { "id": {"read_only": True}, } + + def to_representation(self, instance): + return { + "id": instance.id, + "klass": instance.class_field.access_code, + "school": instance.class_field.teacher.school.pk, + } diff --git a/codeforlife/user/serializers/teacher.py b/codeforlife/user/serializers/teacher.py index 2c3aa26..61f0b90 100644 --- a/codeforlife/user/serializers/teacher.py +++ b/codeforlife/user/serializers/teacher.py @@ -3,10 +3,16 @@ from ..models import Teacher +# pylint: disable-next=missing-class-docstring class TeacherSerializer(serializers.ModelSerializer[Teacher]): + # pylint: disable-next=missing-class-docstring,too-few-public-methods class Meta: model = Teacher - fields = "__all__" + fields = [ + "id", + "school", + "is_admin", + ] extra_kwargs = { "id": {"read_only": True}, } diff --git a/codeforlife/user/serializers/user.py b/codeforlife/user/serializers/user.py index b5230b0..9f8e05e 100644 --- a/codeforlife/user/serializers/user.py +++ b/codeforlife/user/serializers/user.py @@ -1,3 +1,8 @@ +""" +© Ocado Group +Created on 19/01/2024 at 11:06:00(+00:00). +""" + from rest_framework import serializers from ..models import User @@ -5,14 +10,51 @@ from .teacher import TeacherSerializer +# pylint: disable-next=missing-class-docstring class UserSerializer(serializers.ModelSerializer[User]): - student = StudentSerializer(source="new_student") - teacher = TeacherSerializer(source="new_teacher") + student = StudentSerializer( + source="new_student", + read_only=True, + ) + + teacher = TeacherSerializer( + source="new_teacher", + read_only=True, + ) + # pylint: disable-next=missing-class-docstring,too-few-public-methods class Meta: model = User - fields = "__all__" + fields = [ + "student", + "teacher", + "id", + "password", + "first_name", + "last_name", + "email", + "is_active", + "date_joined", + ] extra_kwargs = { "id": {"read_only": True}, "password": {"write_only": True}, + "is_active": {"read_only": True}, + "date_joined": {"read_only": True}, + } + + def to_representation(self, instance): + 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, }