Newer
Older
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
from django import forms
from dynamic_preferences import types
SettingSection = types.Section
StringSetting = types.StringPreference
class PasswordSetting(types.StringPreference):
widget = forms.PasswordInput
class BooleanSetting(types.BooleanPreference):
# Boolean are supported in JSON, so no need to serialized to a string
serializer = None
class IntSetting(types.IntegerPreference):
# Integers are supported in JSON, so no need to serialized to a string
serializer = None
def validate_config(payload, settings):
"""
Dynamic preferences stores settings in a separate database table. However
it is a bit too much for our use cases, and we simply want to store
these in a JSONField on the corresponding model row.
This validates the payload using the dynamic preferences serializers
and return a config that is ready to be persisted as JSON
"""
final = {}
for klass in settings:
setting = klass()
setting_id = setting.identifier()
try:
value = payload[setting_id]
except KeyError:
continue
setting.validate(value)
if setting.serializer:
value = setting.serializer.serialize(value)
final[setting_id] = value
return final