Skip to content

Commit 7f17b45

Browse files
committed
Fix API
Signed-off-by: Tushar Goel <[email protected]>
1 parent 1440d69 commit 7f17b45

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

vulnerabilities/api.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,11 @@ def get_fixed_packages_qs(self):
592592
Filter the packages that fixes a vulnerability
593593
on fields like name, namespace and type.
594594
"""
595-
return self.get_packages_qs().filter(fixingpackagerelatedvulnerability__isnull=False).with_is_vulnerable()
595+
return (
596+
self.get_packages_qs()
597+
.filter(fixingpackagerelatedvulnerability__isnull=False)
598+
.with_is_vulnerable()
599+
)
596600

597601
def get_packages_qs(self):
598602
"""
@@ -620,11 +624,11 @@ def get_queryset(self):
620624
# queryset=self.get_packages_qs(),
621625
# ),
622626
"weaknesses",
623-
# Prefetch(
624-
# "fixed_by_packages",
625-
# queryset=self.get_fixed_packages_qs(),
626-
# to_attr="filtered_fixed_packages",
627-
# ),
627+
Prefetch(
628+
"fixed_by_packages",
629+
queryset=self.get_fixed_packages_qs(),
630+
to_attr="filtered_fixed_packages",
631+
),
628632
)
629633
)
630634

vulnerabilities/models.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,8 @@ def search(self, query: str = None):
151151

152152
def with_package_counts(self):
153153
return self.annotate(
154-
vulnerable_package_count=Count(
155-
"affecting_packages", distinct=True
156-
),
157-
patched_package_count=Count(
158-
"fixed_by_packages", distinct=True
159-
),
154+
vulnerable_package_count=Count("affecting_packages", distinct=True),
155+
patched_package_count=Count("fixed_by_packages", distinct=True),
160156
)
161157

162158

@@ -237,7 +233,7 @@ def affected_packages(self):
237233
"""
238234
Return a queryset of packages that are affected by this vulnerability.
239235
"""
240-
return self.affecting_packages
236+
return self.affecting_packages.with_is_vulnerable()
241237

242238
@property
243239
def packages_fixing(self):
@@ -984,6 +980,7 @@ def add_package_vulnerability_changelog(self, advisory):
984980
related_vulnerability=str(self.vulnerability),
985981
)
986982

983+
987984
class PackageRelatedVulnerabilityBase(models.Model):
988985
"""
989986
Abstract base class for package-vulnerability relations.
@@ -1077,10 +1074,12 @@ def add_package_vulnerability_changelog(self, advisory):
10771074
related_vulnerability=str(self.vulnerability),
10781075
)
10791076

1077+
10801078
class FixingPackageRelatedVulnerability(PackageRelatedVulnerabilityBase):
10811079
class Meta(PackageRelatedVulnerabilityBase.Meta):
10821080
verbose_name_plural = "Fixing Package Related Vulnerabilities"
10831081

1082+
10841083
class AffectedByPackageRelatedVulnerability(PackageRelatedVulnerabilityBase):
10851084
class Meta(PackageRelatedVulnerabilityBase.Meta):
10861085
verbose_name_plural = "Affected By Package Related Vulnerabilities"

vulnerabilities/pipes/advisory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
from vulnerabilities.importer import AdvisoryData
1919
from vulnerabilities.improver import MAX_CONFIDENCE
2020
from vulnerabilities.models import Advisory
21+
from vulnerabilities.models import AffectedByPackageRelatedVulnerability
22+
from vulnerabilities.models import FixingPackageRelatedVulnerability
2123
from vulnerabilities.models import Package
2224
from vulnerabilities.models import VulnerabilityReference
2325
from vulnerabilities.models import VulnerabilityRelatedReference
2426
from vulnerabilities.models import VulnerabilitySeverity
2527
from vulnerabilities.models import Weakness
26-
from vulnerabilities.models import AffectedByPackageRelatedVulnerability
27-
from vulnerabilities.models import FixingPackageRelatedVulnerability
2828

2929

3030
def insert_advisory(advisory: AdvisoryData, pipeline_id: str, logger: Callable = None):

0 commit comments

Comments
 (0)