Skip to content

Commit 0ab075b

Browse files
committed
Use microsecond in vulcoids
Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
1 parent 46b21c8 commit 0ab075b

2 files changed

Lines changed: 10 additions & 31 deletions

File tree

vulnerabilities/models.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,14 @@ class Vulnerability(models.Model):
6565
def save(self, *args, **kwargs):
6666
if self.vulnerability_id:
6767
return super().save(*args, **kwargs)
68-
# Generate unique VULCOID
69-
ie = None
70-
for attempt in range(1, 11):
71-
try:
72-
self.vulnerability_id = self.generate_vulcoid()
73-
# Using the context manager due to https://stackoverflow.com/a/23326971
74-
with transaction.atomic():
75-
return super().save(*args, **kwargs)
76-
77-
except IntegrityError as ie:
78-
sleep(0.5 * attempt)
79-
raise Exception("Failed to generate a unique VULCOID after 10 attempts") from ie
68+
self.vulnerability_id = self.generate_vulcoid()
69+
return super().save(*args, **kwargs)
8070

8171
@staticmethod
8272
def generate_vulcoid(timestamp=None):
8373
if not timestamp:
8474
timestamp = datetime.now()
85-
timestamp = timestamp.strftime("%Y-%m-%d%H%M%S")
75+
timestamp = timestamp.strftime("%Y%m%d-%H%M-%S%f")[:-4]
8676
return f"VULCOID-{timestamp}"
8777

8878
@property

vulnerabilities/tests/test_models.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@
3333
class TestVulnerabilityModel(TestCase):
3434

3535
def test_generate_vulcoid_given_timestamp_object(self):
36-
timestamp_object = datetime(2021, 1, 1, 11, 12, 13)
37-
expected_vulcoid = "VULCOID-2021-01-01111213"
36+
timestamp_object = datetime(2021, 1, 1, 11, 12, 13, 2000)
37+
expected_vulcoid = "VULCOID-20210101-1112-1300"
3838
found_vulcoid = models.Vulnerability.generate_vulcoid(timestamp_object)
3939
assert expected_vulcoid == found_vulcoid
4040

4141
def test_generate_vulcoid(self):
42-
expected_vulcoid = "VULCOID-2021-01-01111213"
43-
with freeze_time("2021-01-01 11:12:13"):
42+
expected_vulcoid = "VULCOID-20210101-1112-1300"
43+
with freeze_time("2021-01-01 11:12:13.0000"):
4444
found_vulcoid = models.Vulnerability.generate_vulcoid()
4545
assert expected_vulcoid == found_vulcoid
4646

@@ -52,22 +52,11 @@ def test_vulnerability_save_with_vulnerability_id(self):
5252
@pytest.mark.django_db
5353
def test_vulnerability_save_without_vulnerability_id(self):
5454
assert models.Vulnerability.objects.filter(
55-
vulnerability_id="VULCOID-2021-01-01111213"
55+
vulnerability_id="VULCOID-20210101-1112-1300"
5656
).count() == 0
5757

58-
with freeze_time("2021-01-01 11:12:13"):
58+
with freeze_time("2021-01-01 11:12:13.0000"):
5959
models.Vulnerability(vulnerability_id="").save()
6060
assert models.Vulnerability.objects.filter(
61-
vulnerability_id="VULCOID-2021-01-01111213"
62-
).count() == 1
63-
64-
assert models.Vulnerability.objects.filter(
65-
vulnerability_id="VULCOID-2021-01-01111214"
66-
).count() == 0
67-
68-
with freeze_time("2021-01-01 11:12:13", tick=True):
69-
# This context manager sets time to "2021-01-01 11:12:13" and starts the clock.
70-
models.Vulnerability(vulnerability_id="").save()
71-
assert models.Vulnerability.objects.filter(
72-
vulnerability_id="VULCOID-2021-01-01111214"
61+
vulnerability_id="VULCOID-20210101-1112-1300"
7362
).count() == 1

0 commit comments

Comments
 (0)