Skip to content

Commit

Permalink
feat(models): add validity constraint on certificate model
Browse files Browse the repository at this point in the history
  • Loading branch information
zyv committed Oct 30, 2023
1 parent 78b790c commit fa88ade
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
23 changes: 23 additions & 0 deletions logbook/migrations/0015_certificate_validity_after_issue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.6 on 2023-10-30 19:32

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("logbook", "0014_certificate_relinquished"),
]

operations = [
migrations.AddConstraint(
model_name="certificate",
constraint=models.CheckConstraint(
check=models.Q(
("valid_until__isnull", True),
("valid_until__gt", models.F("issue_date")),
_connector="OR",
),
name="validity_after_issue",
),
),
]
6 changes: 6 additions & 0 deletions logbook/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ class Certificate(models.Model):
relinquished = models.BooleanField(default=False)

class Meta:
constraints = (
CheckConstraint(
check=Q(valid_until__isnull=True) | Q(valid_until__gt=F("issue_date")),
name="validity_after_issue",
),
)
ordering = ("name", "-valid_until")

def __str__(self):
Expand Down

0 comments on commit fa88ade

Please sign in to comment.