-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdomain.py
More file actions
75 lines (57 loc) · 2.21 KB
/
domain.py
File metadata and controls
75 lines (57 loc) · 2.21 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from django.db import models
from django.conf import settings
from api.models import AuditedModel
class Domain(AuditedModel):
owner = models.ForeignKey(settings.AUTH_USER_MODEL, 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']
def save(self, *args, **kwargs):
app = str(self.app)
domain = str(self.domain)
# get config for the service
config = self._load_service_config(app, 'router')
# See if domains are available
if 'domains' not in config:
config['domains'] = ''
# convert from string to list to work with and filter out empty strings
domains = [_f for _f in config['domains'].split(',') if _f]
if domain not in domains:
domains.append(domain)
config['domains'] = ','.join(domains)
self._save_service_config(app, 'router', config)
# Save to DB
return super(Domain, self).save(*args, **kwargs)
def delete(self, *args, **kwargs):
app = str(self.app)
domain = str(self.domain)
# get config for the service
config = self._load_service_config(app, 'router')
# See if domains are available
if 'domains' not in config:
config['domains'] = ''
# convert from string to list to work with and filter out empty strings
domains = [_f for _f in config['domains'].split(',') if _f]
if domain in domains:
domains.remove(domain)
config['domains'] = ','.join(domains)
self._save_service_config(app, 'router', config)
# Deatch cert, updates k8s
if self.certificate:
self.certificate.detach(domain=str(self.domain))
# Delete from DB
return super(Domain, self).delete(*args, **kwargs)
def __str__(self):
return self.domain