Skip to content

Commit 37a1718

Browse files
committed
feat: add field to track the latest advisory for an avid
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
1 parent 2a08e55 commit 37a1718

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

vulnerabilities/models.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2877,11 +2877,7 @@ def latest_for_avid(self, avid: str):
28772877
)
28782878

28792879
def latest_per_avid(self):
2880-
return self.order_by(
2881-
"avid",
2882-
F("date_collected").desc(nulls_last=True),
2883-
"-id",
2884-
).distinct("avid")
2880+
return self.filter(is_latest=True)
28852881

28862882
def latest_for_avids(self, avids):
28872883
return self.filter(avid__in=avids).latest_per_avid()
@@ -2998,6 +2994,7 @@ class AdvisoryV2(models.Model):
29982994
max_length=200,
29992995
blank=False,
30002996
null=False,
2997+
db_index=True,
30012998
help_text="Unique ID for the datasource used for this advisory ." "e.g.: nginx_importer_v2",
30022999
)
30033000

@@ -3081,6 +3078,14 @@ class AdvisoryV2(models.Model):
30813078
help_text="UTC Date on which the advisory was collected",
30823079
)
30833080

3081+
is_latest = models.BooleanField(
3082+
default=False,
3083+
blank=False,
3084+
null=False,
3085+
db_index=True,
3086+
help_text="Indicates whether this is the latest version of the advisory identified by its AVID.",
3087+
)
3088+
30843089
original_advisory_text = models.TextField(
30853090
blank=True,
30863091
null=True,
@@ -3133,6 +3138,11 @@ class AdvisoryV2(models.Model):
31333138
class Meta:
31343139
unique_together = ["datasource_id", "advisory_id", "unique_content_id"]
31353140
ordering = ["datasource_id", "advisory_id", "date_published", "unique_content_id"]
3141+
constraints = [
3142+
models.UniqueConstraint(
3143+
fields=["avid"], condition=Q(is_latest=True), name="unique_latest_per_avid"
3144+
)
3145+
]
31363146
indexes = [
31373147
models.Index(
31383148
fields=["avid", "-date_collected", "-id"],

0 commit comments

Comments
 (0)