Skip to content

Commit eb2ec45

Browse files
committed
Address review comments
Signed-off-by: Tushar Goel <[email protected]>
1 parent c6329bc commit eb2ec45

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

vulnerabilities/api.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,17 @@ def filter_purl(self, queryset, name, value):
210210
detail={"error": f'"{purl}" is not a valid Package URL: {ve}'},
211211
)
212212

213-
ignorable_fields = ["subpath", "qualifiers"]
213+
lookups = get_purl_query_lookups(purl)
214+
return self.queryset.filter(**lookups)
214215

215-
attrs = {k: v for k, v in purl.to_dict().items() if v and k not in ignorable_fields}
216216

217-
return self.queryset.filter(**attrs)
217+
def get_purl_query_lookups(purl):
218+
lookup_fields = ["type", "namespace", "name", "version"]
219+
return {
220+
field_name: value
221+
for field_name, value in purl.to_dict().items()
222+
if value and field_name in lookup_fields
223+
}
218224

219225

220226
class PackageViewSet(viewsets.ReadOnlyModelViewSet):
@@ -239,17 +245,11 @@ def bulk_search(self, request):
239245
for purl in request.data["purls"]:
240246
try:
241247
purl_string = purl
242-
purl = PackageURL.from_string(purl).to_dict()
248+
purl = PackageURL.from_string(purl)
243249
except ValueError:
244250
return Response(status=400, data={"Error": f"Invalid Package URL: {purl}"})
245-
ignorable_fields = ["subpath", "qualifiers"]
246-
purl_data = Package.objects.filter(
247-
**{
248-
key: value
249-
for key, value in purl.items()
250-
if value and key not in ignorable_fields
251-
}
252-
)
251+
lookups = get_purl_query_lookups(purl)
252+
purl_data = Package.objects.filter(**lookups)
253253
purl_response = {}
254254
if purl_data:
255255
purl_response = PackageSerializer(purl_data[0], context={"request": request}).data

0 commit comments

Comments
 (0)