Skip to content

Commit

Permalink
only teachers method
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Aug 2, 2024
1 parent 131406c commit f5a4cdd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
16 changes: 13 additions & 3 deletions codeforlife/user/filters/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand All @@ -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"]
6 changes: 3 additions & 3 deletions codeforlife/user/views/user_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,16 @@ 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()

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):
Expand Down

0 comments on commit f5a4cdd

Please sign in to comment.