Skip to content

Commit

Permalink
Add advanced search to the PackageFilterSet #871
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Druez <[email protected]>
  • Loading branch information
tdruez committed Aug 23, 2023
1 parent a005646 commit 9883bf1
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions scanpipe/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,14 +567,17 @@ def filter(self, qs, value):
return qs


class DiscoveredPackageSearchFilter(django_filters.CharFilter):
class DiscoveredPackageSearchFilter(QuerySearchFilter):
def filter(self, qs, value):
if not value:
return qs

if value.startswith("pkg:"):
return qs.for_package_url(value)

if ":" in value:
return super().filter(qs, value)

search_fields = ["type", "namespace", "name", "version"]
lookups = Q()
for field_names in search_fields:
Expand All @@ -589,7 +592,9 @@ class PackageFilterSet(FilterSetUtilsMixin, django_filters.FilterSet):
"compliance_alert",
]

search = DiscoveredPackageSearchFilter(label="Search", lookup_expr="icontains")
search = DiscoveredPackageSearchFilter(
label="Search", field_name="name", lookup_expr="icontains"
)
sort = django_filters.OrderingFilter(
label="Sort",
fields=[
Expand Down

0 comments on commit 9883bf1

Please sign in to comment.