-
Notifications
You must be signed in to change notification settings - Fork 61
/
aldryn_config.py
73 lines (63 loc) · 2.61 KB
/
aldryn_config.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
from aldryn_client import forms
def split_and_strip(string):
return [item.strip() for item in string.split(',') if item]
class Form(forms.BaseForm):
templates = forms.CharField(
'List of additional templates (comma separated)',
required=False,
)
alignment = forms.CharField(
'List of alignment types, default "left, center, right" (comma separated)',
required=False,
)
ratio = forms.CharField(
'The ratio used to calculate the missing width or height, default "1.618"',
required=False,
)
nesting = forms.CheckboxField(
'Allow plugins to be nested inside the picture plugin.',
required=False,
initial=False,
)
responsive_images = forms.CheckboxField(
'Enable responsive images technique',
required=False,
initial=False,
)
responsive_images_viewport_breakpoints = forms.CharField(
'List of viewport breakpoints (in pixels) for responsive images (comma separated)',
required=False,
)
def clean(self):
data = super(Form, self).clean()
# older versions of this addon had a bug where the values would be
# saved to settings.json as a list instead of a string.
if isinstance(data['templates'], list):
data['templates'] = ', '.join(data['templates'])
if isinstance(data['alignment'], list):
data['alignment'] = ', '.join(data['alignment'])
# prettify
for field in ('templates', 'alignment', 'responsive_images_viewport_breakpoints'):
data[field] = ', '.join(split_and_strip(data[field]))
return data
def to_settings(self, data, settings):
if data['templates']:
settings['DJANGOCMS_PICTURE_TEMPLATES'] = [
(item, item)
for item in split_and_strip(data['templates'])
]
if data['alignment']:
settings['DJANGOCMS_PICTURE_ALIGN'] = [
(item, item)
for item in split_and_strip(data['alignment'])
]
if data['ratio']:
settings['DJANGOCMS_PICTURE_RATIO'] = float(data['ratio'])
if data['nesting']:
settings['DJANGOCMS_PICTURE_NESTING'] = data['nesting']
settings['DJANGOCMS_PICTURE_RESPONSIVE_IMAGES'] = data.get('responsive_images', False)
breakpoints = data.get('responsive_images_viewport_breakpoints')
if breakpoints:
breakpoints = [float(x) for x in split_and_strip(breakpoints)]
settings['DJANGOCMS_PICTURE_RESPONSIVE_IMAGES_VIEWPORT_BREAKPOINTS'] = breakpoints
return settings