Skip to content

Commit

Permalink
Changed default null label for bool filters in the API.
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptypjeu committed Aug 11, 2023
1 parent fb37967 commit f944d07
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
21 changes: 16 additions & 5 deletions teknologr/api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
from functools import reduce
from operator import and_

class BooleanFilter(django_filters.BooleanFilter):
'''
The defualt null label is for some reason 'unknown', but I want it to be ''
'''
def __init__(self, *args, **kwargs):
kwargs['widget'] = django_filters.widgets.BooleanWidget
super().__init__(*args, **kwargs)

class BaseFilter(django_filters.rest_framework.FilterSet):
'''
Base filter class that takes care of normal users from using staff-only filters.
Expand Down Expand Up @@ -95,7 +103,7 @@ class MemberFilter(BaseFilter):
enrolment_year = django_filters.RangeFilter(
label='Inskrivingsåret är mellan',
)
graduated = django_filters.BooleanFilter(
graduated = BooleanFilter(
method='filter_graduated',
label='Utexaminerad?',
)
Expand All @@ -111,16 +119,16 @@ class MemberFilter(BaseFilter):
student_id = django_filters.CharFilter(
label='Studienummer',
)
dead = django_filters.BooleanFilter(
dead = BooleanFilter(
label='Avliden?',
)
subscribed_to_modulen = django_filters.BooleanFilter(
subscribed_to_modulen = BooleanFilter(
label='Prenumererar på Modulen?',
)
allow_studentbladet = django_filters.BooleanFilter(
allow_studentbladet = BooleanFilter(
label='Prenumererar på Studentbladet?',
)
allow_publish_info = django_filters.BooleanFilter(
allow_publish_info = BooleanFilter(
label='Tillåter publicering av kontaktinformation?',
)
comment = django_filters.CharFilter(
Expand All @@ -137,6 +145,7 @@ class MemberFilter(BaseFilter):
def includes_hidable_field(self):
''' Check if the current query includes filtering on any hidable Member fields '''
for name, value in self.data.items():
# XXX: This wrongly identifies invalid filter values as being set. For example, providing an invalid string to a BooleanFilter field will be caught here, even though the field will not be filtered... Maybe this is good enough?
if name in self.HIDABLE and any(value):
return True
return False
Expand Down Expand Up @@ -181,6 +190,8 @@ def filter_graduated(self, queryset, name, value):
'''
Modifying the graduated flag whan adding a graduated year can sometimes be forgotten, so taking that into consideration for this filter query.
'''
assert value in [True, False, None]

if value:
# Graduated or graduated year is set
return queryset.filter(Q(graduated=True) | Q(graduated_year__isnull=False))
Expand Down
20 changes: 20 additions & 0 deletions teknologr/api/tests_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,26 @@ def setUp(self):
allow_publish_info=True,
)

class MemberFilterGraduatedNullTest(MemberFilterGraduatedTrueTest):
def setUp(self):
super().setUp()

# graduated is the only BooleanFilter on a hidable field, so trying out the null label for it
# Dummy member included
self.query = 'graduated='
self.n_normal = len(Member.objects.all())
self.n_staff = self.n_normal

class MemberFilterGraduatedInvalidTest(MemberFilterGraduatedTrueTest):
def setUp(self):
super().setUp()

# graduated is the only BooleanFilter on a hidable field, so trying out an invalid label for it
# Dummy member included
# XXX: This should probably return all Members, but now the filter is being recognized as "set", so all hidden Members will be filtered out for normal users
self.query = 'graduated=unknown'
self.n_normal = 4
self.n_staff = 5

class MemberFilterGraduatedYearTest(BaseAPITest, TestCases):
def setUp(self):
Expand Down

0 comments on commit f944d07

Please sign in to comment.