diff --git a/codeforlife/user/filters/user.py b/codeforlife/user/filters/user.py index 6e18efc8..d52dbe4d 100644 --- a/codeforlife/user/filters/user.py +++ b/codeforlife/user/filters/user.py @@ -22,13 +22,13 @@ class UserFilterSet(FilterSet): "exact", ) - teachers_in_school = filters.NumberFilter("new_teacher__school") - _id = filters.NumberFilter(method="_id_method") _id_method = FilterSet.make_exclude_field_list_method("id") name = filters.CharFilter(method="name_method") + only_teachers = filters.BooleanFilter(method="only_teachers__method") + def name_method( self: FilterSet, queryset: QuerySet[User], name: str, *args ): @@ -44,6 +44,16 @@ def name_method( first_name__icontains=first_name ) | queryset.filter(last_name__icontains=last_name) + def only_teachers__method( + self: FilterSet, queryset: QuerySet[User], _: str, value: bool + ): + """Get only teacher-users.""" + return ( + queryset.filter(new_teacher__isnull=False, new_student__isnull=True) + if value + else queryset + ) + class Meta: model = User - fields = ["students_in_class", "teachers_in_school", "_id", "name"] + fields = ["students_in_class", "only_teachers", "_id", "name"] diff --git a/codeforlife/user/views/user_test.py b/codeforlife/user/views/user_test.py index aad3267a..6f6b42b9 100644 --- a/codeforlife/user/views/user_test.py +++ b/codeforlife/user/views/user_test.py @@ -160,8 +160,8 @@ def test_list__students_in_class(self): filters={"students_in_class": klass.access_code}, ) - def test_list__teachers_in_school(self): - """Can successfully list teacher-users in a school.""" + def test_list__only_teachers(self): + """Can successfully list only teacher-users.""" user = self.admin_school_teacher_user school_teacher_users = user.teacher.school_teacher_users.all() assert school_teacher_users.exists() @@ -169,7 +169,7 @@ def test_list__teachers_in_school(self): self.client.login_as(user) self.client.list( models=school_teacher_users, - filters={"teachers_in_school": str(user.teacher.school.id)}, + filters={"only_teachers": str(True)}, ) def test_list___id(self):