Commit a903b091 authored by Bryan  BRANCOTTE's avatar Bryan BRANCOTTE
Browse files

preventing ValueError when passing wrong value for primary key

parent 363f89f7
from functools import reduce
from typing import List
from django.db.models import Avg, Q, Count, Func, When, IntegerField, Case, Value, Max, Min
from django.http import JsonResponse
......@@ -30,20 +31,28 @@ class MyAPIView(drf_views.APIView):
raise NotImplementedError()
def to_int_array(input_list: List):
for a in input_list:
try:
yield int(a)
except ValueError:
pass
class FromGetParamsFilterBackend(BaseFilterBackend):
def filter_queryset(self, request, queryset, view):
nop = 1 if "allow_overflow" in request.GET else 0
if "ds" in request.GET:
getlist = reduce(lambda x, y: x + y, [l.split(',') for l in request.GET.getlist("ds")])
queryset = queryset.filter(data_source__pk__in=getlist)
queryset = queryset.filter(data_source__pk__in=to_int_array(getlist))
nop += 1
if "host" in request.GET:
getlist = reduce(lambda x, y: x + y, [l.split(',') for l in request.GET.getlist("host")])
queryset = queryset.filter(host__pk__in=getlist)
queryset = queryset.filter(host__pk__in=to_int_array(getlist))
nop += 1
if "virus" in request.GET:
getlist = reduce(lambda x, y: x + y, [l.split(',') for l in request.GET.getlist("virus")])
queryset = queryset.filter(virus__pk__in=getlist)
queryset = queryset.filter(virus__pk__in=to_int_array(getlist))
nop += 1
if nop == 0:
return queryset.none()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment