Skip to content

Commit

Permalink
refactor(filters): config ArrayField DRY
Browse files Browse the repository at this point in the history
Move filter_overrides for ArrayField to Meta
class of common parent for model-based FilterSets
to reduce redundancy.
  • Loading branch information
koeaw committed Aug 19, 2024
1 parent 41adb9c commit aed71d8
Showing 1 changed file with 15 additions and 33 deletions.
48 changes: 15 additions & 33 deletions apis_ontology/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,21 @@ class BaseEntityFilterSet(AbstractEntityFilterSet):
"""
Parent class for all entity model classes.
"""

class Meta(AbstractEntityFilterSet.Meta):
filter_overrides = {
**AbstractEntityFilterSet.Meta.filter_overrides,
ArrayField: {
"filter_class": django_filters.MultipleChoiceFilter,
# "extra" attribute not working for fields with choices, see:
# https://github.com/carltongibson/django-filter/issues/1475
"extra": lambda f: {
"lookup_expr": "icontains",
"widget": forms.SelectMultiple,
},
},
}

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if "custom_search" in self.filters:
Expand Down Expand Up @@ -182,17 +197,6 @@ class Meta(BaseEntityFilterSet.Meta):
fields = {
"new_fictionality": ["icontains"],
}
filter_overrides = {
ArrayField: {
"filter_class": django_filters.MultipleChoiceFilter,
# "extra" attribute not working for fields with choices, see:
# https://github.com/carltongibson/django-filter/issues/1475
"extra": lambda f: {
"lookup_expr": "icontains",
"widget": forms.SelectMultiple,
},
},
}


class VersionCharacterFilterSet(CharacterFilterSet):
Expand Down Expand Up @@ -261,17 +265,6 @@ class Meta(BaseEntityFilterSet.Meta):
fields = {
"temporal_order": ["icontains"],
}
filter_overrides = {
ArrayField: {
"filter_class": django_filters.MultipleChoiceFilter,
# "extra" attribute not working for fields with choices, see:
# https://github.com/carltongibson/django-filter/issues/1475
"extra": lambda f: {
"lookup_expr": "icontains",
"widget": forms.SelectMultiple,
},
},
}


class VersionWorkFilterSet(WorkFilterSet):
Expand All @@ -293,17 +286,6 @@ class Meta(BaseEntityFilterSet.Meta):
fields = {
"language": ["icontains"],
}
filter_overrides = {
ArrayField: {
"filter_class": django_filters.MultipleChoiceFilter,
# "extra" attribute not working for fields with choices, see:
# https://github.com/carltongibson/django-filter/issues/1475
"extra": lambda f: {
"lookup_expr": "icontains",
"widget": forms.SelectMultiple,
},
},
}


class VersionExpressionFilterSet(ExpressionFilterSet):
Expand Down

0 comments on commit aed71d8

Please sign in to comment.