ok_hand: Use PackageFilterSet instead of filtering in PackageViewSet
from urllib.parse import unquote
from django.urls import reverse
+from django_filters import rest_framework as filters
from packageurl import PackageURL
from rest_framework import serializers
from rest_framework import viewsets
@@ -133,21 +134,18 @@ class PackageSerializer(serializers.ModelSerializer):
]
-class PackageViewSet(viewsets.ReadOnlyModelViewSet):
- queryset = Package.objects.all()
- serializer_class = PackageSerializer
- filterset_fields = ("name", "version", "namespace", "type", "subpath")
+class PackageFilterSet(filters.FilterSet):
+ purl = filters.CharFilter(method="filter_purl")
- def filter_queryset(self, qs):
- purl = self.request.query_params.get("purl")
- if purl:
- purl = unquote(purl)
-
- else:
- return super().filter_queryset(qs)
+ class Meta:
+ model = Package
+ fields = ["name", "type", "version", "subpath", "purl"]
+ def filter_purl(self, queryset, name, value):
+ purl = unquote(value)
try:
purl = PackageURL.from_string(purl)
+
except ValueError as ve:
raise serializers.ValidationError(
detail={"error": f'"{purl}" is not a valid Package URL: {ve}'},
@@ -157,6 +155,13 @@ class PackageViewSet(viewsets.ReadOnlyModelViewSet):
return self.queryset.filter(**attrs)
+class PackageViewSet(viewsets.ReadOnlyModelViewSet):
+ queryset = Package.objects.all()
+ serializer_class = PackageSerializer
+ filter_backends = (filters.DjangoFilterBackend,)
+ filterset_class = PackageFilterSet
+
+
class VulnerabilityViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = VulnerabilitySerializer
paginate_by = 50
