Skip to content

Commit

Permalink
Handle empty list for pulp_href__in filter
Browse files Browse the repository at this point in the history
Fix bug where empty list for the pulp_href__in filter returns all
results instead of no results.

 The problem is that when using FilterMethod, django-filter just simply
ignores None and empty set and returns the full list of results:

https://github.com/carltongibson/django-filter/blob/e4a70a667a0bf3882fd44b557bc76583d2c65cd1/django_filters/filters.py#L804-L805

There's no way to update the method to prevent this since this logic
happens before the method is called. Instead, this change moves the
method to the filter and adds a None check.

fixes pulp#4437
  • Loading branch information
daviddavis committed Sep 18, 2023
1 parent fda9645 commit 58d6f09
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions pulpcore/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,12 @@ class IdInFilter(BaseInFilter, filters.UUIDFilter):


class HREFInFilter(BaseInFilter, filters.CharFilter):
pass
def filter(self, qs, value):
if value is None:
return qs

pks = [extract_pk(href) for href in value]
return qs.filter(pk__in=pks)


class PulpTypeFilter(filters.ChoiceFilter):
Expand Down Expand Up @@ -271,7 +276,7 @@ class BaseFilterSet(filterset.FilterSet):

help_text = {}
pulp_id__in = IdInFilter(field_name="pk", lookup_expr="in")
pulp_href__in = HREFInFilter(field_name="pk", method="filter_pulp_href")
pulp_href__in = HREFInFilter(field_name="pk")
q = ExpressionFilter()

FILTER_DEFAULTS = {
Expand Down Expand Up @@ -307,11 +312,6 @@ class BaseFilterSet(filterset.FilterSet):
"ne": _("not equal to"),
}

def filter_pulp_href(self, queryset, name, value):
# Convert each href to a pk
pks = [extract_pk(href) for href in value]
return queryset.filter(pk__in=pks)

@classmethod
def get_filters(cls):
filters = super().get_filters()
Expand Down

0 comments on commit 58d6f09

Please sign in to comment.