diff --git a/setup.cfg b/setup.cfg
index 47fbaebc2..3d91eb983 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -104,6 +104,8 @@ dev =
ipython==8.0.1
# used for testing
commoncode
+ # debug
+ django-debug-toolbar
[options.entry_points]
console_scripts =
diff --git a/vulnerabilities/api.py b/vulnerabilities/api.py
index 26f6e08ec..12da1ed6f 100644
--- a/vulnerabilities/api.py
+++ b/vulnerabilities/api.py
@@ -256,7 +256,10 @@ def bulk_search(self, request):
@action(detail=False, methods=["get"])
def all(self, request):
- vulnerable_packages = Package.objects.vulnerable().only(*PackageURL._fields)
+ """
+ Return all the vulnerable Package URLs.
+ """
+ vulnerable_packages = Package.objects.vulnerable().only(*PackageURL._fields).distinct()
vulnerable_purls = [str(package.purl) for package in vulnerable_packages]
return Response(vulnerable_purls)
diff --git a/vulnerabilities/migrations/0028_alter_packagerelatedvulnerability_fix.py b/vulnerabilities/migrations/0028_alter_packagerelatedvulnerability_fix.py
new file mode 100644
index 000000000..e3f7d3073
--- /dev/null
+++ b/vulnerabilities/migrations/0028_alter_packagerelatedvulnerability_fix.py
@@ -0,0 +1,18 @@
+# Generated by Django 4.0.7 on 2022-10-19 16:18
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('vulnerabilities', '0027_alter_vulnerabilityreference_url'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='packagerelatedvulnerability',
+ name='fix',
+ field=models.BooleanField(db_index=True, default=False, help_text='Does this relation fix the specified vulnerability ?'),
+ ),
+ ]
diff --git a/vulnerabilities/models.py b/vulnerabilities/models.py
index 3bfcb0ca3..00c1b9084 100644
--- a/vulnerabilities/models.py
+++ b/vulnerabilities/models.py
@@ -17,6 +17,8 @@
from django.core.validators import MaxValueValidator
from django.core.validators import MinValueValidator
from django.db import models
+from django.db.models import Count
+from django.db.models import Q
from django.db.models.functions import Length
from django.db.models.functions import Trim
from django.dispatch import receiver
@@ -86,8 +88,7 @@ def __str__(self):
@property
def severities(self):
- for reference in self.references.all():
- yield from VulnerabilitySeverity.objects.filter(reference=reference.id)
+ return VulnerabilitySeverity.objects.filter(reference__in=self.references.all())
@property
def vulnerable_to(self):
@@ -202,7 +203,19 @@ def vulnerable(self):
"""
Return all vulnerable packages.
"""
- return Package.objects.filter(packagerelatedvulnerability__fix=False).distinct()
+ return self.filter(packagerelatedvulnerability__fix=False)
+
+ def with_vulnerability_counts(self):
+ return self.annotate(
+ vulnerability_count=Count(
+ "vulnerabilities",
+ filter=Q(packagerelatedvulnerability__fix=False),
+ ),
+ patched_vulnerability_count=Count(
+ "vulnerabilities",
+ filter=Q(packagerelatedvulnerability__fix=True),
+ ),
+ )
class Package(PackageURLMixin):
@@ -310,7 +323,9 @@ class PackageRelatedVulnerability(models.Model):
)
fix = models.BooleanField(
- default=False, help_text="Does this relation fix the specified vulnerability ?"
+ default=False,
+ db_index=True,
+ help_text="Does this relation fix the specified vulnerability ?",
)
class Meta:
diff --git a/vulnerabilities/templates/vulnerability_details.html b/vulnerabilities/templates/vulnerability_details.html
index d0bf2defa..ae172c2e2 100644
--- a/vulnerabilities/templates/vulnerability_details.html
+++ b/vulnerabilities/templates/vulnerability_details.html
@@ -37,21 +37,21 @@
- Fixed by packages ({{ vulnerability.resolved_to|length }})
+ Fixed by packages ({{ resolved_to|length }})
- Affected packages ({{ vulnerability.vulnerable_to|length }})
+ Affected packages ({{ vulnerable_to|length }})
- References ({{ vulnerability.references.all|length }})
+ References ({{ references|length }})
@@ -69,7 +69,7 @@
Aliases |
- {% for alias in vulnerability.aliases.all %}
+ {% for alias in aliases %}
{% if alias.url %}
{{ alias }}
{% else %}
@@ -121,11 +121,11 @@
- Fixed by packages ({{ vulnerability.resolved_to.all|length }})
+ Fixed by packages ({{ resolved_to|length }})
|