From 4dcbd9fbe1e27524bdc8abc21b6abeb244511b53 Mon Sep 17 00:00:00 2001 From: SKairinos Date: Wed, 24 Jul 2024 12:41:55 +0000 Subject: [PATCH] fix: add class filterset --- codeforlife/user/filters/__init__.py | 1 + codeforlife/user/filters/klass.py | 17 +++++++++++++++++ codeforlife/user/views/klass.py | 2 ++ codeforlife/user/views/klass_test.py | 12 ++++++++++++ 4 files changed, 32 insertions(+) create mode 100644 codeforlife/user/filters/klass.py diff --git a/codeforlife/user/filters/__init__.py b/codeforlife/user/filters/__init__.py index f2ca2351..5528718b 100644 --- a/codeforlife/user/filters/__init__.py +++ b/codeforlife/user/filters/__init__.py @@ -3,4 +3,5 @@ Created on 12/04/2024 at 14:52:22(+01:00). """ +from .klass import ClassFilterSet from .user import UserFilterSet diff --git a/codeforlife/user/filters/klass.py b/codeforlife/user/filters/klass.py new file mode 100644 index 00000000..d2a85fd6 --- /dev/null +++ b/codeforlife/user/filters/klass.py @@ -0,0 +1,17 @@ +""" +© Ocado Group +Created on 24/07/2024 at 13:19:57(+01:00). +""" + +from django_filters import ( # type: ignore[import-untyped] # isort: skip + rest_framework as filters, +) + +from ..models import Class + + +# pylint: disable-next=missing-class-docstring +class ClassFilterSet(filters.FilterSet): + class Meta: + model = Class + fields = ["teacher"] diff --git a/codeforlife/user/views/klass.py b/codeforlife/user/views/klass.py index 8255c037..ddfdd71a 100644 --- a/codeforlife/user/views/klass.py +++ b/codeforlife/user/views/klass.py @@ -5,6 +5,7 @@ from ...permissions import OR from ...views import ModelViewSet +from ..filters import ClassFilterSet from ..models import Class from ..models import User as RequestUser from ..permissions import IsStudent, IsTeacher @@ -16,6 +17,7 @@ class ClassViewSet(ModelViewSet[RequestUser, Class]): http_method_names = ["get"] lookup_field = "access_code" serializer_class = ClassSerializer + filterset_class = ClassFilterSet def get_permissions(self): # Only school-teachers can list classes. diff --git a/codeforlife/user/views/klass_test.py b/codeforlife/user/views/klass_test.py index 7ba22f18..33230971 100644 --- a/codeforlife/user/views/klass_test.py +++ b/codeforlife/user/views/klass_test.py @@ -106,3 +106,15 @@ def test_list(self): self.client.login_as(user) self.client.list(models=user.teacher.classes.all()) + + def test_list__teacher(self): + """Can successfully list classes assigned to a teacher.""" + user = self.admin_school_teacher_user + classes = Class.objects.filter(teacher=user.teacher) + assert classes.exists() + + self.client.login_as(user) + self.client.list( + models=classes, + filters={"teacher": user.teacher.id}, + )