From 6d9e1fbe3e7e49124ab34f667a89790346fd4db7 Mon Sep 17 00:00:00 2001 From: SKairinos Date: Sat, 20 Jan 2024 11:29:44 +0000 Subject: [PATCH] base model serializer --- codeforlife/serializers/__init__.py | 6 ++++++ codeforlife/serializers/base.py | 21 +++++++++++++++++++++ codeforlife/user/serializers/klass.py | 8 ++++++-- codeforlife/user/serializers/school.py | 8 ++++++-- codeforlife/user/serializers/student.py | 8 ++++++-- codeforlife/user/serializers/teacher.py | 8 ++++++-- codeforlife/user/serializers/user.py | 5 ++--- 7 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 codeforlife/serializers/__init__.py create mode 100644 codeforlife/serializers/base.py diff --git a/codeforlife/serializers/__init__.py b/codeforlife/serializers/__init__.py new file mode 100644 index 00000000..4a685c70 --- /dev/null +++ b/codeforlife/serializers/__init__.py @@ -0,0 +1,6 @@ +""" +© Ocado Group +Created on 20/01/2024 at 11:19:12(+00:00). +""" + +from .base import * diff --git a/codeforlife/serializers/base.py b/codeforlife/serializers/base.py new file mode 100644 index 00000000..8f3ea630 --- /dev/null +++ b/codeforlife/serializers/base.py @@ -0,0 +1,21 @@ +""" +© Ocado Group +Created on 20/01/2024 at 11:19:24(+00:00). + +Base model serializers. +""" + +import typing as t + +from django.db.models import Model +from rest_framework.serializers import ModelSerializer as _ModelSerializer + +AnyModel = t.TypeVar("AnyModel", bound=Model) + + +class ModelSerializer(_ModelSerializer[AnyModel], t.Generic[AnyModel]): + """Base model serializer for all model serializers.""" + + # pylint: disable-next=useless-parent-delegation + def update(self, instance, validated_data: t.Dict[str, t.Any]): + return super().update(instance, validated_data) diff --git a/codeforlife/user/serializers/klass.py b/codeforlife/user/serializers/klass.py index 0c06bbb3..1398db3b 100644 --- a/codeforlife/user/serializers/klass.py +++ b/codeforlife/user/serializers/klass.py @@ -1,10 +1,14 @@ -from rest_framework import serializers +""" +© Ocado Group +Created on 20/01/2024 at 11:28:29(+00:00). +""" +from ...serializers import ModelSerializer from ..models import Class # pylint: disable-next=missing-class-docstring -class ClassSerializer(serializers.ModelSerializer[Class]): +class ClassSerializer(ModelSerializer[Class]): # pylint: disable-next=missing-class-docstring,too-few-public-methods class Meta: model = Class diff --git a/codeforlife/user/serializers/school.py b/codeforlife/user/serializers/school.py index 3d96a935..52163149 100644 --- a/codeforlife/user/serializers/school.py +++ b/codeforlife/user/serializers/school.py @@ -1,10 +1,14 @@ -from rest_framework import serializers +""" +© Ocado Group +Created on 20/01/2024 at 11:28:19(+00:00). +""" +from ...serializers import ModelSerializer from ..models import School # pylint: disable-next=missing-class-docstring -class SchoolSerializer(serializers.ModelSerializer[School]): +class SchoolSerializer(ModelSerializer[School]): # pylint: disable-next=missing-class-docstring,too-few-public-methods class Meta: model = School diff --git a/codeforlife/user/serializers/student.py b/codeforlife/user/serializers/student.py index 45389e82..3814f4a5 100644 --- a/codeforlife/user/serializers/student.py +++ b/codeforlife/user/serializers/student.py @@ -1,10 +1,14 @@ -from rest_framework import serializers +""" +© Ocado Group +Created on 20/01/2024 at 11:27:56(+00:00). +""" +from ...serializers import ModelSerializer from ..models import Student # pylint: disable-next=missing-class-docstring -class StudentSerializer(serializers.ModelSerializer[Student]): +class StudentSerializer(ModelSerializer[Student]): # pylint: disable-next=missing-class-docstring,too-few-public-methods class Meta: model = Student diff --git a/codeforlife/user/serializers/teacher.py b/codeforlife/user/serializers/teacher.py index 61f0b90f..cf15e29f 100644 --- a/codeforlife/user/serializers/teacher.py +++ b/codeforlife/user/serializers/teacher.py @@ -1,10 +1,14 @@ -from rest_framework import serializers +""" +© Ocado Group +Created on 20/01/2024 at 11:27:43(+00:00). +""" +from ...serializers import ModelSerializer from ..models import Teacher # pylint: disable-next=missing-class-docstring -class TeacherSerializer(serializers.ModelSerializer[Teacher]): +class TeacherSerializer(ModelSerializer[Teacher]): # pylint: disable-next=missing-class-docstring,too-few-public-methods class Meta: model = Teacher diff --git a/codeforlife/user/serializers/user.py b/codeforlife/user/serializers/user.py index 9f8e05e3..affad4cc 100644 --- a/codeforlife/user/serializers/user.py +++ b/codeforlife/user/serializers/user.py @@ -3,15 +3,14 @@ Created on 19/01/2024 at 11:06:00(+00:00). """ -from rest_framework import serializers - +from ...serializers import ModelSerializer from ..models import User from .student import StudentSerializer from .teacher import TeacherSerializer # pylint: disable-next=missing-class-docstring -class UserSerializer(serializers.ModelSerializer[User]): +class UserSerializer(ModelSerializer[User]): student = StudentSerializer( source="new_student", read_only=True,