/
models.py
391 lines (305 loc) · 11.9 KB
/
models.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
"""Database models for storing site configuration."""
from __future__ import annotations
from typing import Dict, Optional
from django.contrib.sites.models import Site
from django.db import models
from typing_extensions import TypeAlias
from djblets.cache.synchronizer import GenerationSynchronizer
from djblets.db.fields.json_field import JSONField
from djblets.siteconfig.managers import SiteConfigurationManager
from djblets.util.typing import JSONDict, JSONValue
#: An alias for valid value types in site configuration settings.
#:
#: Version Added:
#: 3.1
SiteConfigurationSettingsValue: TypeAlias = JSONValue
#: An alias for the container of settings in a site configuration.
#:
#: Version Added:
#: 3.1
SiteConfigurationSettings: TypeAlias = JSONDict
_GLOBAL_DEFAULTS: SiteConfigurationSettings = {}
_DEFAULTS: Dict[int, SiteConfigurationSettings] = {}
class SiteConfigSettingsWrapper(object):
"""Wraps the settings for a SiteConfiguration.
This is used by the context processor for templates to wrap accessing
settings data, properly returning defaults.
"""
######################
# Instance variables #
######################
#: The site configuration associated with this wrapper.
#:
#: Type:
#: SiteConfiguration
siteconfig: SiteConfiguration
def __init__(
self,
siteconfig: SiteConfiguration,
) -> None:
"""Initialize the wrapper.
Args:
siteconfig (SiteConfiguration):
The site configuration to wrap.
"""
self.siteconfig = siteconfig
def __getattr__(
self,
name: str,
) -> Optional[object]:
"""Return an attribute from the site configuration.
If the attribute is not present in the site configuration's settings,
the registered default will be returned.
Args:
name (str):
The name of the attribute.
Returns:
object:
The resulting value from the site configuration or the default.
"""
return self.siteconfig.get(name)
class SiteConfiguration(models.Model):
"""Stored version and settings data for a Django site.
This stores dynamic settings for a site, along with version information,
allowing the application to alter and apply/synchronize settings across
threads, processes, and servers without restarting the server.
Consumers should not create or fetch their own instance of this class
through standard Django query functions. Instead, they should use
:py:meth:`SiteConfiguration.objects.get_current()
<djblets.siteconfig.managers.SiteConfigurationManager.get_current>`
instead. See the documentation for that method for details on how to safely
look up and use site configuration.
"""
site = models.ForeignKey(Site,
related_name='config',
on_delete=models.CASCADE)
version = models.CharField(max_length=20)
#: A JSON dictionary field of settings stored for a site.
settings = JSONField()
objects = SiteConfigurationManager()
@classmethod
def add_global_defaults(
cls,
defaults_dict: SiteConfigurationSettings,
) -> None:
"""Add a dictionary of global defaults for settings.
These defaults will be used when calling :py:meth:`get` for any setting
not stored. Defaults registered for a specific site configuration take
precedent over global defaults.
Args:
default_dict (dict):
A dictionary of defaults, mapping siteconfig settings keys to
JSON-serializable values.
"""
_GLOBAL_DEFAULTS.update(defaults_dict)
@classmethod
def add_global_default(
cls,
key: str,
default_value: SiteConfigurationSettingsValue,
) -> None:
"""Add a global default value for a settings key.
The default will be used when calling :py:meth:`get` for this key,
if a value is not stored. Defaults registered for a specific site
configuration take precedent over global defaults.
Args:
key (str):
The settings key to set the default for.
default_value (object):
The value to set as the default.
"""
cls.add_global_defaults({key: default_value})
@classmethod
def remove_global_default(
cls,
key: str,
) -> None:
"""Remove a global default value for a settings key.
Args:
key (str):
The settings key to remove the default for.
"""
_GLOBAL_DEFAULTS.pop(key)
@classmethod
def clear_global_defaults(cls) -> None:
"""Clear all default values for this site configuration.
This will clear only global defaults. This will not affect defaults
registered on specific site configurations.
"""
_GLOBAL_DEFAULTS.clear()
@classmethod
def get_global_defaults(cls) -> SiteConfigurationSettings:
"""Return all global defaults for settings.
Returns:
dict:
A dictionary of all registered global defaults for settings.
"""
return _GLOBAL_DEFAULTS
def __init__(self, *args, **kwargs) -> None:
"""Initialize the site configuration.
Args:
*args (tuple):
Positional arguments to pass to the parent constructor.
**kwargs (dict):
Keyword arguments to pass to the parent constructor.
"""
super().__init__(*args, **kwargs)
# Optimistically try to set the Site to the current site instance,
# which either is cached now or soon will be. That way, we avoid
# a lookup on the relation later.
cur_site = Site.objects.get_current()
if cur_site.pk == self.site_id:
self.site = cur_site
# Begin managing the synchronization of settings between all
# SiteConfigurations.
self._gen_sync = GenerationSynchronizer(
'%s:siteconfig:%s:generation' % (self.site.domain, self.pk))
self.settings_wrapper = SiteConfigSettingsWrapper(self)
def get(
self,
key: str,
default: Optional[SiteConfigurationSettingsValue] = None,
) -> Optional[SiteConfigurationSettingsValue]:
"""Return the value for a setting.
If the setting is not found, the default value will be returned. This
is represented by the default parameter, if passed in, or a global
default (from :py:meth:`add_default`) if set.
If no default is available, ``None`` will be returned.
Args:
key (str):
The site configuration settings key.
default (object, optional):
The default value to return. If not provided, the registered
default will be returned.
Returns:
object:
The resulting value.
"""
if default is None:
try:
default = _DEFAULTS[self.pk][key]
except KeyError:
default = _GLOBAL_DEFAULTS.get(key)
return self.settings.get(key, default)
def set(
self,
key: str,
value: SiteConfigurationSettingsValue,
) -> None:
"""Set a value for a setting.
The setting will be stored locally until the model is saved, at which
point it will be synchronized with other processes/servers.
Args:
key (str):
The key for the setting.
value (object):
The JSON-serializable object to store.
"""
self.settings[key] = value
def add_defaults(
self,
defaults_dict: SiteConfigurationSettings,
) -> None:
"""Add a dictionary of defaults for settings.
These defaults will be used when calling :py:meth:`get` for any setting
not stored. These will only be registered for this site configuration,
and will not be registered for global defaults.
Args:
default_dict (dict):
A dictionary of defaults, mapping siteconfig settings keys to
JSON-serializable values.
"""
_DEFAULTS.setdefault(self.pk, {}).update(defaults_dict)
def add_default(
self,
key: str,
default_value: SiteConfigurationSettingsValue,
) -> None:
"""Add a default value for a settings key.
The default will be used when calling :py:meth:`get` for this key,
if a value is not stored. This will only be registered for this site
configuration, and will not be registered for global defaults.
Args:
key (str):
The settings key to set the default for.
default_value (object):
The value to set as the default.
"""
self.add_defaults({key: default_value})
def remove_default(
self,
key: str,
) -> None:
"""Remove a default value on this site configuration.
This will remove only defaults registered on this site configuration.
This does not affect global defaults.
Args:
key (str):
The settings key to remove the default for.
"""
try:
del _DEFAULTS[self.pk][key]
except KeyError:
pass
def clear_defaults(self) -> None:
"""Clear all default values for this site configuration.
This will clear only defaults registered on this site configuration.
This does not affect global defaults.
"""
_DEFAULTS[self.pk] = {}
def get_defaults(self) -> SiteConfigurationSettings:
"""Return all defaults for this site configuration.
This will return only defaults registered on this site configuration.
The result does not include global defaults.
Returns:
dict:
A dictionary of all registered defaults for settings.
"""
return _DEFAULTS.get(self.pk, {})
def is_expired(self) -> bool:
"""Return whether or not this SiteConfiguration is expired.
If the configuration is expired, it will need to be reloaded before
accessing any settings.
Returns:
bool:
Whether or not the current state is expired.
"""
return self._gen_sync.is_expired()
def save(
self,
*args,
clear_caches: bool = True,
**kwargs,
) -> None:
"""Save the site configuration to the database.
By default, saving will clear the caches across all processes/servers
using this site configuration, causing them to be re-fetched on the
next request.
Args:
clear_caches (bool, optional):
Whether to clear the caches. This is ``True`` by default.
**kwargs (dict):
Additional keyword arguments to pass to the parent method.
"""
self._gen_sync.mark_updated()
if clear_caches:
# The cached siteconfig might be stale now. We'll want a refresh.
# Also refresh the Site cache, since callers may get this from
# Site.config.
SiteConfiguration.objects.clear_cache()
Site.objects.clear_cache()
super().save(*args, **kwargs)
def __str__(self) -> str:
"""Return a string version of the site configuration.
The returned string will list the associated site's domain and the
stored application version.
Returns:
str:
The string representation of the site configuration.
"""
return '%s (version %s)' % (self.site, self.version)
class Meta:
# Djblets 0.9+ sets an app label of "djblets_siteconfig" on
# Django 1.7+, which would affect the table name. We need to retain
# the old name for backwards-compatibility.
db_table = 'siteconfig_siteconfiguration'