-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathappsettings.py
More file actions
111 lines (95 loc) · 4.29 KB
/
appsettings.py
File metadata and controls
111 lines (95 loc) · 4.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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import logging
from django.conf import settings
from django.db import models
from django.contrib.postgres.fields import ArrayField
from api.models import UuidAuditedModel
from api.exceptions import DeisException, AlreadyExists
class AppSettings(UuidAuditedModel):
"""
Instance of Application settings used by scheduler
"""
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
app = models.ForeignKey('App', on_delete=models.CASCADE)
maintenance = models.NullBooleanField(default=None)
routable = models.NullBooleanField(default=None)
whitelist = ArrayField(models.CharField(max_length=50), default=[])
class Meta:
get_latest_by = 'created'
unique_together = (('app', 'uuid'))
ordering = ['-created']
def __str__(self):
return "{}-{}".format(self.app.id, str(self.uuid)[:7])
def new(self, user, whitelist):
"""
Create a new application appSettings using the provided whitelist
on behalf of a user.
"""
appSettings = AppSettings.objects.create(
owner=user, app=self.app, whitelist=whitelist)
return appSettings
def update_maintenance(self, previous_settings):
old = getattr(previous_settings, 'maintenance', None)
new = getattr(self, 'maintenance', None)
# If no previous settings then assume it is the first record and default to true
if not previous_settings:
setattr(self, 'maintenance', False)
self.app.maintenance_mode(False)
# if nothing changed copy the settings from previous
elif new is None and old is not None:
setattr(self, 'maintenance', old)
elif old != new:
self.app.maintenance_mode(new)
self.summary += ["{} changed maintenance mode from {} to {}".format(self.owner, old, new)] # noqa
def update_routable(self, previous_settings):
old = getattr(previous_settings, 'routable', None)
new = getattr(self, 'routable', None)
# If no previous settings then assume it is the first record and default to true
if not previous_settings:
setattr(self, 'routable', True)
self.app.routable(True)
# if nothing changed copy the settings from previous
elif new is None and old is not None:
setattr(self, 'routable', old)
elif old != new:
self.app.routable(new)
self.summary += ["{} changed routablity from {} to {}".format(self.owner, old, new)]
def update_whitelist(self, previous_settings):
# If no previous settings then assume it is the first record and do nothing
if not previous_settings:
return
old = getattr(previous_settings, 'whitelist', [])
new = getattr(self, 'whitelist', [])
# if nothing changed copy the settings from previous
if len(new) == 0 and len(old) != 0:
setattr(self, 'whitelist', old)
elif set(old) != set(new):
self.app.whitelist(new)
added = ', '.join(k for k in set(new)-set(old))
added = 'added ' + added if added else ''
deleted = ', '.join(k for k in set(old)-set(new))
deleted = 'deleted ' + deleted if deleted else ''
changes = ', '.join(i for i in (added, deleted) if i)
if changes:
if self.summary:
self.summary += ' and '
self.summary += "{} {}".format(self.owner, changes)
def save(self, *args, **kwargs):
self.summary = []
previous_settings = None
try:
previous_settings = self.app.appsettings_set.latest()
except AppSettings.DoesNotExist:
pass
try:
self.update_maintenance(previous_settings)
self.update_routable(previous_settings)
self.update_whitelist(previous_settings)
except Exception as e:
self.delete()
raise DeisException(str(e)) from e
if not self.summary and previous_settings:
self.delete()
raise AlreadyExists("{} changed nothing".format(self.owner))
summary = ' '.join(self.summary)
self.app.log('summary of app setting changes: {}'.format(summary), logging.DEBUG)
return super(AppSettings, self).save(**kwargs)