Skip to content

Commit

Permalink
Remove unused filters
Browse files Browse the repository at this point in the history
  • Loading branch information
eriktm committed Jan 6, 2024
1 parent fc5c741 commit 8e6c81b
Showing 1 changed file with 0 additions and 54 deletions.
54 changes: 0 additions & 54 deletions unicorn/utilities/filters.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import itertools

import django_filters
from django import forms
from django.utils.encoding import force_str

#
# Filters
Expand All @@ -15,53 +11,3 @@ class NumericInFilter(django_filters.BaseInFilter, django_filters.NumberFilter):
"""

pass


class NullableCharFieldFilter(django_filters.CharFilter):
"""
Allow matching on null field values by passing a special string used to signify NULL.
"""

null_value = "NULL"

def filter(self, qs, value):
if value != self.null_value:
return super(NullableCharFieldFilter, self).filter(qs, value)
qs = self.get_method(qs)(**{"{}__isnull".format(self.name): True})
return qs.distinct() if self.distinct else qs


class NullableModelMultipleChoiceField(forms.ModelMultipleChoiceField):
"""
This field operates like a normal ModelMultipleChoiceField except that it allows for one additional choice which is
used to represent a value of Null. This is accomplished by creating a new iterator which first yields the null
choice before entering the queryset iterator, and by ignoring the null choice during cleaning. The effect is similar
to defining a MultipleChoiceField with:
choices = [(0, 'None')] + [(x.id, x) for x in Foo.objects.all()]
However, the above approach forces immediate evaluation of the queryset, which can cause issues when calculating
database migrations.
"""

iterator = forms.models.ModelChoiceIterator

def __init__(self, null_value=0, null_label="-- None --", *args, **kwargs):
self.null_value = null_value
self.null_label = null_label
super(NullableModelMultipleChoiceField, self).__init__(*args, **kwargs)

def _get_choices(self):
if hasattr(self, "_choices"):
return self._choices
# Prepend the null choice to the queryset iterator
return itertools.chain([(self.null_value, self.null_label)], self.iterator(self))

choices = property(_get_choices, forms.ChoiceField._set_choices)

def clean(self, value):
# Strip all instances of the null value before cleaning
if value is not None:
stripped_value = [x for x in value if x != force_str(self.null_value)]
else:
stripped_value = value
super(NullableModelMultipleChoiceField, self).clean(stripped_value)
return value

0 comments on commit 8e6c81b

Please sign in to comment.