|
8 | 8 |
|
9 | 9 | from django import forms |
10 | 10 | from django.contrib.admin.options import IncorrectLookupParameters |
11 | | -from django.db.models import F |
12 | 11 | from django.utils.functional import cached_property |
13 | 12 | from django.utils.translation import gettext_lazy as _ |
14 | 13 |
|
|
17 | 16 | from component_catalog.models import Component |
18 | 17 | from component_catalog.models import ComponentKeyword |
19 | 18 | from component_catalog.models import Package |
20 | | -from component_catalog.models import Vulnerability |
21 | 19 | from component_catalog.programming_languages import PROGRAMMING_LANGUAGES |
22 | 20 | from dje.filters import DataspacedFilterSet |
23 | 21 | from dje.filters import DefaultOrderingFilter |
24 | 22 | from dje.filters import HasRelationFilter |
25 | 23 | from dje.filters import MatchOrderedSearchFilter |
26 | 24 | from dje.filters import RelatedLookupListFilter |
27 | | -from dje.filters import SearchFilter |
28 | 25 | from dje.widgets import BootstrapSelectMultipleWidget |
29 | 26 | from dje.widgets import DropDownRightWidget |
30 | 27 | from dje.widgets import SortDropDownWidget |
@@ -283,82 +280,3 @@ def show_created_date(self): |
283 | 280 | @cached_property |
284 | 281 | def show_last_modified_date(self): |
285 | 282 | return not self.sort_value or self.has_sort_by("last_modified_date") |
286 | | - |
287 | | - |
288 | | -class NullsLastOrderingFilter(django_filters.OrderingFilter): |
289 | | - """ |
290 | | - A custom ordering filter that ensures null values are sorted last. |
291 | | -
|
292 | | - When sorting by fields with potential null values, this filter modifies the |
293 | | - ordering to use Django's `nulls_last` clause for better handling of null values, |
294 | | - whether in ascending or descending order. |
295 | | - """ |
296 | | - |
297 | | - def filter(self, qs, value): |
298 | | - if not value: |
299 | | - return qs |
300 | | - |
301 | | - ordering = [] |
302 | | - for field in value: |
303 | | - if field.startswith("-"): |
304 | | - field_name = field[1:] |
305 | | - ordering.append(F(field_name).desc(nulls_last=True)) |
306 | | - else: |
307 | | - ordering.append(F(field).asc(nulls_last=True)) |
308 | | - |
309 | | - return qs.order_by(*ordering) |
310 | | - |
311 | | - |
312 | | -vulnerability_score_ranges = { |
313 | | - "low": (0.1, 3), |
314 | | - "medium": (4.0, 6.9), |
315 | | - "high": (7.0, 8.9), |
316 | | - "critical": (9.0, 10.0), |
317 | | -} |
318 | | - |
319 | | -SCORE_CHOICES = [ |
320 | | - (key, f"{key.capitalize()} ({value[0]} - {value[1]})") |
321 | | - for key, value in vulnerability_score_ranges.items() |
322 | | -] |
323 | | - |
324 | | - |
325 | | -class VulnerabilityFilterSet(DataspacedFilterSet): |
326 | | - q = SearchFilter( |
327 | | - label=_("Search"), |
328 | | - search_fields=["vulnerability_id", "aliases"], |
329 | | - ) |
330 | | - sort = NullsLastOrderingFilter( |
331 | | - label=_("Sort"), |
332 | | - fields=[ |
333 | | - "max_score", |
334 | | - "min_score", |
335 | | - "affected_products_count", |
336 | | - "affected_packages_count", |
337 | | - "fixed_packages_count", |
338 | | - "created_date", |
339 | | - "last_modified_date", |
340 | | - ], |
341 | | - widget=SortDropDownWidget, |
342 | | - ) |
343 | | - max_score = django_filters.ChoiceFilter( |
344 | | - choices=SCORE_CHOICES, |
345 | | - method="filter_by_score_range", |
346 | | - label="Score Range", |
347 | | - help_text="Select a score range to filter.", |
348 | | - ) |
349 | | - |
350 | | - class Meta: |
351 | | - model = Vulnerability |
352 | | - fields = [ |
353 | | - "q", |
354 | | - ] |
355 | | - |
356 | | - def __init__(self, *args, **kwargs): |
357 | | - super().__init__(*args, **kwargs) |
358 | | - self.filters["max_score"].extra["widget"] = DropDownRightWidget(anchor=self.anchor) |
359 | | - |
360 | | - def filter_by_score_range(self, queryset, name, value): |
361 | | - if value in vulnerability_score_ranges: |
362 | | - low, high = vulnerability_score_ranges[value] |
363 | | - return queryset.filter(max_score__gte=low, max_score__lte=high) |
364 | | - return queryset |
0 commit comments