Skip to content

Commit

Permalink
fix: add class filterset
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jul 24, 2024
1 parent 8170ba6 commit 4dcbd9f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions codeforlife/user/filters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
Created on 12/04/2024 at 14:52:22(+01:00).
"""

from .klass import ClassFilterSet
from .user import UserFilterSet
17 changes: 17 additions & 0 deletions codeforlife/user/filters/klass.py
Original file line number Diff line number Diff line change
@@ -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"]
2 changes: 2 additions & 0 deletions codeforlife/user/views/klass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions codeforlife/user/views/klass_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
)

0 comments on commit 4dcbd9f

Please sign in to comment.