diff --git a/vulnerabilities/views.py b/vulnerabilities/views.py index 03a7428c1..8d29d140f 100644 --- a/vulnerabilities/views.py +++ b/vulnerabilities/views.py @@ -14,6 +14,7 @@ from django.contrib import messages from django.core.exceptions import ValidationError from django.core.mail import send_mail +from django.db.models import Prefetch from django.http.response import Http404 from django.shortcuts import redirect from django.shortcuts import render @@ -22,7 +23,6 @@ from django.views import generic from django.views.generic.detail import DetailView from django.views.generic.list import ListView -from django.db.models import Prefetch from vulnerabilities import models from vulnerabilities.forms import ApiUserCreationForm @@ -159,30 +159,26 @@ def get_queryset(self): "exploits", Prefetch( "affecting_packages", - queryset=models.Package.objects.only( - "type", "namespace", "name", "version" - ), + queryset=models.Package.objects.only("type", "namespace", "name", "version"), ), Prefetch( "fixed_by_packages", - queryset=models.Package.objects.only( - "type", "namespace", "name", "version" - ), + queryset=models.Package.objects.only("type", "namespace", "name", "version"), ), ) ) - def get_context_data(self, **kwargs): """ Build context with preloaded QuerySets and minimize redundant queries. """ context = super().get_context_data(**kwargs) vulnerability = self.object - + # Pre-fetch and process data in Python instead of the template weaknesses_present_in_db = [ - weakness_object for weakness_object in vulnerability.weaknesses.all() + weakness_object + for weakness_object in vulnerability.weaknesses.all() if weakness_object.weakness ] @@ -201,6 +197,7 @@ def get_context_data(self, **kwargs): ) return context + class HomePage(View): template_name = "index.html" @@ -285,19 +282,19 @@ def get_queryset(self): """ Prefetch and optimize related data to minimize database hits. """ - return super().get_queryset().prefetch_related( - Prefetch( + return ( + super() + .get_queryset() + .prefetch_related( + Prefetch( "affecting_packages", - queryset=models.Package.objects.only( - "type", "namespace", "name", "version" - ), + queryset=models.Package.objects.only("type", "namespace", "name", "version"), ), Prefetch( "fixed_by_packages", - queryset=models.Package.objects.only( - "type", "namespace", "name", "version" - ), + queryset=models.Package.objects.only("type", "namespace", "name", "version"), ), + ) ) def get_context_data(self, **kwargs): @@ -311,9 +308,11 @@ def get_context_data(self, **kwargs): sorted_affected_packages, all_affected_fixed_by_matches, ) = vulnerability.aggregate_fixed_and_affected_packages() - context.update({ - "affected_packages": sorted_affected_packages, - "fixed_by_packages": sorted_fixed_by_packages, - "all_affected_fixed_by_matches": all_affected_fixed_by_matches, - }) + context.update( + { + "affected_packages": sorted_affected_packages, + "fixed_by_packages": sorted_fixed_by_packages, + "all_affected_fixed_by_matches": all_affected_fixed_by_matches, + } + ) return context diff --git a/vulnerablecode/urls.py b/vulnerablecode/urls.py index d1c1a56bc..07f321a71 100644 --- a/vulnerablecode/urls.py +++ b/vulnerablecode/urls.py @@ -26,7 +26,8 @@ from vulnerabilities.views import HomePage from vulnerabilities.views import PackageDetails from vulnerabilities.views import PackageSearch -from vulnerabilities.views import VulnerabilityDetails, VulnerabilityPackagesDetails +from vulnerabilities.views import VulnerabilityDetails +from vulnerabilities.views import VulnerabilityPackagesDetails from vulnerabilities.views import VulnerabilitySearch from vulnerablecode.settings import DEBUG_TOOLBAR