Skip to content

Commit

Permalink
Add full purl search for the Dependencies tab UI #138
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Jul 25, 2024
1 parent eb07b7f commit 6d3f4ce
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion product_portfolio/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

from django import forms
from django.contrib import admin
from django.db.models import Q
from django.utils.translation import gettext_lazy as _

import django_filters
from packageurl.contrib.django.utils import purl_to_lookups

from component_catalog.models import ComponentKeyword
from component_catalog.programming_languages import PROGRAMMING_LANGUAGES
Expand Down Expand Up @@ -317,8 +319,27 @@ class Meta:
]


class PackageURLSearchFilter(SearchFilter):
def filter(self, qs, value):
if value and value.startswith("pkg:"):
base_lookups = purl_to_lookups(value)
packages_fk_fields = ["for_package", "resolved_to_package"]

combined_q = Q()
for package_fk in packages_fk_fields:
fk_lookups = {
f"{package_fk}__{field}": value for field, value in base_lookups.items()
}
combined_q |= Q(**fk_lookups)

if combined_q:
return qs.filter(combined_q)

return super().filter(qs, value)


class DependencyFilterSet(DataspacedFilterSet):
q = SearchFilter(
q = PackageURLSearchFilter(
label=_("Search"),
search_fields=[
"dependency_uid",
Expand Down

0 comments on commit 6d3f4ce

Please sign in to comment.