Skip to content

Commit

Permalink
data access layer
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jan 19, 2024
1 parent 1ff7142 commit 809269a
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 13 deletions.
14 changes: 12 additions & 2 deletions codeforlife/tests/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import typing as t
from datetime import datetime
from unittest.mock import patch

from django.db.models import Model
Expand Down Expand Up @@ -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(
Expand Down
24 changes: 20 additions & 4 deletions codeforlife/user/serializers/klass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
18 changes: 16 additions & 2 deletions codeforlife/user/serializers/school.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
14 changes: 13 additions & 1 deletion codeforlife/user/serializers/student.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
8 changes: 7 additions & 1 deletion codeforlife/user/serializers/teacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}
48 changes: 45 additions & 3 deletions codeforlife/user/serializers/user.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,60 @@
"""
© Ocado Group
Created on 19/01/2024 at 11:06:00(+00:00).
"""

from rest_framework import serializers

from ..models import User
from .student import StudentSerializer
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,
}

0 comments on commit 809269a

Please sign in to comment.