-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdomain.py
More file actions
48 lines (41 loc) · 1.29 KB
/
domain.py
File metadata and controls
48 lines (41 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from django.db import models
from django.db import transaction
from django.contrib.auth import get_user_model
from api.models import AuditedModel
User = get_user_model()
class Domain(AuditedModel):
owner = models.ForeignKey(User, on_delete=models.PROTECT)
app = models.ForeignKey('App', on_delete=models.CASCADE)
domain = models.TextField(
blank=False, null=False, unique=True,
error_messages={
'unique': 'Domain is already in use by another application'
}
)
certificate = models.ForeignKey(
'Certificate',
on_delete=models.SET_NULL,
blank=True,
null=True
)
class Meta:
ordering = ['domain', 'certificate']
@transaction.atomic
def save(self, *args, **kwargs):
try:
# Save to DB
return super(Domain, self).save(*args, **kwargs)
finally:
self.app.refresh()
@transaction.atomic
def delete(self, *args, **kwargs):
# Deatch cert, updates k8s
if self.certificate:
self.certificate.detach(domain=str(self.domain))
try:
# Delete from DB
return super(Domain, self).delete(*args, **kwargs)
finally:
self.app.refresh()
def __str__(self):
return self.domain