Skip to content

Commit

Permalink
Refactor Views
Browse files Browse the repository at this point in the history
Signed-off-by: Tushar Goel <[email protected]>
  • Loading branch information
TG1999 committed Jan 16, 2025
1 parent 3bdefc6 commit 68926b2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
45 changes: 22 additions & 23 deletions vulnerabilities/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
]

Expand All @@ -201,6 +197,7 @@ def get_context_data(self, **kwargs):
)
return context


class HomePage(View):
template_name = "index.html"

Expand Down Expand Up @@ -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):
Expand All @@ -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
3 changes: 2 additions & 1 deletion vulnerablecode/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 68926b2

Please sign in to comment.