Skip to content

Commit

Permalink
Sort the vulnerability by risk in listing #98
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Nov 12, 2024
1 parent 7d5b4a6 commit 4f37c7b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion component_catalog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class TabVulnerabilityMixin:
template = "component_catalog/tabs/tab_vulnerabilities.html"

def tab_vulnerabilities(self):
vulnerabilities_qs = self.object.affected_by_vulnerabilities.all()
vulnerabilities_qs = self.object.affected_by_vulnerabilities.order_by_risk()
if not vulnerabilities_qs:
return

Expand Down
6 changes: 1 addition & 5 deletions product_portfolio/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from django.core.paginator import Paginator
from django.db import transaction
from django.db.models import Count
from django.db.models import F
from django.db.models import Prefetch
from django.db.models.functions import Lower
from django.forms import modelformset_factory
Expand Down Expand Up @@ -1111,10 +1110,7 @@ def get_context_data(self, **kwargs):
package_qs = Package.objects.filter(product=product).only_rendering_fields()
vulnerability_qs = base_vulnerability_qs.prefetch_related(
Prefetch("affected_packages", package_qs)
).order_by(
F("max_score").desc(nulls_last=True),
"-min_score",
)
).order_by_risk()

self.filterset = self.filterset_class(
self.request.GET,
Expand Down
7 changes: 7 additions & 0 deletions vulnerabilities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ def with_affected_packages_count(self):
affected_packages_count=Count("affected_packages", distinct=True),
)

def order_by_risk(self):
return self.order_by(
models.F("risk_score").desc(nulls_last=True),
models.F("weighted_severity").desc(nulls_last=True),
models.F("exploitability").desc(nulls_last=True),
)


class Vulnerability(HistoryDateFieldsMixin, DataspacedModel):
"""
Expand Down
11 changes: 4 additions & 7 deletions vulnerabilities/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#

from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import F
from django.http import Http404
from django.utils.translation import gettext_lazy as _

Expand Down Expand Up @@ -47,18 +46,16 @@ def get_queryset(self):
"aliases",
"summary",
"fixed_packages_count",
"max_score",
"min_score",
"exploitability",
"weighted_severity",
"risk_score",
"created_date",
"last_modified_date",
"dataspace",
)
.with_affected_products_count()
.with_affected_packages_count()
.order_by(
F("max_score").desc(nulls_last=True),
"-min_score",
)
.order_by_risk()
)

def get_context_data(self, **kwargs):
Expand Down

0 comments on commit 4f37c7b

Please sign in to comment.